struct or class - which is best to keep a collection of data records?

O

Oberon

I want to create a collection of records which will be permanently
available at the application level in an ASP.NET application.

How should I go about that?

Lets say that the record structure has 4 fields with these types:

type field name
============
integer recordID
string title
bool live
DateTime meetTime

The data should be held in a collection and will be static (making it
available at the application level).

Should I?
1. create a struct for the record.
3. create the collection class
3. create an Add method to add a struct to the collection class
4. use the Add method to add new instances of the struct

OR should I be doing this will a class instead of a struct?, or does
it depend on what else I might want to do with my collection class?


PS: I have a code example of how to do this in VB.NET but I will do it
in C#. The VB.NET uses a collection class where each member is a class
of the record type. But VB.NET doesn't have structs, does it?
 
O

orekinbck

Hey Oberon

In terms of collections, there are a bewildering variety of choices
available. Unless there are unique requirements I usually find that
an ArrayLists of classes does just fine:
- ArrayLists allow access by index
- ArrayList sizes are dynamic (unlike arrays)
- ArrayLists can be sorted (you can implement custom sorting through
IComparer classes).

If there only needs to be one instance of the collection, then make it
a static property of the class. I would not bother with
add/remove/update as methods of the class, as they are pretty much one
line implementations anyway.

Hope this helps
Bill

PS - When you say 'permanently available' are you storing the data in a
database or file ?
 
O

Oberon

Hey Oberon

In terms of collections, there are a bewildering variety of choices
available. Unless there are unique requirements I usually find that
an ArrayLists of classes does just fine:
- ArrayLists allow access by index
- ArrayList sizes are dynamic (unlike arrays)
- ArrayLists can be sorted (you can implement custom sorting through
IComparer classes).

If there only needs to be one instance of the collection, then make it
a static property of the class. I would not bother with
add/remove/update as methods of the class, as they are pretty much one
line implementations anyway.

Hope this helps
Bill

PS - When you say 'permanently available' are you storing the data in a
database or file ?

The data is in the database but I want to keep it in RAM to speed up
access. There isn't a lot of data. Maybe only 50 records and it is
pretty much used all the time; in that every user will need access to
it.
 
G

Guest

PS: I have a code example of how to do this in VB.NET but I will do it
in C#. The VB.NET uses a collection class where each member is a class
of the record type. But VB.NET doesn't have structs, does it?

VB.Net has a struct keyword, I've forgotten the name, though. ;)
I want to create a collection of records which will be permanently
available at the application level in an ASP.NET application.
The data should be held in a collection and will be static (making it
available at the application level).
Lets say that the record structure has 4 fields with these types:

type field name
============
integer recordID
string title
bool live
DateTime meetTime

You biggest concern will be the thread-safety of this collection.

Your second concern should be about the type of the collection, e.g. do you
plan to access the elements by index or key (recordID, title...). After
you've decided that, we can tackle the record itself.

Answer these questions for yourself:
Will you access the elements only by index?
Internally in your collection, do you use only an array (record[]) as data
structure?
Will you never inherit from the record type?
Are default values of null/0/false for all fields acceptable?

If you've answered all questions with "yes", you *might* use a struct. But
I'd recommend using a class. It gives you greater design freedom.
Should I?
1. create a struct for the record.
See above.
3. create the collection class Definately.

3. create an Add method to add a struct/class to the collection class Definately.

4. use the Add method to add new instances of the struct/class
You can have both Add methods.

HTH,
Mark
 
O

Oberon

PS: I have a code example of how to do this in VB.NET but I will do it
in C#. The VB.NET uses a collection class where each member is a class
of the record type. But VB.NET doesn't have structs, does it?

VB.Net has a struct keyword, I've forgotten the name, though. ;)
I want to create a collection of records which will be permanently
available at the application level in an ASP.NET application.
The data should be held in a collection and will be static (making it
available at the application level).
Lets say that the record structure has 4 fields with these types:

type field name
============
integer recordID
string title
bool live
DateTime meetTime

You biggest concern will be the thread-safety of this collection.

Your second concern should be about the type of the collection, e.g. do you
plan to access the elements by index or key (recordID, title...). After
you've decided that, we can tackle the record itself.

Answer these questions for yourself:
Will you access the elements only by index?
Yes.

Internally in your collection, do you use only an array (record[]) as data
structure?

Probably, although it might be a good iead to have a hashtable which
may then transform in to a Sorted List if need be. Does that rule out
a struct and suggest a class?
 
B

Bruce Wood

You should definitely use a class, not a struct.

Structs in .NET come with value semantics. This means that they are
_copied_ whenever they are assigned, and this can lead to truly
confusing behvaiour when you put them into collections.

My rule is a simple one: if it has an ID (such as the record ID you
indicated above) then it should definitely be a class, not a struct.

Structs are useful in a truly limited number of cases, in which you
want to mimic the behaviour of built-in types like int, double, or
DateTime.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top