473,803 Members | 4,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design question : struct or class

Hello,

I've got a design question. I need to keep track of some variables and I am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

Since there's no real business logic my choice would be structs. But looking
at the size of the objects in memory I would think they are a bit large to
put on the stack and should be better of on the heap. Which would mean I
should use classes.

Any ideas about what would be best?

TIA

Yves
Nov 15 '05
26 1941
I would suggest grouping your 'data' into structs/classes (based on the
your need for value type/reference type. You would potentially
end up w/ many of these structures and not just 'one' global placeholder.

For more, check out the structural design patterns @ c2 -
http://c2.com/cgi/wiki?CategoryStructuralPatterns

- raghu
codymanix wrote:
I've got a design question. I need to keep track of some variables and I


am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would


depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

You should *Always* use classes. structs are only needed in very rare
circumatances which isn't the case.

Nov 15 '05 #11
peter x <la******@boltb lue.com> wrote:

<snip>
When objects are passed to a method, they are passed by reference.


No they are not. "Pass by reference" has a very specific meaning, and
it *doesn't* apply here (unless you use the ref modifier). There is a
big difference between a reference being passed by value (which is what
actually happens) and a parameter itself being passed *by* reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #12
I stand corrected :) Although I'm not sure I totally understand your
response. Can you send me an example to help me clarify. Apart from this
point was the rest of the assessment correct? specifically the point about
the overhead involved in passing by value. I may need to reassess how I
program

Cheers, Peter
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
peter x <la******@boltb lue.com> wrote:

<snip>
When objects are passed to a method, they are passed by reference.


No they are not. "Pass by reference" has a very specific meaning, and
it *doesn't* apply here (unless you use the ref modifier). There is a
big difference between a reference being passed by value (which is what
actually happens) and a parameter itself being passed *by* reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #13
peter x <la******@boltb lue.com> wrote:
I stand corrected :) Although I'm not sure I totally understand your
response. Can you send me an example to help me clarify.
Sure.

Here's what pass by value means:
The value of the expression in the actual parameter (i.e. in the
calling code) becomes the value of the new variable which is the formal
parameter (i.e. in the method itself).

Here's what pass by reference means:
Only a variable (non-read only) can be used as a pass by reference
parameter, and the formal parameter variable in the called method is
effectively aliased with the actual parameter - they are different
names for the same memory location, so changing the value of one
changes the value of the other.

Note that nowhere in those definitions is the type of the parameter
(i.e. reference type or value type) used. Both value type parameters
and reference type parameters can be passed by value or by reference,
and in both cases the default is "by value". The only twist here is
understanding that the value of a reference type expression (or
variable) isn't an object - it's a reference. So, if I do:

string x = "hello";
Foo (x);
....
void Foo (string y)
{
....
}

the string itself isn't passed - the string *reference* (the value of
x) is passed by value.

For more examples etc, have another look at the article I linked to
before.
Apart from this point was the rest of the assessment correct?
As far as I saw, yes - I only skimmed it though. (The usual terminology
is reference type rather than object type though.)
specifically the point about
the overhead involved in passing by value. I may need to reassess how I
program


Unlikely. You should indeed by using classes most of the time, and
passing a reference by value is indeed cheaper than passing a large
value type value.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #14
Yes, if the memory that the object is going to occupy is large, the
usual choice is a class but, if the object is going to contain only
data-members, then it is beter to use a struct as a class is generally
resolved as a container for logic apart from data-members.

with regards,
J.V.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID= P3966388&BN=999 &PN=2
- Or, just search on "J.V.Ravichandr an"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #15
As Jon has stated, objects, by default, are passed by value. Structures
have the overhead of value semantics, as I see it, just like C++
classes,
but without the benefit of C++'s deterministic destructors. Thus the
limited usefullness of structures in C#.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #16
Jeff Louie <an*******@devd ex.com> wrote:
As Jon has stated, objects, by default, are passed by value.


I didn't quite state that - I stated that *parameters* are passed by
value by default. Reference type objects are never passed in any way,
as the type of an expression is never a reference type object, only a
reference.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #17
L#
On Fri, 30 Jan 2004 00:56:36 +0100, "codymanix"
<do************ *********@gmx.d e> wrote:


You should *Always* use classes. structs are only needed in very rare
circumatance s which isn't the case.


