Results 1 to 5 of 5

Thread: Structures are a bad thing?

  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

    Structures are a bad thing?

    A book I was reading said this:

    Structures are frowned upon in object-oriented programming. A class can do everything that a Structure can do—and more—so you should use classes instead of Structures.
    Why does it say that structures are frowned upon?

    IIRC, we can use property let/get to validate the values that we pass to the structure, so it shouldn't be that, should it?

    Is it wrong to assume that creating a class for the sake of a simple structure-type implementation is a waste?

    Please explain in -terms.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I have your same doubt, but I know at least a little difference between them. Class is a reference type, Structure is a value type. It makes a difference when you pass them Byval to a sub. In case of Class it will be affected anyway.
    Live long and prosper (Mr. Spock)

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    A structure is basically just a collection of properties grouped together.
    A class on the other hand is an entirely integral piece of executable code with its own methods, events and properties.

    The class is the foundation of object orientated programming.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: Structures are a bad thing?

    dead post alert...

    Quote Originally Posted by plenderj
    A structure is basically just a collection of properties grouped together.
    A class on the other hand is an entirely integral piece of executable code with its own methods, events and properties.

    The class is the foundation of object orientated programming.

    I had the same question and did a search for the topic before posting... something i'm not clear on, structures can have it's own methods, events, and values as well, correct? The IDE doesn't mind when I add Public Sub Foo() to the structure...

    My app will be sending data back and fourth through the internet using serialized objects. Might it be more efficient to just use structures instead since none of the objects being sent use inheritance at all (except from Object of course)?

  5. #5

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

    Re: Structures are a bad thing?

    You would be able to, most likely, but it would be inefficient I suppose. The struct is value type, class is reference type, so the object would be taken care of by the GC.

    Besides, with a structre, you can't do things like Inheriting and I don't believe there is a way to control how the struct is "instantiated" if I may use that term.

    It's best to use a struct only as your own data type for your functions or summit.

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