Vb.net 铸造LHS

Vb.net 铸造LHS,vb.net,string,operators,concatenation,Vb.net,String,Operators,Concatenation,嗯。现在这个很有趣。以前从未遇到过这种情况。在以下情况下,如何使用Concat Assign操作符(&=) Session("MyKey") &= "some string" 'Option Strict prohibits operands of type Object and String 看起来我甚至无法将LHS转换为字符串: DirectCast(Session("MyKey"), String) &= "some string" 'Cannot assign becu

嗯。现在这个很有趣。以前从未遇到过这种情况。在以下情况下,如何使用Concat Assign操作符(
&=

Session("MyKey") &= "some string" 'Option Strict prohibits operands of type Object and String
看起来我甚至无法将LHS转换为字符串:

DirectCast(Session("MyKey"), String) &= "some string" 'Cannot assign becuz LHS is a value
我甚至不能这样做:

Dim s as String = Session("MyKey").ToString()
s &= "some string" 's is a new animal. Doesn't affect Session("MyKey")

我知道使用简单的
Session(“MyKey”)=Session(“MyKey”).ToString()和“some string”
可以很容易地做到这一点,但我只是想确保我没有遗漏真正基本的东西。

你没有遗漏任何可笑的基本东西。会话变量的处理方式与对象类型的任何其他变量一样。不能在赋值目标上(即在赋值语句的左侧)使用DirectCast或ToString之类的运算符。将要指定的字符串连接到字符串变量(或字符串生成器)中,并将结果指定给会话变量

Dim str As String = ""
If IsNothing(Session("MyKey")) = False Then str = Session("MyKey").ToString
str &= "some string"
Session("MyKey") = str