Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
This seems like it should be simple. I want to create a class (more like a datatype, number or string, really) with a Set... End Set block but without properties.

For example, if I start with "Dim x As New SampleClass", instead of having to say "x.value = ..." I'd rather say "x = ..." or "MsgBox(x)". But I want something to happen each time a SampleClass is set, i.e. "x = ..."

I've looked at examples of classes and structures and I've looked up how to define a new data type but none of the examples were like what I want to do. I know I could just say "x.value = ..." and MsgBox(x.value) but I'd rather not.
Posted

This is unrelated to properties, but you can define conversion operators: https://msdn.microsoft.com/en-us/library/yf7b9sy7.aspx[^],
https://msdn.microsoft.com/en-us/library/hz3wbx3x.aspx[^],
https://msdn.microsoft.com/en-us/library/4w127ed2.aspx[^].

The notable feature of widening operators described above is that you don't have to use CType where you use the conversion, so the conversion is syntactically implicit, which is the key for the syntax you want to achieve.

—SA
 
Share this answer
 
v2
You'd normally use a Structure to do this. Something like this:
VB
<debuggerdisplay("{m_value}")>
Public Structure CustomValueType

    Private m_Value As Integer

    Public Shared ReadOnly MinValue = New CustomValueType With {.m_Value = -100}
    Public Shared ReadOnly MaxValue = New CustomValueType With {.m_Value = 100}

    Private Sub New(ByVal value As Integer)
        If value < CustomValueType.MinValue Then
            Throw New ArgumentOutOfRangeException("value", "Value cannot be less than CustomValueType.MinValue.")
        End If

        If value > CustomValueType.MaxValue Then
            Throw New ArgumentOutOfRangeException("value", "Value cannot be greater than CustomValueType.MaxValue.")
        End If

        m_Value = value
    End Sub

    Public Shared Widening Operator CType(ByVal value As CustomValueType) As Integer
        Return value.m_Value
    End Operator

    Public Shared Narrowing Operator CType(ByVal value As Integer) As CustomValueType
        Return New CustomValueType(value)
    End Operator

    Public Shared Operator <(ByVal x As CustomValueType, ByVal y As CustomValueType) As Boolean
        Return x.m_Value < y.m_Value
    End Operator

    Public Shared Operator >(ByVal x As CustomValueType, ByVal y As CustomValueType) As Boolean
        Return x.m_Value > y.m_Value
    End Operator

    Public Shared Operator <(ByVal x As Integer, ByVal y As CustomValueType) As Boolean
        Return x < y.m_Value
    End Operator

    Public Shared Operator >(ByVal x As Integer, ByVal y As CustomValueType) As Boolean
        Return x > y.m_Value
    End Operator

End Structure

It's used just like you want:
VB
Sub Main()

    Dim x As CustomValueType

    ' Works just fine...
    x = 31

    ' Will explode due to the limits imposed by the types New method...
    x = 110

End Sub

The problem with creating your own value type like this is that you have to define a ton of Operators for your type. Notice, my example is only implementing implicit conversions between an Integer and the CustomValueType and the < and > operators. You still have to define =, <>, >=, <=, +, -, *, /, and some other conversions, but only if they make sense for your implementation.
 
Share this answer
 
In C# overloading the assignment operator is done using the 'Implicit operator syntax:
C#
public class DemoImplicitClass
{
    public string SomeData;

    public DemoImplicitClass(string somedata = null)
    {
        SomeData = somedata;
    }

    public static implicit operator DemoImplicitClass(string somedata)
    {
        return new DemoImplicitClass(somedata);
    }
}

// sample test in some method or EventHandler:
DemoImplicitClass demo1 = "hello";

Console.WriteLine(demo1.SomeData);
I ran this C# code through two on-line (free) C#->VB converters: Telerik's converter [^] gave this output:
VB
Public Class DemoImplicitClass
    Public SomeData As String
    
    Public Sub New(Optional somedata__1 As String = Nothing)
    	SomeData = somedata__1
    End Sub
    
    Public Shared Widening Operator CType(somedata As String) As DemoImplicitClass
    	Return New DemoImplicitClass(somedata)
    End Operator
End Class
DeveloperFusion's converter choked on this input and never finished the conversion.

I don't grok VB.Net, but there is what appears to be a good discussion of Widening and Narrowing conversions in VB here: [^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900