In C#, a property’s setter value keyword will automatically be same as the property’s type.
For example, in C# ,type of value is string
private string str = string.Empty;
public string MyText
{
get { return str; }
set { str = value; }
}
If we convert this snippet to VB.Net we get
Private str As String = String.Empty
Public Property MyText() As String
Get
Return str
End Get
Set(ByVal value As String)
str = value
End Set
End Property
Questions
-
Why does set have this line
Set(ByVal value As String)? Shouldn’t value type
automatically be String. This way.Private str As String = String.Empty Public Property MyText() As String Get Return str End Get Set str = value End Set End PropertyWhat’s the use of that?
-
I cannot change
BYValtoByRef(I tried, it gives error), then what’s use of that also?
You can omit the
(ByVal value As String)part. Visual Studio will keep adding it, but it is not required by either the language nor the compiler.You can use a parameter name other than
value. Also note that since VS2010 SP1, you can omit theByValkeyword.Example: