Results 1 to 7 of 7

Thread: Value Types and Classes [resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Value Types and Classes [resolved]

    What is the difference between these two? From the definition of a Value Type

    Value types are stored as primitive data types, but they can contain fields, properties, events, and both static and nonstatic methods. Value types do not carry the overhead of an object that is being held in memory.
    To me, it seems like a Value Type is a class. What then, is the difference? I'd like a better explanation than the one I've found here.
    Last edited by mendhak; Oct 29th, 2003 at 04:09 AM.

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    I think reference types are stores on the heap.... bache!
    I just give you an example. this is the msot important thing you should care about, figure out the details on your own


    Dim A,B as myClass
    B = new myClass()
    A=B

    ' if you change A, B will be changed also. If you change B, A will be changed also, cuz myClass is a class and class is a reference type!


    Dim A, B as myStruct
    A=B
    B.foo = "blah"

    ' A.foo hasn't changed. Structures are value types


    uuh and classes are reference types. Most things are reference types, besides the primative data types and some other things

    remember one thing though, if you have an array of value types (ie, an array of ints), the array itself is a reference type.

    dim arr() as integer
    arr is a reference type
    arr(0) is a value type
    Last edited by MrPolite; Oct 28th, 2003 at 02:11 AM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Originally posted by MrPolite
    Most things are reference types, besides the primative data types and some other things
    No, among primitive data types only String is a Reference type, all others are Value Types and 'Object' can act as both.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Types can be divided into two types:
    • Reference
    • Value


    The main difference between the two is where they are allocated in memory. Value types are stored in an application specific location of memory, called the stack. Reference types on the other hand are stored on the managed heap. The CLR, specifically the garbage collector, monitors the heap for objects that are no longer reference, and releases their memory and de-allocated the memory when necessary.

    All objects (including value types) ultimately derive from System.Object. When a value type needs to act like an object, boxing occurs. Boxing allows a value type to be treated as a reference type The quote you posted is describing a structure, which is in fact a value type (somewhat of a lightweight class).
    However, unlike reference types, you can not derive a class from a value type, but you can implement one or more interfaces.

  5. #5
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    To summarise...

    Value types:

    --are allocated on the stack
    --are copied when passed as arguments
    --cannot be inherited

    Reference type:

    --allocated on the stack
    --can be inherited (unless marked as sealed)
    --only addresses are passed as args, not the value

    like Lunatic3 says, ultimately 'object' can act as both but there is a boxing overhead in switching between the 2.

    Cheers...

  6. #6
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    beat me to the punch Lethal...

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Thanks for the explanation guys. I've understood.


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