Jasir
Jasir

Reputation: 697

Is there any function in vb.net as an alternative to magic methods __get() and __set() in php

PHP has magic methods like __get() and __set(). Is there any alternative for it in vb.net. Or is there any other tricks that can be done instead?

This was what i done in php

private $data = array();

public function __construct($arData) {
    $data['var1'] = 'value1';
    $data['var2'] = 'value2';
    $data['var3'] = 'value3';
}

public function __get($propertyName) {
    if(!array_key_exists($propertyName, $this->data))
        throw new Exception("Invalid property");
    else
        return $this->data[$propertyName];
}

Now i am able to access property that actually doesn't exist like $obj->var1, $obj->var2 and $obj->var3

Can this be done in VB .Net?

Upvotes: 1

Views: 594

Answers (2)

user2480047
user2480047

Reputation:

You can use Get/Set or even nothing. Sample code:

Public Class testClass
    Private _val As String

    Public Property myProp1 As String
        Get
            Return _val
        End Get
        Set(ByVal value As String)
            _val = value
        End Set
    End Property

    Public Property myProp2 As String
End Class

You can access these properties by doing:

Dim test As testClass = New testClass()
test.myProp1 = "val1"
test.myProp2 = "val2"

As you can see, myProp1 has the Get/Set structure; my myProp2 is declared like a variable. Practically speaking, both alternatives deliver the same.

UPDATE

You can emulate the PHP behaviour in your post via System.Reflection:

Public Function _get(propertyName As String, curInstance As testClass) As Object

    Dim curProp As System.Reflection.PropertyInfo = curInstance.GetType().GetProperty(propertyName)
    Dim outVal As Object = Nothing
    If (curProp IsNot Nothing) Then
        outVal = curProp.GetValue(curInstance, Nothing)
    Else
        'NOT FOUND
    End If

    Return outVal
End Function

Public Sub _set(propertyName As String, curInstance As testClass, newVal As Object) 

    Dim curProp As System.Reflection.PropertyInfo = curInstance.GetType().GetProperty(propertyName)
    If (curProp IsNot Nothing) Then
        curProp.SetValue(curInstance, newVal, Nothing)
    Else
        'NOT FOUND
    End If

End Sub

What can be called:

Dim outval As String = DirectCast(_get("myProp1", test), String)
_set("myProp2", test, "val3")

Upvotes: 2

Yuriy Galanter
Yuriy Galanter

Reputation: 39777

You can implement a private dictionary to keep Property Name/Values pairs. Something like:

Public Class myObj

    Private m_aProperties As Dictionary(Of String, String)

    Public Sub New()
        m_aProperties = New (Of String, String)
    End Sub

    Public Function __Get(i_sPropName As String) As String

        If m_aProperties.ContainsKey(i_sPropName) Then
            Return m_aProperties(i_sPropName)
        Else
            Return String.Empty
        End If

    End Function

    Public Sub __Set(i_sPropName As String, i_sPropValue As String)

        If m_aProperties.ContainsKey(i_sPropName) Then
            m_aProperties(i_sPropName) = i_sPropValue
        Else
            m_aProperties.Add(i_sPropName, i_sPropValue)
        End If

    End Sub
End Class

It would create a new entry in dictionary if the property doesn't exist. It will return an empty string if you attempt to return a non-existing property. Example of usage:

    Dim myObj1 As New myObj

    myObj1.__Set("aaa", "123")
    Dim s As String = myObj1.__Get("aaa")

This can be expanded to work for different data types, to handle non-existing properties differently etc. But the basic concept to create property at runtime is to keep it in a dictonary.

Upvotes: 3

Related Questions