Results 1 to 4 of 4

Thread: Clarification on Something

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    102

    Clarification on Something

    Lo all, just a small clarification on something in OOP.

    If I had a class called characters and done this in another class

    VB Code:
    1. Public Members() as Character
    2.  
    3. Public Sub SetMembers(a as Character)
    4.      Members(0) = a
    5. end sub

    Then Members(0) would hold a reference to the character ??

    Where if I done this

    VB Code:
    1. Public Members() as New Character
    2.  
    3. Public Sub Set Members(a as Character)
    4.       Members(0) = a
    5. end Sub]

    Then Members would hold a reference to a charcter but a new instance which just holds same data as 'a'.??

    Is this correct?

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

    Re: Clarification on Something

    The first one wouldn't work because you never instantiate (read: create) the array and the second wouldn't work because it is invalid syntax. To declare and instantiate an array in one line you would use either of these two sytaxes:
    VB Code:
    1. Private arr1(upperBound) As Object
    2. Private arr2() As Object = New Object(upperBound) {}
    You can then initialise the elements in the second case if you like also. If you declare an arry without specifying an upper bound then you have created a variable that can refer to an array but you have not created an array object. You have to, at some point, specify the upper bound so that the elements are created and you can then assign objects to them.
    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
    Lively Member
    Join Date
    Jul 2004
    Posts
    102

    Re: Clarification on Something

    Sorry I dodnt include the array bits in full for speed lol. If they werent arrays would it hold the refernce for an existing object which I could then get info from??

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

    Re: Clarification on Something

    If you assign an instance of a class to a variable then that variable contains a reference to the object. if you assign the same instance to another variable then both variables refer to the same object. If you do the same thing with an instance of a structure, then each variable will contain a copy of the original object. With structures, e.g Integers, Booleans, Chars, etc. the variable contains the object itself, so every variable is a distinct object. With classes the variable contains a reference to the object, i.e. its address in memory, so a number of variables can all refer to the same object. I hope that answers your question becasue I'm afraid I'm not 100% sure what you're asking. Note that arrays are instances of the Array class, so a number of array variables can refer to the same object. The elements of an array can be classes or structures, so the aforementioned rules apply in each case.
    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

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