ekolis
ekolis

Reputation: 6816

How do I make a readonly computed property in VB.NET without specifying the full Get / End Get syntax?

I looked here but it only showed how to make constant readonly properties. I want a readonly computed property, that is reevaluated every time it's executed. So I can do something like this:

Public Property A As Double

Public Property B As Double

Public ReadOnly Property C As Double = Math.Sqrt(A * B) ' this will just save off the initial values of A and B and not recompute when they change, right?

In C# you can make a readonly property reevaluate each type by using the lambda operator instead of an equals sign:

private Random rng = new Random();

public int NotRandomNumber { get; } = rng.Next(); // caches the value

public int ActualRandomNumber { get; } => rng.Next(); // recomputes the value

But VB doesn't have a lambda operator that I'm aware of. So is there any way to do this in VB? Or do I have to write it out in full, with Get / End Get / Return?

Upvotes: 0

Views: 277

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39152

Yes, you need the full Get --> End Get with the ReadOnly modifier:

Public ReadOnly Property C As Double
    Get
        Return Math.Sqrt(A * B)
    End Get
End Property

VB.Net is VERBOSE...BY DESIGN.

If you want less verbosity, switch to C#?

Upvotes: 1

Related Questions