Results 1 to 4 of 4

Thread: [2.0] Need to retrieve Private Array from another class

  1. #1

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60

    [2.0] Need to retrieve Private Array from another class

    I have two classes

    Class 1 has a List of objects that's PRIVATE. I currently have two functions to add and delete from the list. I need a way to retrieve all the objects in the list.. either by using a function to RETURN the list so I can access it on Class 2, or some other magical way. Hopefuly there's a way to do it without making the list public?

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

    Re: [2.0] Need to retrieve Private Array from another class

    The standard way would be to use a Private variable and a Public ReadOnly property. That's how just about every collection property in the Framework is structured. That way you can get a reference to the collection from outside so you can get the items and modify the collection but you cannot assign a whole new collection in its place:
    VB Code:
    1. Private myField As ArrayList
    2.  
    3. Public ReadOnly Property MyProperty() As ArrayList
    4.     Get
    5.         If Me.myField Is Nothing Then
    6.             Me.myField = New ArrayList
    7.         End If
    8.  
    9.         Return Me.myField
    10.     End Get
    11. End Property
    If you don't want the caller to be able to modify the collection then you should return an array containing the items instead of a reference to the collection:
    VB Code:
    1. Private myField As ArrayList
    2.  
    3. Public ReadOnly Property MyProperty() As Object()
    4.     Get
    5.         If Me.myField Is Nothing Then
    6.             Me.myField = New ArrayList
    7.         End If
    8.  
    9.         Return Me.myField.ToArray()
    10.     End Get
    11. End Property
    Note that this creates a completely new array object each time the property is accessed so it should be done as infrequently as possible.
    Last edited by jmcilhinney; Aug 10th, 2006 at 02:18 AM.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Need to retrieve Private Array from another class

    Ooops. Sprecken zie VB?
    Code:
    private ArrayList myField;
    
    public ArrayList MyProperty
    {
        get
        {
            if (this.myField == null)
            {
                this.myField = new ArrayList();
            }
    
            return this.myField;
        }
    }
    Code:
    private ArrayList myField;
    
    public object[] MyProperty
    {
        get
        {
            if (this.myField == null)
            {
                this.myField = new ArrayList();
            }
    
            return this.myField.ToArray();
        }
    }
    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

  4. #4

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60

    Re: [2.0] Need to retrieve Private Array from another class

    Just what i needed, thanks a lot!

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