473,394 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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?

Nov 17 '05 #1
5 1860
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 ?

Nov 17 '05 #2
On 18 May 2005 07:29:38 -0700, or*******@yahoo.com.au wrote:
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.

Nov 17 '05 #3
> 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
Nov 17 '05 #4
On Wed, 18 May 2005 09:30:00 -0700, StealthyMark
<St**********@discussions.microsoft.com> wrote:
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?
Will you never inherit from the record type?
Never.
Are default values of null/0/false for all fields acceptable?
Yes.
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


Nov 17 '05 #5
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.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Joshua Beall | last post by:
Hi All, What is the best way to use a cookie to remember a logged in user? Would you store the username and password in two separate cookies? Should the password be plain text? Hashed? Not...
8
by: J.Marsch | last post by:
Ok, I think that what I'm seeing is due to some implicit boxing/unboxing going on, but I'd like to understand this a little better. I have a collection of structs. If I try to iterate the...
1
by: Sam | last post by:
Hi, I write my code behind in C# and everytime I need a new functionality, I build a class for it and save it in a folder I call ClassLibrary. My question is I don't know how to send my web...
4
by: Vadivel Kumar | last post by:
Hi Guys, I have a doubt which is little bit conceptual rather than a coding convention. I have a a table called products in which I have 40000 and odd products which is expected to be...
7
by: Buddy Ackerman | last post by:
I created this class Public Class HTMLFileInput : Inherits System.Web.UI.HtmlControls.HtmlInputFile Public Property Data As String Get Return ViewState("HTMLFileInput.Data") End Get Set...
34
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have...
1
by: Russell Mangel | last post by:
Sorry about the Cross-Post, I posted my question in the wrong group. Hello, What is the simplest way to create a dynamic collection (during run-time), using basic C (Struct data types). Since...
6
by: Glenn | last post by:
OK, need help in translating what I'd like to do in old school C to best method C# If I was wanting to create an array of struct's, writing and reading them to/from a file.. how would I do this...
4
by: robert.waters | last post by:
I have to parse a binary file having a number of fixed records, each record containing datas in fixed positions. I would like to parse this binary file into an array of structures having members...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.