Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to get one class accessing another class' properties?

Posted on 2006-05-11
7
Medium Priority
?
212 Views
Last Modified: 2010-08-05
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.  (#_#)
0
Comment
Question by:Flying-Kiwi
7 Comments
 
LVL 12

Expert Comment

by:Proactivation
ID: 16656628
Try:

Dim person As Person = New Person
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 200 total points
ID: 16656690
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16657419
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Flying-Kiwi
ID: 16657421
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
 

Author Comment

by:Flying-Kiwi
ID: 16657500
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
 
LVL 12

Expert Comment

by:Proactivation
ID: 16657569
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
 

Author Comment

by:Flying-Kiwi
ID: 16657627
Thx!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question