You should use structs if they are very small. They live on the stack
and are therefore faster. But if they are too big, the advantage of
being faster dissappears. If they become too big, the framework will
put them on the heap anyway, even if you declared it as a struct.

So, is they are very small, use structs, else use classes.

--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be
Nov 15 '05 #18
L#
On Sat, 31 Jan 2004 12:05:33 -0000, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:
peter x <la******@boltb lue.com> wrote:

<snip>
When objects are passed to a method, they are passed by reference.


No they are not. "Pass by reference" has a very specific meaning, and
it *doesn't* apply here (unless you use the ref modifier). There is a
big difference between a reference being passed by value (which is what
actually happens) and a parameter itself being passed *by* reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.


Being objects, they are passed by value; meaning that the pointer to
the object is passed by value. Eventually it has the same effect as
being passed by reference. Placing the ref keyword in front of it,
won't make a difference.

--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be
Nov 15 '05 #19
"L#" <ludwig_(nospam please)stuyck@p andora(nospampl ease).be> wrote in message
news:oo******** *************** *********@4ax.c om...
On Fri, 30 Jan 2004 00:56:36 +0100, "codymanix"
<do************ *********@gmx.d e> wrote:


You should *Always* use classes. structs are only needed in very rare
circumatance s which isn't the case.


You should use structs if they are very small. They live on the stack
and are therefore faster. But if they are too big, the advantage of
being faster dissappears. If they become too big, the framework will
put them on the heap anyway, even if you declared it as a struct.

So, is they are very small, use structs, else use classes.

Hi L#,

Structs reside on the stack if they are parameters or local variables,
otherwise they are stored on the heap. The best way to determine whether
the size of the struct is affecting performance is to benchmark your
application. I've never seen or heard anything that would make me believe
that the size of a value type determines whether they reside on the stack or
on the heap.

Jon Skeet wrote a nice article that explains this better:
http://www.yoda.arachsys.com/csharp/memory.html.

The reason you would use a struct instead of a class is if you need
value-type semantics, similar to int, float, or DateTime.

Joe
--
http://www.csharp-station.com
Nov 15 '05 #20

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

Similar topics

8
3774
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various events. The callbacks specified by the subscriber will be called when the events get trigerred in a different thread. Each event has different kinds of data associated with it. To achieve this I have the following: // The following describes the...
11
1428
by: | last post by:
I have a rather newbie question regarding design of class hierarchy. Suppose I have a class Class0, and need to implement a public Class0.compute() interface. There are three different ways to choose the implementation, and the user of the class has to be able to make that choice. Here is what I have in mind. I declare three classes, ClassA, ClassB, ClassC, and each of them has a distinct compute() method that can be used by Class0. ...
31
2000
by: grahamo | last post by:
This came up in an interview I did a while ago and I wanted to know the correct answer. The setup is this; If I have a base class "food" and also two classes "meat" and "veg" that inherit from food, thus; food / \ / \ meat veg
0
1122
by: ma740988 | last post by:
I've provided a stripped down version - as best I could - to get us by. That said, I'm in a quandry with respect to a design here. Consider: // stream.h #ifndef STREAM_H #define STREAM_H # pragma warning (disable : 4786) // if using microsofts compiler ver6
0
1546
by: ma740988 | last post by:
I'm going through modern C++ design looking for tips and while hi-tech I suspect one solution to my issue would involve the factory design pattern. // algorithms.h class Algorithms { protected: typedef std::deque<double> DDEQUE; // need to make this even more generic to support floats .. i.e float and double public:
5
1849
by: aaragon | last post by:
Hello everybody, I appreciate your taking the time to take a look at this example. I need some help to start the design of an application. To that purpose I'm using policy-based design. The idea is to have a Class that stores elements of Class2 in different ways (arrays in stack memory, arrays in heap memory, using the std::vector and so on). I would like the user to customize the creation of a class with Class2 and StoragePolicy like...
3
2490
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
8
1874
by: obrianpatrick | last post by:
Hi, I am relatively new to object oriented programming and design. I am developing an application in VS 2005. I am having the following design problem: I have two interfaces X and Y. Y is derived from X as the following: __interface X {
1
1615
by: mattmao | last post by:
I am brand new to C#.NET so here is my trial on this lab exercise: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace lab02exec { public class Program
0
9703
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
10069
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...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
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
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
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.