Assign direct value to object

I have several properties, for example

Public Property FIRSTNAME As New SQLString("FirstName", 50)
Public Property FULLNAME As New SQLString("Name", 50)

      

The SQLString object is defined as:

Public Class SQLString
    Property SQL_Column As String
    Property Limit As Integer
    Property Value As String

    Public Sub New(SQLcolumn As String, limit_ As Integer)
        SQL_Column = SQLcolumn
        Limit = limit_
    End Sub

    Public ReadOnly Property SQL_value() As String
        Get
            Return "'" & clean(Value, Limit) & "'"
        End Get
    End Property
End Class

      

Note that with this method each of my properties (for example FIRSTNAME

) can have several additional properties as needed.

For access to this simple example FIRSTNAME.SQL_Column

.

This works, however I would also like to store the value (like a string datatype) in a property FIRSTNAME

that will make it accessible like so:

Dim MyFirstName As String = FIRSTNAME

      

Instead

Dim MyFirstName As String = FIRSTNAME.Value

      

This is what I have to do now. The only way I can do this is to set the default SQLString object to a string (or other data type), like so:

Public Class SQLString As String

      

Obviously the above code doesn't work, but I'm wondering if there is something like this?

+3


source to share


2 answers


The default property access modifier (ex: Public, Private, etc.) is most restrictive if no access modifier is provided. In the SQLString class, since there is no access modifier in front of the class properties Public

, they are essentially Private

not accessible from outside the class.

Adding a property access modifier should fix the problem you are seeing:

Public Property SQL_Column As String
Public Property Limit As Integer
Public Property Value As String

      

Please tell us about the issues for voting runs - here is a working script for the suggested code changes above ( https://dotnetfiddle.net/96o8qm ).



Imports System
Dim p as Person = new Person()
p.FIRSTNAME = new SQLString("Test", 1)
p.FIRSTNAME.Value = "Test Value"
Console.WriteLine("Person Value: {0}", p.FIRSTNAME.Value)

Public Class Person
    Public Property FIRSTNAME AS SQLString
End Class

Public Class SQLString
    Public Property SQL_Column As String
    Public Property Limit As Integer
    Public Property Value As String

    Public Sub New(SQLcolumn As String, limit_ As Integer)
        SQL_Column = SQLcolumn
        Limit = limit_
    End Sub

    Public ReadOnly Property SQL_value() As String
        Get
            Return ""
        End Get
    End Property

End Class

      

This gives the result:

Person Value: Test Value

      

+2


source


The answer to your question is pretty simple; add the CType expansion operator .

Example:

Public Class SQLString

    Public Shared Widening Operator CType(ByVal s As SQLString) As String
        Return If((s Is Nothing), Nothing, s.Value)
    End Operator

    Public Property Value As String

End Class

      

Test:



Dim firstName As New SQLString() With {.Value = "Bjørn"}
Dim myName As String = firstName

Debug.WriteLine(myName)

      

Exit (immediate window):

Bjorn

+1


source







All Articles