Object and field Returning


I have a question that I need some assistance on. So, any feedback would be greatly appreciated and examples would be great as well.

If I have an object in a list, for instance, List<Cars>. If I want to return that with an addition row that indicates how many cars I have in that List. (i.e. Cars.Count) - how would I do that.

For instance, the cars contain:

Lexus, 2013, 4 door
Mercedes, 2012, 2 door
Jetta, 2008, 4 door
Explorer, 2010, 4 door
Jaquar, 2013, 4 door
Focus, 2007, 2 door

The cars are in the list of the Cars object and the 6 is the list count but I need them it to show like that on a return of a function.

How would/can I do that?

Any assistance would be greatly appreciated.

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.

Kyle AbrahamsSenior .Net DeveloperCommented:
You wouldn't.

Cars is a class which holds information about a car.

a car has no knowledge of other cars, or how many were returned in this thing called a query from the database.

You could, of course, just access the property of the list itself:

List<Cars> cars = new List<Cars>();
TheCount.Text = cars.Count;  //TheCount being a textbox or a label.
you could make it a list of objects and then cast back....
or return a Tuple.
davismAuthor Commented:
Do you happen to have a sample of that? I'm not entirely sure I understand what you are getting at.

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

you would have your method return a tuple and then build and return it at the end.

Tuple<List<Cars>, Int32> retVal = new Tuple<List<Cars>, Int32>(yourListOfCars, yourListOfCars.Count);

and you would access the values with retVal.Item1 (your list) reVal.Item2 (the count)

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
there are a lot of tutorials out there using them... they don't want us to post links anymore.
as far as the objects list:

            List<Object> objs = new List<object>();
davismAuthor Commented:

I will take a look at the other tutorials.

With respect to objects list...yep, already tried something list that but didn't work out as expected.

I will take a look at that again.

I'll get back to you on info.

Might be able to help further if you could tell us what you are going to need this setup for....
are you trying to bind something? are you trying to do something dynamically?
davismAuthor Commented:
It's basically like creating a file used for exchanging data that includes a header, body (content), and footer.

The typically, I have seen something and have something like this in a file context using like Perl, but this is coming from a web service response which will turn out to be a serialized xml and done in c#.
davismAuthor Commented:
Excellent p_davis! Worked like a charm through the web service. The soap nodes weren't all clear cut and the wsdl showed some strangeness when a tuple was used but it worked none-the-less.

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

From novice to tech pro — start learning today.