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 Property
What's the use of that?
I cannot change BYVal
to ByRef
(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 the ByVal
keyword.
Example:
Class Test
Private str As String = String.Empty
Public Property MyText() As String
Get
Return str
End Get
Set
str = value
End Set
End Property
Public Property MyText2() As String
Get
Return str
End Get
Set(something As String)
str = something
End Set
End Property
End Class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With