没有属性的VB.NET类 [英] VB.NET Class without properties

查看:70
本文介绍了没有属性的VB.NET类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该很简单。我想用Set ... End Set块创建一个类(更像数据类型,数字或字符串,但没有属性。



例如,如果我从Dim x As New SampleClass开始,而不是说x.value = ...我宁愿说x = ...或MsgBox(x)。但是每次设置SampleClass时我都希望发生一些事情,即x = ......



我看过类和结构的例子而且我'我们查找了如何定义新数据类型,但没有一个例子像我想做的那样。我知道我可以说x.value = ...和MsgBox(x.value),但我宁愿不这样做。

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.

推荐答案

这与属性,但您可以定义转换运算符 https://msdn.microsoft。 com / zh-CN / library / yf7b9sy7.aspx [ ^ ],

https:// msdn。 microsoft.com/en-us/library/hz3wbx3x.aspx [ ^ ],

https:// msdn.microsoft.com/en-us/library/4w127ed2.aspx [ ^ ]。



加宽运营商的显着特征上面描述的是你不必使用 CType 你在哪里使用转换,所以转换是语法隐含的,这是你想要的语法的关键实现。



-SA
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


您通常使用Structure来执行此操作。这样的事情:

You'd normally use a Structure to do this. Something like this:
<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:

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



像这样创建自己的值类型的问题是你必须为你的类型定义大量的运算符。请注意,我的示例仅实现了Integer与CustomValueType和<之间的隐式转换。和>运营商。您仍然需要定义=,<>,> =,< =,+, - ,*,/和其他一些转换,但前提是它们对您的实现有意义。


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.

在C#重载中,赋值运算符使用'隐式运算符语法:
In C# overloading the assignment operator is done using the 'Implicit operator syntax:
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);

我通过两个在线(免费)C# - > VB转换器运行此C#代码:Telerik的转换器[ ^ ]给出了此输出:

I ran this C# code through two on-line (free) C#->VB converters: Telerik's converter [^] gave this output:

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的转换器在这个输入上被阻塞,从未完成转换。



我不认真VB.Net,但这里似乎有很好的讨论VB中的扩展和缩小转换:[ ^ ]。

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: [^].


这篇关于没有属性的VB.NET类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