Flying-Kiwi
asked on
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. (#_#)
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. (#_#)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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. :--)
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. :--)
ASKER
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!
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!
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.
ASKER
Thx!
Dim person As Person = New Person