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
--------------
Public Class Person
    Public Name As String
    Public Age As Integer
End Class


Class1.vb
-------------
Public Class Class1
    Dim person As New Person
    person.Name = "Bob"        <---- Name doesn't show up in Intelisense and this gives a "Declaration expected" error.

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.  (#_#)
Flying-KiwiAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
Its complaining because you are trying to assing a value outside of a method:

Person.vb
--------------
Public Class Person
    Public Name As String
    Public Age As Integer
End Class


Class1.vb
-------------
Public Class Class1
    Dim person As New Person

    Public Sub New()
        person.Name = "Bob"       '// Inside constructor, so is now ok
    End Sub
End Class
0
 
ProactivationCommented:
Try:

Dim person As Person = New Person
0
 
Arthur_WoodCommented:
carl_tawn>>  this:

Public Class Class1
    Dim person As New Person

    Public Sub New()
        person.Name = "Bob"       '// Inside constructor, so is now ok
    End Sub
End Class

declares the variable perso, but DOES NOT create an instance.  change it like this:

Public Class Class1
    Dim person As Person = New Person

    Public Sub New()
        person.Name = "Bob"       '// Inside constructor, so is now ok
    End Sub
End Class

AW
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Flying-KiwiAuthor Commented:
Thx guys. Much appreciated.

Proactivation: Actually, it didn't make any difference, but thx for trying.  :--)

Carl: That's it! Outside of a method. Thank *^*( that prob's behind me.  :--)  

0
 
Flying-KiwiAuthor Commented:
Hi Arthur,

We posted at the same time.

I was going to add a question about the difference between the two, so I'm glad you brought it up. I tried it the 'short' way and it seemed to work okay (e.g. the assigned value of "Bob" was output to the Output window).

So what does the 'long' declaration do that the 'short' one doesn't?

I'd really like to clear this one up!
0
 
ProactivationCommented:
The long declaration declares your variable as a particular object type, then instatiates it with an object of that type.  VB lets you get away with the shorter method by hiding that functionality away and handling itself, which you can't always do with C#, etc.
0
 
Flying-KiwiAuthor Commented:
Thx!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.