473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrays of classes or structs, which is best

Hi,

I have a number of queue structures. I want to have multiple instances of
these,

i.e Instance 1 Instance 2 Instance 3 ...
------------ ------------ ------------
queueA queueB queueC

I was thinking of usind a structure or a class to store the queues and then
having an array of theses structures or classes.

As I am only storing queues what is the best object to hold them, a class, a
structue or another data structure?

Thanks
Macca
May 26 '06 #1
5 1370
Read recomendations there http://www.jaggersoft.com/pubs/StructsVsClasses.htm
I have a number of queue structures. I want to have multiple instances of
these,

i.e Instance 1 Instance 2 Instance 3 ...
------------ ------------ ------------
queueA queueB queueC

I was thinking of usind a structure or a class to store the queues and then
having an array of theses structures or classes.

As I am only storing queues what is the best object to hold them, a class, a
structue or another data structure?


--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

May 26 '06 #2
I don't find that reference very helpful in deciding whether to use
struct or class. It is very good at covering the small-scale
differences between the two, but doesn't really give a high-level view
of when one is more appropriate than the other.

May 26 '06 #3
Use a class. In this situation you absolutely don't want a struct. I
can think of no good reason to have a struct holding a collection. The
..NET Framework's Queue is a class... and there's a good reason for
that.

On top of this, you're storing them in an aggregate structure, which
(at least in .NET 1.1) will cause counterintuitiv e behaviour if you use
struct. (Not to say that storing structs in ArrayLists or other
collections is wrong... just that it produces behaviour that is
difficult to understand.)

Use a class. Even better, just use the Queue class from the Framework.

May 26 '06 #4
On Fri, 26 May 2006 03:29:02 -0700, Macca
<Ma***@discussi ons.microsoft.c om> wrote:
Hi,

I have a number of queue structures. I want to have multiple instances of
these,

i.e Instance 1 Instance 2 Instance 3 ...
------------ ------------ ------------
queueA queueB queueC

I was thinking of usind a structure or a class to store the queues and then
having an array of theses structures or classes.

As I am only storing queues what is the best object to hold them, a class, a
structue or another data structure?

Thanks
Macca


A struct is a value type (on the stack); a class is a reference type
(on the heap). Only use structs in specific cases (see
http://msdn.microsoft.com/library/de...uidelines.asp).

In your case, a class would be a good choice.
--
Ludwig Stuyck
http://www.coders-lab.be
May 26 '06 #5
Ludwig <no**@none.co m> wrote:
A struct is a value type (on the stack); a class is a reference type
(on the heap).


Note that structs can also end up on the heap, as part of other
objects. See
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 27 '06 #6

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

Similar topics

1
368
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct that contains an array of other structs. typedef struct { int nWidth; int nHeight; DWORD dwFlags; } HCA_MODE; typedef struct
19
2853
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type > >pointer-to-array-of-size-N-of-type-T (which is fine) and not having type > >array-of-size-N-of-type-T (with some exceptions, which is curious). > > So far > >the consensus seems to be that while everyone is aware of this no one knows
7
7527
by: Rennie deGraaf | last post by:
A question regarding this code, which defines a struct containing a size and a variable-sized array: typedef struct { uint16_t count; unsigned char bytes; } foo_t; ....
33
3876
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
14
4950
by: Stainless | last post by:
I have a public class Globals, which obviously holds all my global data. I have an array of 243 items, currently structs of type typedef struct STAR { int x; int y; int stellar_class; }t_star;
19
3844
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as dictionary keys, based on their content, not their identity. Is this feasible using the arrays directly, or do I need to wrap them in a struct that handles GetHashCode and Equal? If so, is such a wrapper present in the standard class library?
10
4996
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
64
3439
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like pointers. Can someone please explain why? thanks. Zytan
29
2792
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.