Nikhil Agrawal
Nikhil Agrawal

Reputation: 48600

Why does the setter of a VB.NET property require a typed argument and why is it ByVal?

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

  1. 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?

  2. I cannot change BYVal to ByRef (I tried, it gives error), then what's use of that also?

Upvotes: 12

Views: 2399

Answers (5)

Darren Grantham
Darren Grantham

Reputation: 1

You could have value be an enum and do a select case on it for instance and set the value that way. You don't have to limit the property's set call to only passing the same value type which is a nice feature

Upvotes: 0

sloth
sloth

Reputation: 101162

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

Upvotes: 9

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239824

Because you are allowed in VB to change the name of the parameter:

Public Property MyText() As String
    Get
        Return str
    End Get
    Set(ByVal val As String)
        str = val
    End Set
End Property

You can't change the ByVal to ByRef because property parameters can only ever be passed by value. But VB sometimes favours stating facts that are unalterable.

The developers could (in theory) have let you just have:

Set(val)

Because nothing else is alterable. But they opted to make the parameters for properties resembles the parameters for Functions and Subs.


In fact, you don't have to specify the parameter at all. Per the VB language spec (9.7.2):

If a parameter list is specified, it must have one member, that member must have no modifiers except ByVal, and its type must be the same as the type of the property. The parameter represents the property value being set. If the parameter is omitted, a parameter named Value is implicitly declared

And (9.5.2):

A parameter that does not specify ByRef or ByVal defaults to ByVal.

Upvotes: 2

to StackOverflow
to StackOverflow

Reputation: 124804

If the value were ByRef, the Setter would be able to modify the caller's value. It doesn't make sense to allow a property setter to cause side effects like this.

Upvotes: 1

Habib
Habib

Reputation: 223422

I think, its part of the syntax in Visual basic. You can omit the data type , if OptionStrict is not on. But if you specify the type, it must be same as of property type.

From MSDN - Set Statement (Visual Basic)

datatype
Required if Option Strict is On. Data type of the value parameter. The data type specified must be the same as the data type of the property where this Set statement is declared.

Upvotes: 2

Related Questions