VB.NET Public Property on a single line

vb.net property get set shorthand
public property let in vb net
vb.net class property attributes
vb.net property default value
vb.net property vs variable
vb.net property private set
define property in vb net
vb.net create object with properties

Is there any way I can put Public Properties on a single line in VB.NET like I can in C#? I get a bunch of errors every time I try to move everything to one line.

C#:

public void Stub{ get { return _stub;} set { _stub = value; } }

VB.NET

Public Property Stub() As String
    Get
        Return _stub
    End Get
    Set(ByVal value As String)
        _stub = value
    End Set
End Property

Thanks

EDIT: I should have clarified, I'm using VB 9.0.

You can use automatically implemented properties in both VB 10 and C#, both of which will be shorter than the C# you've shown:

public string Stub { get; set; }

Public Property Stub As String

For non-trivial properties it sounds like you could get away with putting everything on one line in VB - but because it's that bit more verbose, I suspect you'd end up with a really long line, harming readability...

c# - VB.NET Public Property on a single line, Is there any way I can put Public Properties on a single line in VB.NET like I can in C#? I get a bunch of errors every time I try to move everything to one line. C#: The code is generated if you put the cursor on a blank line following the Property statement, type a G (for Get) or an S (for Set) and press ENTER. The Visual Basic Code Editor automatically generates the Get or Set procedure for read-only and write-only properties when you press ENTER at the end of a Property statement.

Yes you can

Public Property Stub() As String : Get : Return _stub : End Get : Set(ByVal value As String) :_stub = value : End Set : End Property

and you can even make it shorter and not at all readable ;-)

Public Property Stub() As String:Get:Return _stub:End Get:Set(ByVal value As String):_stub = value:End Set:End Property

Auto-Implemented Properties, https://docs.microsoft.com › › Visual Basic guide › Language reference VB.NET program that uses property syntax Class Example Private _count As Integer Public Property Number() As Integer Get Return _count End Get Set(ByVal value As Integer) _count = value End Set End Property End Class Module Module1 Sub Main() Dim e As Example = New Example() ' Set property. e.Number = 1 ' Get property.

It is possible to define a variable on one line, that behaves like a property, if one is willing to use a slightly different syntax, and link to a C# assembly (or play around with IL).

Tested with VS2017 and .Net 4.7.2.

The C# code (currently not available in VB.Net):

public class Propertor<T>
{
    public T payload;
    private Func<Propertor<T>, T> getter;
    private Action<Propertor<T>, T> setter;

    public T this[int n = 0]
    {
        get
        {
            return getter(this);
        }
        set
        {
            setter(this, value);
        }
    }

    public Propertor(Func<T> ctor = null, Func<Propertor<T>, T> getter = null, Action<Propertor<T>, T> setter = null)
    {
        if (ctor != null) payload = ctor();
        this.getter = getter;
        this.setter = setter;
    }
    private Propertor(T el, Func<Propertor<T>, T> getter = null)
    {
        this.getter = getter;
    }
    public static implicit operator T(Propertor<T> el)
    {
        return el.getter != null ? el.getter(el) : el.payload;
    }
    public override string ToString()
    {
        return payload.ToString();
    }
}

Then in your VB program, for example:

Private prop1 As New Propertor(Of String)(ctor:=Function() "prop1", getter:=Function(self) self.payload.ToUpper, setter:=Sub(self, el) self.payload = el + "a")
Private prop2 As New Propertor(Of String)(ctor:=Function() "prop2", getter:=Function(self) self.payload.ToUpper, setter:=Sub(self, el) self.payload = el + "a")
public Sub Main()
    Console.WriteLine("prop1 at start : " & prop1.ToString)
    Dim s1 As String = prop1
    Console.WriteLine("s1 : " & s1)
    Dim s2 As String = prop1()
    Console.WriteLine("s2 : " & s2)
    prop1() = prop1()
    Console.WriteLine("prop1 reassigned : " & prop1.ToString)
    prop1() = prop2()
    Console.WriteLine("prop1 reassigned again : " & prop1.ToString)
    prop1() = "final test"
    Console.WriteLine("prop1 reassigned at end : " & prop1.ToString)
end sub

This results in:

prop1 at start : prop1
s1 : PROP1
s2 : PROP1
prop1 reassigned : PROP1a
prop1 reassigned again : PROP2a
prop1 reassigned at end : final testa

Property Statement, If Typeof m(i) Is Property Info Then Dim pro As Property Info = CType(m(i), public Regions)escription property, one line for the private RegionDescription field,  VB needs to know that you want to set up a Property for your Class. The way you do this is type "Public Property … End Property". Access the code for your Class. Type a few lines of space between the End Sub of your DoMessageBox Method, and the line that reads "End Class". On a new line, type the following:

Building Client/Server Applications with VB .NET: An , The way you do this is type "Public Property … End Property". Access the code for your Class. Type a few lines of space between the End Sub of your  It is possible to define a variable on one line, that behaves like a property, if one is willing to use a slightly different syntax, and link to a C# assembly (or play around with IL). Tested with VS2017 and .Net 4.7.2. The C# code (currently not available in VB.Net): Then in your VB program, for example:

Creating Properties for your VB NET Classes, line */ /// <summary>XML comments on single line</summary> Public Property Size As Integer = -1 ' Default value, Get and Set both Public. A property procedure is a series of Visual Basic statements that manipulate a custom property on a module, class, or structure. Property procedures are also known as property accessors. Visual Basic provides for the following property procedures: A Get procedure returns the value of a property. It is called when you access the property in an

VB.NET and C# Comparison, C# and Visual Basic .NET are the two primary languages used to program on the . C# lacks the DirectCast (mapping to a single CLR instruction), strict type conversion can In VB you'd have to define two properties instead: a read-only property all Visual Basic keywords to the default capitalised forms, e.g. "Public", "If". VB will allow you to make a public only (which means in reality it is just a field) property with a default value. C# allows you make a mixed access property, but it is impossible to set a default value (except in the constructor)

Comments
  • VB.net equivalent of C# Property Shorthand? covers the answer - an equivalent is available as of VB10.
  • Yes you can use collan (:) to right multiple lines in a single line.
  • @djacobson: Not quite the same, as the OP isn't actually using automatically implemented properties in the code given...
  • @Jon Skeet The example given doesn't do anything to the field except assign/retrieve its value... That's the only case in which there exists an equivalent one-line property syntax between C# and VB, and that's auto-properties. Is it not so? :)
  • @djacobson: Yes, but that's not equivalent to the code in the question - that's what I'm saying. (I referred to automatically implemented properties in my answer, too.)
  • Looks like I need to be using VB version 10.0 for this (which I didn't specify or even know I was using). Thanks though.
  • @Fred: Right, have edited to make the answer clearer, thanks.
  • Wow, looks bad but at least it's not 8 lines of code! Considering I'm using VB 9.0 this is the best option. Thanks
  • Please consider: MAX(Readability) > MIN(Number of Lines). I wouldn't want to maintain code like the above...
  • it should work in VB9 it has been there since a long time. msdn.microsoft.com/en-us/library/865x40k4%28v=VS.71%29.aspx
  • @Chrissie1 - then I must have another problem, because I get the error "Visual Basic 9.0 does not support auto-implemented properties" and other syntax errors (blue lines)
  • What version of VS are you using and against which framework are you compiling?