Using Var in C#

I have following piece of code

foreach(var item in Someclass.GetDetails(Obj1))

{
txtbox1.text=item.myProperty1;
txtbox2.text=item.myProperty2;
//txtbox3.text=
}

Open in new window


 When I debug the above code, the item in foreach loop contains methods "GetType","GetHashType" and properties "myProperty1" and "myProperty2".

I have a new requirement  and now I need to assign a property "myProperty3" to txtbox3.text in the above foreach loop.

The Parameter object  "Obj1" in the Someclass.GetDetails(Obj1) contains more methods and properties and one of the property is "myProperty3".

How can I assign "myProperty3"  to a text box using item in the above foreach loop code.
ksd123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
You can use casting - assuming the item is actually a 'foo' object then something like this:
txtbox1.text=((foo)item).myProperty1;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
Alternative is not to use var in the foreach loop

foreach(foo item in Someclass.GetDetails(Obj1))

{
txtbox1.text=item.myProperty1;
txtbox2.text=item.myProperty2;
//txtbox3.text=
}
0
ksd123Author Commented:
In this statement foreach(var item in Someclass.GetDetails(Obj1)),  what is type of "item"? Here I am using "item" for the first time.

In my original question ,I have below piece of code

txtbox1.text=item.myProperty1;
txtbox2.text=item.myProperty2;

Can I assign like this in the foreach loop?

txtbox3.text=obj1.myProperty3;
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

AndyAinscowFreelance programmer / ConsultantCommented:
foreach(var item in Someclass.GetDetails(Obj1))

In that line of code the var is the type of object (var being a built in general type), item is the name of the variable in the code.

It could easily be written as
foreach(foo abc in Someclass.GetDetails(Obj1))
as long as you do have a definition for an object (class or struct) that is called foo


>>Can I assign like this in the foreach loop?
Inside the foreeach loop you can use any methods/properties that the object has.  Using the generic type var means that only the methods/properties belonging to that can be used.
0
käµfm³d 👽Commented:
(var being a built in general type)
I'm sure that I'm misinterpreting what is attempting to be said in that bit, but var itself is not a type. The var keyword causes the compiler to infer the type from the thing that is on the right-hand side of the = symbol. For example:

var x = 1;  // x is of type int
var y = "hello world!"; // y is of type string
var z = new Foo();  // z is of type Foo

In your example, item's type will be that of whatever the return type of the GetDetails method. You can mouse over the variable within the IDE to see what type has been inferred:

Screenshot
More information:  http://msdn.microsoft.com/en-us/library/bb383973.aspx
0
käµfm³d 👽Commented:
P.S.

Sometimes var can't effectively infer the type of the thing on the right. In such cases the compiler will default the type to object (as everything in .NET is an object). You may be encountering such an instance. If that be the case, then you will need either explicit type declaration or casting, as AndyAinscow mentioned.
0
AndyAinscowFreelance programmer / ConsultantCommented:
@kaufmed - I'd misunderstood what exactly var was, I was confusing it with variant.

OK, if myProperty3 doesn't appear in the list of available methods/properties for the 'item' then you do need to cast it explicitly - see my first comment.
0
ksd123Author Commented:
Thanks AndyAinscow and kaufmed for the detailed explanation on var.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.