Results 1 to 4 of 4

Thread: How to get one class accessing another class' properties?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    252

    Question How to get one class accessing another class' properties?

    Hi,

    I must be overlooking something really simple.

    SITUATION
    A form Application, but this is about the two added Class Modules: Class1.vb and Person.vb

    Person.vb
    --------------
    VB Code:
    1. Public Class Person
    2.     Public Name As String
    3.     Public Age As Integer
    4. End Class



    Class1.vb
    -------------
    VB Code:
    1. Public Class Class1
    2.     Dim person As New Person
    3.     person.Name = "Bob"        <---- Name doesn't show up in Intelisense and this gives a "Declaration expected" error.
    4.  
    5. End Class


    PS
    * They are in the same (root) namespace.
    * I know the above Class design is *crappo*. It's just put together as a simple illustration of my headache, I mean problem. (#_#)

  2. #2
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    Re: How to get one class accessing another class' properties?

    I have declared Public variabes in classes before and didn't need to reference the class that it belongs to, to use it. Just try using:

    VB Code:
    1. Public Class Class1
    2.     Dim person As New Person
    3.     Name = "Bob"        <---- Name doesn't show up in Intelisense and this gives a "Declaration expected" error.
    4.  
    5. End Class

    Hope that works!
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

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

    Re: How to get one class accessing another class' properties?

    You cannot assign to property of a member variable outside a method.
    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
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Arrow Re: How to get one class accessing another class' properties?

    Add a constructor to the class pewrson that takes a name and use that e.g.
    VB Code:
    1. Public Class Person
    2.     Public Name As String
    3.     Public Age As Integer
    4.  
    5.     Public Sub New(byval NameIn as String)
    6.        Name = NameIn
    7.     End Sub
    8. End Class
    9.  
    10. Public Class Class1
    11.     Dim person As New Person("Bob")
    12. End Class

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