C# Array Class Lists with Classes

koeks525

Executive Member
Joined
Jul 14, 2012
Messages
6,011
Reaction score
1,197
Location
Canada
Hey guys,

We started this new thing in C#, in class, where one class is an array, and it used like a list, for the other class (Eg having a list class and then a class for something else, like Houses). I understand the basics of this, but coding it up is still a bit confusing for me now (I am slowly getting it). I am busy looking for C# ebooks that I could read through, with regards to this topic. I couldn't exactly find a nice YouTube video tutorial on this.

I thought I should drop this question here, do you guys know of places that help explain this, in simple terms...I don't want to post my problem question (a practical) here, I will learn through practise. I just need a nice explanation of this (my professor is confusing me more :D )

Thanks
 
We started this new thing in C#, in class, where one class is an array, and it used like a list, for the other class (Eg having a list class and then a class for something else, like Houses). I understand the basics of this...

I don't. Could you elaborate with an example?
 
I don't. Could you elaborate with an example?

This is from the current practical we doing:

You need to write a program that will keep information on earthquakes in South Africa. For each earthquake you want to store the following: Magnitude, Town closest to the epicentre and the province the town is in. For this program develop the following classes:

Class Earthquake – it should have the normal methods you would associate with a “unit class”
Class List – A class that keeps a list of earthquakes. It should have the basic methods one would expect in a “list class”, including methods that do a linear search, a binary search (on town), as well as a Bubble Sort on the town.
 
Build a class that mimics the functionality of the framework's collection classes?

So you'll have class with a property that exposes a List<EarthQuake> object.
You'll then code the accessors for this property plus methods like InsertEartQuake(obj param1, obj param2, ...)
RemoveEarthQuake(obj param1)
 
IEnumerable(Of YourClass)

Lets say you are querying a database for messages. You get 500 records returned.

You'd have a MessageContainer class with stuff like int recordCount (stuff that makes sense in a header)

And then you'd define a different class Message, which has stuff like, string name, string messagebody etc

In the MessageContainer class you'd define a method Messages which is IEnumerable(Of Message). I've seen people use dictionary and List for this as well.

When you have data loaded into it, you'd simply loop through MessageContainer.Messages and cast each of them into a Message class and access the data in there as Message.name or Message.messagebody etc

http://msdn.microsoft.com/en-us/lib...?cs-save-lang=1&cs-lang=csharp#code-snippet-2
 
Okay well that Class List will just contain an ArrayList probably which you can sort and iterate through.

+1

What you are looking for is Collections; more specifically ArrayLists.
  1. Define your class to hold the data e.g. earthQuake.
  2. Define an ArrayList to hold the list occurrences e.g. ArrayList listOfEarthQuakes = new ArrayList()
  3. Then add the occurrences to the list: listOfEarthQuakes.Add(earthQuake);
 
So you'll have class with a property that exposes a List<EarthQuake> object.

Question is what the lecturer allows. It seems he needs to write his own adaptation of a List<T>.
 
Question is what the lecturer allows. It seems he needs to write his own adaptation of a List<T>.

Yep. Might need to use an arraylist that will allow for growing and shrinking data with the pop and push thing and then to reinvent the wheel.



:)
 
I can't see why one would want to use an ArrayList for something like this...

List<T> is strongly typed and supports Binary Searching (BinarySearch) as well as Linear searching (Contains).

This would be my first attempt with my limited understand of what you are looking for.

EarthQuakeRepository
-List<Province>
-List<Town>
-List<EarthQuake>

Town
-Name
-Province

Province
-Name
-List<Town>

EarthQuake
-Magnitude
-Town (This would be a reference to a Town object in the List<Town> so that you could type earthquake.Town.Province.Name if you wanted to)

EDIT: I'm not sure when last I saw an ArrayList being used. I imagine that the only reason that they have not marked it obsolete is to prevent compiler warnings on legacy code.
 
Last edited:
Like Bar0n said - depends on the brief on the allowable tech...

The teacher might want to see your implementation and understanding of the binary search and your implementation of a List<T> type of container.
 
EDIT: I'm not sure when last I saw an ArrayList being used. I imagine that the only reason that they have not marked it obsolete is to prevent compiler warnings on legacy code.

Performance on ArrayList is also not as good as List<T> in some scenarios.

I'd also just stick to List<T>
 
Performance on ArrayList is also not as good as List<T> in some scenarios.

I'd also just stick to List<T>

Definitely. In the old .NET 2ish days I used only ArrayList, until at some point I got around to benchmarking the two.
Ater that I upgraded all my old code to List<> asap.

As for coding problems, youtube isn't really the greatest. A video can usually show you how to do 1 specific thing but you won't learn much else. Your problem is really specific, so you'll have problems finding info on the whole of it.

Read e.g. http://www.dotnetperls.com/ for some generic C# info. Then read up more about e.g. classes and sorting algorithms. From there you can hopefully implement the 2 classes, then write custom search methods or extend the existing search methods with your own parameters (e.g. by Town Name), depending on how advanced your class is.
 
I can't see why one would want to use an ArrayList for something like this...

List<T> is strongly typed and supports Binary Searching (BinarySearch) as well as Linear searching (Contains).

This would be my first attempt with my limited understand of what you are looking for.

EarthQuakeRepository
-List<Province>
-List<Town>
-List<EarthQuake>

Town
-Name
-Province

Province
-Name
-List<Town>

EarthQuake
-Magnitude
-Town (This would be a reference to a Town object in the List<Town> so that you could type earthquake.Town.Province.Name if you wanted to)

EDIT: I'm not sure when last I saw an ArrayList being used. I imagine that the only reason that they have not marked it obsolete is to prevent compiler warnings on legacy code.

Any course that doesn't mention that these old collections should not be used isn't a good course. Essentially it means that course is pre 2.0. I believe that Silverlight doesn't even have ArrayList at all.

I seen some books that suggest it might be deprecated in future, but who knows.
 
Top
Sign up to the MyBroadband newsletter
X