Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET重载数组访问?_Vb.net_Operator Overloading_Overloading - Fatal编程技术网

VB.NET重载数组访问?

VB.NET重载数组访问?,vb.net,operator-overloading,overloading,Vb.net,Operator Overloading,Overloading,是否可以在VB.net中重载数组/dict访问操作符?例如,您可以声明如下内容: Dim mydict As New Hashtable() mydict.add("Cool guy", "Overloading is dangerous!") mydict("Cool guy") = "Overloading is cool!" 这很好用。但我想说的是: mydict("Cool guy") = "3" 然后让3自动转换成整数3 我的意思是,当然我可以有一个私有成员mydict.coolg

是否可以在VB.net中重载数组/dict访问操作符?例如,您可以声明如下内容:

Dim mydict As New Hashtable()
mydict.add("Cool guy", "Overloading is dangerous!")
mydict("Cool guy") = "Overloading is cool!"
这很好用。但我想说的是:

mydict("Cool guy") = "3"
然后让3自动转换成整数3

我的意思是,当然我可以有一个私有成员mydict.coolguy和setCoolguy()和getCoolguy()方法,但如果可能的话,我更愿意用前一种方式编写它

谢谢


澄清一下——我希望能够“做有价值的事情”。比如说,我有

myclass.fizzlesticks ' String type
myclass.thingone     ' Numerical type, say integer
然后我想能够写作

myclass("thingummy") = "This is crazy"
它触发了一个如下所示的方法

Private sub insanitea(Byval somarg as Object, Byval rhs as Object)
    If somearg = "thingummy" And rhs = "This is crazy" Then
        thingone = 4
        fizzlesticks = rhs & " and cool too!"
    End If
End Sub

这不是确切的用例,但我认为它能更好地说明我在寻找什么?

为什么您不能执行以下操作:

mydict("Cool guy") = 3 //without quotes
那你就可以了

dim x =  mydict("Cool guy") + 7

//x returns 10
或者,你可以做一个Int32.tryParse

dim x as integer
if int32.tryParse(mydict("Cool guy"), x) then
     return x + 7 //would return 10
end if

不,不能在Visual Basic中重载数组访问运算符

您可以重载的唯一运算符是:

Unary operators:
+   -   Not   IsTrue   IsFalse   CType

Binary operators:
+   -   *   /   \   &   Like   Mod   And   Or   Xor
^   <<   >>   =   <>   >   <   >=   <=
一元运算符:
+-非IsTrue为False CType
二进制运算符:
+-*/\&类似Mod和或Xor

^>=><>=我尝试了一下你的问题,但不确定问题的标题和我读到的内容。让我知道我是否在这里偏离了基准。这可能和我的例子一样准确,如果这是我所需要的,那么你的解决方案会非常好。我只想在我的类中有一些稍微复杂的行为,也就是说,在赋值时,如果值不是应该的值,我希望能够转换它们。如果这有意义的话。。。