classes and structures

Posted on 2011-05-04
Last Modified: 2012-05-11
What is the difference between a structure and a class and when would you use on or the other.

An example of where you would use the structure rather then a class would be great
Question by:runnerjp2005
    LVL 83

    Expert Comment

    A structure is usually used to hold Data where as a class can include the functionality.
    LVL 83

    Expert Comment

    A structure is usually used where multiple values need to be passed around, such as a return value of a function.
    LVL 83

    Expert Comment


    Author Comment

    so is one a reference to data and the other is data itself.

    I also though structures and classes can both hold vunctions.

    ones by ref and other is by val ?
    LVL 16

    Expert Comment

    by:Imran Javed Zia
    Th main points in class vs structure are:
    Pass by Reference and Pass by Value

    Please go throught the following links for more details:
    LVL 12

    Accepted Solution

    Structs differ from classes in several important ways:

    - Structs are value types.
    - All struct types implicitly inherit from the class System.ValueType.
    - Assignment to a variable of a struct type creates a copy of the value being assigned.
    - The default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null.
    - Boxing and unboxing operations are used to convert between a struct type and object.
    - The meaning of this is different for structs.
    - Instance field declarations for a struct are not permitted to include variable initializers.
    - A struct is not permitted to declare a parameterless instance constructor.
    - A struct is not permitted to declare a destructor.
    LVL 12

    Assisted Solution

    by:Mohamed Abowarda
    Structures are always better to declare a group of related variable, for example:
        Public Structure Person
            Public FirstName As String
            Public LastName As String
            Public Occupation As String
        End Structure

    Open in new window

    While it's better to use classes when you have methods, events, properties, etc...
        Public Class ImageProcessor
            ' Variables
            Private image As Bitmap
            ' Events
            Public Event ProcessCompleted(ByVal e As EventArgs)
            Public Event ProcessError(ByVal e As EventArgs)
            ' Methods
            Public Sub ImageProcessor()
            End Sub
            Public Sub Start()
            End Sub
        End Class

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now