tags:

views:

177

answers:

4

I have a function that returns this object:

 var result = AjaxUserToPlacePaging();

 //this is the object
 object[] objcs = new object[1];

 objcs[0] = new
 {
     countHereNow = countHereNow.ToString(),
     countWillBeHere = countWillBeHere.ToString(),
     countWasHere = countWasHere.ToString()
 };

How can I extract the information from the object?

for example

 int count  = result[0].countHereNow;

the resun i nned the function to be object is because i using jquery and with the jquery it takes the object and converting it into json

+2  A: 

I recommend making a custom class, and storing that, instead of just storing an anonymous type into an "object" directly.

objcs[0] = new MyClass
{
    countHereNow = countHereNow.ToString(),
    countWillBeHere = countWillBeHere.ToString(),
    countWasHere = countWasHere.ToString()
};

That will allow you to cast it back, when you retrieve:

MyClass myClass = result[0] as MyClass;
if (myClass != null)
{
    int count = myClass.countHereNow;
}

There are tricks you can do to work with anonymous types, but in most cases, they just make the code less maintainable.

Reed Copsey
`if (myClass != null)`. This is C#.
Mehrdad Afshari
if you impleicity declare the array, you wounld't have to create a type, as per answer below.
Jaimal Chohan
@Mehrdad: Thanks - fixed. @Jaimal Chohan: That works fine, provided you don't want to use this outside of your local scope (ie: return from a method). It's less than obvious, though, IMO.
Reed Copsey
If you know what you're doing and using caching wisely, anonymous types combined with reflection can be very powerful, as shown by the ASP.NET MVC framework.
DrJokepu
@DrJokepu: I completely agree, and use them regularly, but they can also (easily) lead to maintanence nightmares. I still feel that creating your own custom class is often the best practice, rather than letting the compiler create a class for you.
Reed Copsey
+4  A: 

You will need to use Reflection:

string countHereNow = string.Empty;
Type objType = result[0].GetType();
PropertyInfo prop = objType.GetPropertyInfo("countHereNow");
countHereNow = prop.GetValue(result[0], null) as string ?? string.Empty;

Typically I think returning an anonymous type is a bad idea, but maybe you have a reason for it. Otherwise, I would create a custom class and just return an array of that class type.

Max Schmeling
this return CS0021: Cannot apply indexing with [] to an expression of type 'object'
avi
What exactly are you returning? Your code doesn't show that... you'll need to make sure "result" is of type object[] for this to work...
Max Schmeling
A: 

You need to use reflection (or something that uses reflection) to get the contents of your anonymous object outside the function where you create it. Maybe you should create a class and return instances of that instead?

svinto
A: 

You're currently declaring the array as an object array

object[] objcs = new object[1];

Declare it as a implicitly typed (var) array

var objcs = new [];

That way the objects you actually adding to the array won't be of SystemObject type but will be implicitly typed

Jaimal Chohan
huh? this wouldn't solve his issue at all...
Stan R.
Sure it would. By impliclty declaring the type, you don;t have to explicity create a class to access the properties.
Jaimal Chohan
If fact, its all here. http://msdn.microsoft.com/en-us/library/bb384090.aspx
Jaimal Chohan
I'm not sure, but I would assume you can't return that from a function though.
Max Schmeling
+1: This is the simplest option IF you're only using this within the same method.
Reed Copsey
Thats very true, I had a case of RTFQ :(
Jaimal Chohan