Class Property Access

aaron_work

New member
Joined
May 7, 2010
Messages
1
Programming Experience
Beginner
Hopefully there is a quick answer to this but I can't seem to find the correct words to search for an answer. I'm using VS 2010 so I have auto-implemented properties.

I have a class and I want to access the properties by a string.

Public Class MyClass
Property Prop1 As String
Property Prop2 As String
End Class

OK, Of course I can set/get the properties by doing this...

Dim MyVar = New MyClass
MyVar.Prop1 = "Prop1 Value"
MyVar.Prop2 = "Prop2 Value"

But, I also want to be able to access the variables something like this way....

Dim MyVar = New MyClass
MyVar.Items("Prop1") = "Prop1 Value"
MyVar.Items("Prop2") = "Prop2 Vaule"

How can I setup MyClass to work both ways?

Thanks,
Aaron
 
I believe this is what your after:


Inside Cheese.cs
VB.NET:
Public Class Cheese

    Private mycheese As String
    Public Property Type() As String
        Get
            Return mycheese
        End Get
        Set(ByVal value As String)
            mycheese = value
        End Set
    End Property
End Class

Inside Sandwhich.cs
VB.NET:
Public Class Sandwhich
    Dim cheese As Cheese

    Public Function Main()
        cheese = New Cheese()
        cheese.Type = "Mozarella"
    End Function


End Class
 
Back
Top