design question : struct or class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phoenix

    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


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: design question : struct or class

    Phoenix,[color=blue]
    > Since there's no real business logic my choice would be structs.[/color]
    Structs can have "business logic" as equally well as classes.

    I normally make every thing a class, unless I know I need a struct, I use
    the following to know I need a struct:
    - act like a primitive type
    - have an instance size under 16 bytes
    - are immutable
    - value semantics are desirable



    Hope this helps
    Jay

    "phoenix" <patient0@skyne tWORK.be> wrote in message
    news:O1efil54DH A.1948@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hello,
    >
    > I've got a design question. I need to keep track of some variables and I[/color]
    am[color=blue]
    > 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[/color]
    depend[color=blue]
    > 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[/color]
    looking[color=blue]
    > 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
    >
    >[/color]


    Comment

    • Peter Rilling

      #3
      Re: design question : struct or class

      It sounds like your object has the potential of being quite large. Remember
      that structures are allocated on the stack, which is finite. I don't know
      the stack size off the top of my head.

      "phoenix" <patient0@skyne tWORK.be> wrote in message
      news:O1efil54DH A.1948@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Hello,
      >
      > I've got a design question. I need to keep track of some variables and I[/color]
      am[color=blue]
      > 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[/color]
      depend[color=blue]
      > 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[/color]
      looking[color=blue]
      > 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
      >
      >[/color]


      Comment

      • Jeff Louie

        #4
        Re: design question : struct or class

        Pheonix... To be clear, if you add structures to a .NET _collection_,
        they
        will be boxed anyway.

        Regards,
        Jeff[color=blue]
        >array of max 255 of these classes/structs (in most cases it will be[/color]
        less
        then 5 however)<


        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Jeff Louie

          #5
          Re: design question : struct or class

          Peter... To be clear, structures can be inlined if they are part of a
          class or
          ?array.

          Regards,
          Jeff[color=blue]
          >Remember that structures are allocated on the stack, which is finite.<[/color]

          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • codymanix

            #6
            Re: design question : struct or class

            > I've got a design question. I need to keep track of some variables and I
            am[color=blue]
            > 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[/color]
            depend[color=blue]
            > 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)[/color]

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

            --
            cody

            [Freeware, Games and Humor]
            www.deutronium.de.vu || www.deutronium.tk


            Comment

            • ozbear

              #7
              Re: design question : struct or class

              On Fri, 30 Jan 2004 00:56:36 +0100, "codymanix"
              <dont.spam.me.d eutronium@gmx.d e> wrote:
              [color=blue][color=green]
              >> I've got a design question. I need to keep track of some variables and I[/color]
              >am[color=green]
              >> 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[/color]
              >depend[color=green]
              >> 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)[/color]
              >
              >You should *Always* use classes. structs are only needed in very rare
              >circumatance s which isn't the case.
              >[/color]

              Rubbish....anyo ne saying "always" do or don't do X is practicing
              religion, not programming. Structs have their proper place in the
              programming paradigm, just as classes do. Knowing which to use and
              when defines the difference between a programmer and a zealot/ostrich.

              Oz

              Comment

              • Guest's Avatar

                #8
                Re: design question : struct or class

                Use them whatever best way suits you. Scew the fanboys.

                Same for any aspect of software. Do what works best for you.


                "ozbear" <ozbear2@yahoo. com> wrote in message
                news:401a31e1.1 431256578@news-server...[color=blue]
                > On Fri, 30 Jan 2004 00:56:36 +0100, "codymanix"
                > <dont.spam.me.d eutronium@gmx.d e> wrote:
                >[color=green][color=darkred]
                > >> I've got a design question. I need to keep track of some variables and[/color][/color][/color]
                I[color=blue][color=green]
                > >am[color=darkred]
                > >> planning to put them inside a class or struct. Basically I'm talking[/color][/color][/color]
                about[color=blue][color=green][color=darkred]
                > >> 10 bools, 20 ints and 2 arrays of ints. The size of the arrays would[/color]
                > >depend[color=darkred]
                > >> on some external value (going from 0 to around 1000 max). I would have[/color][/color][/color]
                an[color=blue][color=green][color=darkred]
                > >> array of max 255 of these classes/structs (in most cases it will be[/color][/color][/color]
                less[color=blue][color=green][color=darkred]
                > >> then 5 however)[/color]
                > >
                > >You should *Always* use classes. structs are only needed in very rare
                > >circumatance s which isn't the case.
                > >[/color]
                >
                > Rubbish....anyo ne saying "always" do or don't do X is practicing
                > religion, not programming. Structs have their proper place in the
                > programming paradigm, just as classes do. Knowing which to use and
                > when defines the difference between a programmer and a zealot/ostrich.
                >
                > Oz
                >[/color]


                Comment

                • peter x

                  #9
                  Re: design question : struct or class

                  Hi there,

                  The main difference is that a 'class' is an object type and a struct is a
                  'value' type. When it comes to assignment and parameter passing its
                  important to know the difference:


                  Assignment
                  -------------
                  myObject o1 = new myObject()
                  myObject o2 = new myObject()
                  o1 = o2

                  Objects are 'reference' types and are really pointers to an address in
                  memory where values are stored. In the above example space is allocated for
                  two myObject types. o1 points to the first myObject type and o2 points to
                  the second myObjects type. Because objects are reference types, its not the
                  values held in o2 that are copied into o1 but rather the address in memory
                  that is pointed too by o2. Consequently, 'o1 = o2' has the effect of
                  pointing both o1 and o2 at the same place in memory and changes to o1 will
                  change o2 and vice versa. If no references remain to the memory allocated
                  for the original o1 object it will eventually be garbage collected.

                  myStruct s1 = new myStruct()
                  myStruct s2 = new myStruct()
                  s1 = s2

                  Structs are 'value' types and are very much like ints or floats. In the
                  above example space is allocated for s1 and s2. s1 is the first myStruct
                  type and s2 is the second myStruct type. Because structs are value types,
                  its not the reference for s2 that is copied into s1 but the values.
                  Consequently, 's1 = s2' has the effect of maintaining two seperate areas in
                  memory with the same values. Sometimes this is what you want and sometimes
                  it isn't. For example, If your struct type contains a nested object it isn't
                  the values of the nested object that are copied but rather the reference. In
                  the case of 's1 = s2', you will have two seperate blocks of memory which
                  contain the same values but the object reference in both will point to the
                  same place in memory. To find out more about avoiding this problem look for
                  help and shallow vs deep copying.


                  Parameter Passing
                  ---------------------

                  When objects are passed to a method, they are passed by reference. This
                  means that any changes made to the object within the method will remain once
                  the method had been completed.

                  When structs are passed to a method, they are passed by value. This means
                  that the system makes a copy is made and changes made to the struct will not
                  remain when the method is complete. You can get round this by returning the
                  copied struct and assigning it to a variable but there is an obvious over
                  head in working with copies.


                  Summary
                  ----------

                  The choice of whether to use a class or a struct is entirely down to you,
                  both have there place. However, because of the overheads involved in passing
                  by value and the added complexity of nested objects, I uses classes most of
                  the time. I only use structs when the offer me exactly what i need. I hope
                  this helps and that my explanation was too confusing and if I am wrong on
                  any point my apologies in advance.


                  Happy coding, Peter




                  "codymanix" <dont.spam.me.d eutronium@gmx.d e> wrote in message
                  news:%23M3XdPs5 DHA.1948@TK2MSF TNGP12.phx.gbl. ..[color=blue][color=green]
                  > > I've got a design question. I need to keep track of some variables and I[/color]
                  > am[color=green]
                  > > planning to put them inside a class or struct. Basically I'm talking[/color][/color]
                  about[color=blue][color=green]
                  > > 10 bools, 20 ints and 2 arrays of ints. The size of the arrays would[/color]
                  > depend[color=green]
                  > > on some external value (going from 0 to around 1000 max). I would have[/color][/color]
                  an[color=blue][color=green]
                  > > array of max 255 of these classes/structs (in most cases it will be less
                  > > then 5 however)[/color]
                  >
                  > You should *Always* use classes. structs are only needed in very rare
                  > circumatances which isn't the case.
                  >
                  > --
                  > cody
                  >
                  > [Freeware, Games and Humor]
                  > www.deutronium.de.vu || www.deutronium.tk
                  >
                  >[/color]


                  Comment

                  • peter x

                    #10
                    Re: design question : struct or class

                    Hi there,

                    The main difference is that a 'class' is an object type and a struct is a
                    'value' type. When it comes to assignment and parameter passing its
                    important to know the difference:


                    Assignment
                    -------------
                    myObject o1 = new myObject()
                    myObject o2 = new myObject()
                    o1 = o2

                    Objects are 'reference' types and are really pointers to an address in
                    memory where values are stored. In the above example space is allocated for
                    two myObject types. o1 points to the first myObject type and o2 points to
                    the second myObjects type. Because objects are reference types, its not the
                    values held in o2 that are copied into o1 but rather the address in memory
                    that is pointed too by o2. Consequently, 'o1 = o2' has the effect of
                    pointing both o1 and o2 at the same place in memory and changes to o1 will
                    change o2 and vice versa. If no references remain to the memory allocated
                    for the original o1 object it will eventually be garbage collected.

                    myStruct s1 = new myStruct()
                    myStruct s2 = new myStruct()
                    s1 = s2

                    Structs are 'value' types and are very much like ints or floats. In the
                    above example space is allocated for s1 and s2. s1 is the first myStruct
                    type and s2 is the second myStruct type. Because structs are value types,
                    its not the reference for s2 that is copied into s1 but the values.
                    Consequently, 's1 = s2' has the effect of maintaining two seperate areas in
                    memory with the same values. Sometimes this is what you want and sometimes
                    it isn't. For example, If your struct type contains a nested object it isn't
                    the values of the nested object that are copied but rather the reference. In
                    the case of 's1 = s2', you will have two seperate blocks of memory which
                    contain the same values but the object reference in both will point to the
                    same place in memory. To find out more about avoiding this problem look for
                    help and shallow vs deep copying.


                    Parameter Passing
                    ---------------------

                    When objects are passed to a method, they are passed by reference. This
                    means that any changes made to the object within the method will remain once
                    the method had been completed.

                    When structs are passed to a method, they are passed by value. This means
                    that the system makes a copy is made and changes made to the struct will not
                    remain when the method is complete. You can get round this by returning the
                    copied struct and assigning it to a variable but there is an obvious over
                    head in working with copies.


                    Summary
                    ----------

                    The choice of whether to use a class or a struct is entirely down to you,
                    both have there place. However, because of the overheads involved in passing
                    by value and the added complexity of nested objects, I uses classes most of
                    the time. I only use structs when the offer me exactly what i need. I hope
                    this helps and that my explanation was too confusing and if I am wrong on
                    any point my apologies in advance.


                    Happy coding, Peter




                    "codymanix" <dont.spam.me.d eutronium@gmx.d e> wrote in message
                    news:%23M3XdPs5 DHA.1948@TK2MSF TNGP12.phx.gbl. ..[color=blue][color=green]
                    > > I've got a design question. I need to keep track of some variables and I[/color]
                    > am[color=green]
                    > > planning to put them inside a class or struct. Basically I'm talking[/color][/color]
                    about[color=blue][color=green]
                    > > 10 bools, 20 ints and 2 arrays of ints. The size of the arrays would[/color]
                    > depend[color=green]
                    > > on some external value (going from 0 to around 1000 max). I would have[/color][/color]
                    an[color=blue][color=green]
                    > > array of max 255 of these classes/structs (in most cases it will be less
                    > > then 5 however)[/color]
                    >
                    > You should *Always* use classes. structs are only needed in very rare
                    > circumatances which isn't the case.
                    >
                    > --
                    > cody
                    >
                    > [Freeware, Games and Humor]
                    > www.deutronium.de.vu || www.deutronium.tk
                    >
                    >[/color]


                    Comment

                    • rhavaldar at shodaka dot net

                      #11
                      Re: design question : struct or class

                      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 -


                      - raghu


                      codymanix wrote:
                      [color=blue][color=green]
                      >>I've got a design question. I need to keep track of some variables and I[/color]
                      >
                      > am
                      >[color=green]
                      >>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[/color]
                      >
                      > depend
                      >[color=green]
                      >>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)[/color]
                      >
                      >
                      > You should *Always* use classes. structs are only needed in very rare
                      > circumatances which isn't the case.
                      >[/color]


                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: design question : struct or class

                        peter x <lad4bear@boltb lue.com> wrote:

                        <snip>
                        [color=blue]
                        > When objects are passed to a method, they are passed by reference.[/color]

                        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 - <skeet@pobox.co m>
                        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                        If replying to the group, please do not mail me too

                        Comment

                        • peter x

                          #13
                          Re: design question : struct or class

                          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]" <skeet@pobox.co m> wrote in message
                          news:MPG.1a85aa 6c3b3ea02598a02 1@msnews.micros oft.com...[color=blue]
                          > peter x <lad4bear@boltb lue.com> wrote:
                          >
                          > <snip>
                          >[color=green]
                          > > When objects are passed to a method, they are passed by reference.[/color]
                          >
                          > 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 - <skeet@pobox.co m>
                          > http://www.pobox.com/~skeet
                          > If replying to the group, please do not mail me too[/color]


                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: design question : struct or class

                            peter x <lad4bear@boltb lue.com> wrote:[color=blue]
                            > I stand corrected :) Although I'm not sure I totally understand your
                            > response. Can you send me an example to help me clarify.[/color]

                            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.
                            [color=blue]
                            > Apart from this point was the rest of the assessment correct?[/color]

                            As far as I saw, yes - I only skimmed it though. (The usual terminology
                            is reference type rather than object type though.)
                            [color=blue]
                            > specifically the point about
                            > the overhead involved in passing by value. I may need to reassess how I
                            > program[/color]

                            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 - <skeet@pobox.co m>
                            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                            If replying to the group, please do not mail me too

                            Comment

                            • Ravichandran J.V.

                              #15
                              Re: design question : struct or class

                              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!

                              Comment

                              Working...