[VB.Net] Help with class

MatP

Member
Joined
Oct 12, 2004
Messages
11
Ok, Im not enough familiar with class for doing what Im looking for.

I have two classes, keeping it simple, say Class1 and Class2.

Now, Class2 contain a property in it that is class1 (Class2 is not a collection of class1) :

Lets keep it simple, here is the Class1 code:
Code:

Code:
Public Class Class1
  Private _Prop1 as String
  Public Property PropClass1()
        Get
            Return _Prop1
        End Get
        Set(ByVal Value)
            _Prop1 = Value
        End Set
End Class


Now for the Class2 Code :
Code:

Code:
Public Class Class2
  Private _Prop2 as Class1 *******
  Public Property PropClass2()
        Get
            Return _Prop2
        End Get
        Set(ByVal Value)
            _Prop2 = Value
        End Set
End Class


What do I need to change in class1 or class2 to make the PropClass1 accessible from a Class2 Object ?

What I would like is to be able to do:
Code:

Code:
Dim myClass as Class2
Class2.PropClass2.PropClass1 = "I changed the Property of the PropClass1 of the _Prop2 Object in Class 2, Yeah"


Actually, with the code above, Class2.PropClass2 is accessible, but not the ".PropClass1" part.

What I need to do for now is:
Code:

Code:
Dim myClass1 as Class1
Dim myClass2 as Class2
myClass1.PropClass1 = "blabalblaa"
myClass2.PropClass2 = myClass1


That way it works, but I do not want this.

In other words, I would like PropClass2 of Class2 to have sub-properties that are the same as the class1 properties, since PropClass2 is a Class1.

Hope it was understandable...
 
I notice you are not creating a valid instance of Class1 within Class2 - try making the following change to your code and see if that fixes the problem
Code:
Public Class Class2
    Private _Prop2 As New Class1 *******
    Public Property PropClass2()
        Get
            Return _Prop2
        End Get
        Set(ByVal Value)
            _Prop2 = Value
        End Set
    End Property
End Class
 
PlausiblyDamp said:
I notice you are not creating a valid instance of Class1 within Class2 - try making the following change to your code and see if that fixes the problem
Code:
Public Class Class2
    Private _Prop2 As New Class1 *******
    Public Property PropClass2()
        Get
            Return _Prop2
        End Get
        Set(ByVal Value)
            _Prop2 = Value
        End Set
    End Property
End Class

It doesnt work either. Well, is it that bad if instead of declaring _Prop2 as Private, I declare it as Public and that way I can access its properties, but it wont be anymore a property of Class2, but an object that I can access outside of the class.
 
Are you wanting to access an instance of class2 or just the class itself?

i.e.
as it stands
Code:
Dim aClass As New Class2
aClass.PropClass2.PropClass1 = "I changed the Property of the PropClass1 of the _Prop2 Object in Class 2, Yeah"

will work.

If you really want to go through the class name then you will need to declare both the property and _Prop 2 as being shared (I doubt that is what you want from you code though).

s
 
PlausiblyDamp said:
Are you wanting to access an instance of class2 or just the class itself?

i.e.
as it stands
Code:
Dim aClass As New Class2
aClass.PropClass2.PropClass1 = "I changed the Property of the PropClass1 of the _Prop2 Object in Class 2, Yeah"

will work.

If you really want to go through the class name then you will need to declare both the property and _Prop 2 as being shared (I doubt that is what you want from you code though).

s

But if I consider the autoComplete options, once I wrote the second dot, the only thing that is accessible after PropClass2, its GetType. So I guess that maybe I can access the properties of Class1, but it will be in conflict with Option Strict.
 
Code:
Public Class Class2
    Private _Prop2 As New Class1 *******
    Public Property PropClass2() As Class1
        Get
            Return _Prop2
        End Get
        Set(ByVal Value As Class1)
            _Prop2 = Value
        End Set
    End Property
End Class
 
PlausiblyDamp said:
Code:
Public Class Class2
    Private _Prop2 As New Class1 *******
    Public Property PropClass2() As Class1
        Get
            Return _Prop2
        End Get
        Set(ByVal Value As Class1)
            _Prop2 = Value
        End Set
    End Property
End Class

Oops.. yes I forgot to put the type at the property.
 
Back
Top