Results 1 to 3 of 3

Thread: Classes VS Structures

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Classes VS Structures

    Hello,
    I know that Structures are value types and classes are reference type, i know that since structures are stored in stack they are faster to use i also understands that structures can not use the Inheritance concept

    but still in most examples i see i always see that the authors use classes when building something like BBL library, if structures is light-weight compare to classes and i don't need it to be inherited should i use structure instead of class?

    is there a rule of when to use class and when to use structure ?

    Thanks.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Classes VS Structures

    The vast majority of the types you create should be classes. Structures are faster to access because they are stored on the stack, but that also means that any time you assign the value of a variable containing a structure to another variable, you copy the entire object.

    As a rule of thumb, value types should never be more than 16 bytes in size. A standard reference type variable is 4 bytes. An Integer is 4 bytes. A Long is 8 bytes. As such, if you were going to define a type with three fields: one a reference type (that includes String), one an Integer and one a Long, that would be as big as a structure should be. If you wanted to add another field then you should consider a class rather than a structure.

    Structures should only be used to represent very simple type. In many cases, a structure contains just a single field and then the properties and method of the type interpret that one value. A classic example is the DateTime structure. Internally, its just a single number that occupies 8 bytes:
    Quote Originally Posted by MSDN
    Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar. For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.
    Generally, assume that new types that you create will be classes unless you have some specific reason for making them structures.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Classes VS Structures

    Ok thanks for the info JM.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width