instantiate application object and initialize a list [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to instantiate application object and initialize a list with it.
I have this code in VB.net but I cant convert it to C#.
this is VB code:
If Not IsNothing(Application("MessageList")) Then
MessageList = Application("MessageList")
End If

You have to compare the Application collection object with null. Also type cast the object returned by Application collection to MessageList if MessageList is a type.
if(Application["MessageList"] != null)
MessageList = (List) Application["MessageList"];
Edit based on comments
The MessageList of List of Message and declared
List<Message> MessageList = new List<Message>();
Your condition would be.
if(Application["MessageList"] != null)
MessageList = (List<Message>) Application["MessageList"];

Related

Property IncreaseValue by reflection [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i'm need to know how to add a value to a property using reflection
example i have property with name Health
i want to increase the value by 10
var prop = typeof (Character).GetProperty(actionParams.PropertyName);
var oldValue = (int)prop.GetValue(client.Character);
prop.SetValue(client.Character, Convert.ChangeType((oldValue + 10), prop.PropertyType));
but i get cast error
System.InvalidCastException: Specified cast is not valid.
Most probably your property with name Health is not of type int, so the cast (int)prop.GetValue(client.Character); fails.

Web service WHERE / ORDER BY (?) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a web service that returns the list of all the clients.
I can get the list into a gridview by calling the method get all data like this.
test.RH_WebServiceService ligar = new test.RH_WebService();
test.baseList[] data = ligar.getAllData();
The thing is I wanted to filter it by name (for example) I've been reading online and people have said to me that I can just do it like this:
test.baseList[] data = ligar.getAllData().Where(condition);
However I can't get it to work. Do you guys have any ideas?
Assuming you are using Linq then you can just do:
test.baseList[] data = ligar.getAllData().Where(d => d.Name == "John");
The d is a random letter given to the object. Name is what i am assuming your property is called. Although i would recommend creating a method in your service that you pass the name in and get back the filtered data. That way you only return data you need which will improve performance. Something like this:
test.baseList[] data = ligar.getDataByName("John");
Maybe this will help some:
test.baseList[] data = ligar.getAllData().Where(f=> f.field == "value").ToArray();

Database field Enum to C# List [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
In my database I have a Enum field. I want to put those Enum attributes in a List in C#.
I searched but I couldn't find the answer. Is this possible?
I use a MySqlDatabase. If I want to get the rows from the database I use:
using (var uow = new UnitOfWorkScope<TrackerEntities>(UnitOfWorkScopePurpose.Reading))
In my application I use the Entity Framework
if you loading data in a data reader, however this is sql-server way. But point here is to convert string into enum
var list = new List<YourEnumType>();
var field= reader["DBFieldName"] != DBNull.Value ? reader["DBFieldName"].ToString()
: "";
var myField=(YourEnumType) Enum.Parse(typeof(YourEnumType ), field);
list.Add(myField);
Also have a look at The ENUM Type
You'll need to make a query against the schema table to list the enum values.
SELECT COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = #TABLE_NAME AND
COLUMN_NAME = #COLUMN_NAME

ArrayList<Uri> Intent. How to write in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to write this in C#?
Intent intent = new Intent(Intent.ActionSendMultiple, Android.Provider.MediaStore.Images.Media.ExternalContentUri);
handler(intent);
....
void handler(Intent intent)
{
ArrayList<Uri> imageUris = intent.GetParcelableArrayListExtra(Intent.ExtraStream);
...
}
ArrayList is available is c# but it does not have a generic (<T>) form. ArrayList is a list of objects.
What you want is System.Collections.Generic.List<T>
EDIT : In response to comments, try this;
List<Uri> imageUris = new List<Uri>(intent.GetIntegerArrayListExtra(Intent.ExtraStream));
Easy, just use List<Uri>. It's also faster than ArrayList.

cast List object to new object extending List [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to generate a custom xml doc, using this class:
[CollectionDataContract(Name="Applications", ItemName="Application")]
public class ApplicationNamesList : List<string> { }
The xml output im aming at should look like this
<Applications>
<Application>...</Application>
<Application>...</Application>
<Application>...</Application>
</Applications>
But once I have a List<string> object and try and cast this to ApplicationNamesList i get an InvalidCastException.
Is there something basic im not getting here?
A List<string> simply isn't a ApplicationNamesList. You would need to do something like this:
var result = new ApplicationNamesList();
result.AddRange(list);
With list being a List<string>.
Sometimes it helps to use real world examples:
Every Porsche ( => ApplicationNamesList) is a car ( => List<string>). But not every car is a Porsche.

Categories