Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable declaration with Nothing check

Tags:

vb.net

Very (very) often we need to write stuff like

Dim Data = GetSomeData()
If Data IsNot Nothing Then
  Data.DoSomething()
Else
  ...
End If

Maybe I am asking in vain but I am seriously hoping that VB.Net has some construct like:

IfExists Data = GetSomeData() Then
  Data.DoSomething()
Else
  ...
End IfExists

In my dreams it does two important things:

  1. No extra line for Nothing check
  2. Variable A is not visible outside of the block and thus can't be used later on by mistake (just like "Using" or "With")

Is there anything similar to that that I haven't found yet?

Thanks!

EDIT: Inspired by Bjørn-Roger Kringsjå's Answer I came up with something that would satisfy me (humbled by VB.Net's deficiencies):

<Extension()>
Public Sub IfExists(Of T)(This As T, DoIfNotNothing As Action(Of T), Optional DoIfNothing As Action = Nothing)
    If This IsNot Nothing Then
        DoIfNotNothing(This)
    ElseIf DoIfNothing IsNot Nothing Then
        DoIfNothing()
    End If
End Sub

Then I can call it like this (with the false part being optional)

GetSomeData().IfExists(Sub(Data) Data.DoSomething())

or

GetSomeData().IfExists(Sub(Data) Data.DoSomething(), Sub() DoSomethingElse())
like image 764
Majnu Avatar asked Aug 07 '14 17:08

Majnu


People also ask

Can you have an empty variable?

A variable is considered empty if it does not exist or if its value equals false .

Can you declare a variable without giving it a value?

He says that it's also possible to declare a variable without giving it an initial value and also that we must be careful not to use a variable which has been declared without an initial value and that has not been assigned a value.

Can you declare an empty variable in Python?

In Python, sometimes it makes sense to declare a variable and not assign a value. To declare a variable without a value in Python, use the value “None”.


2 Answers

As stated by others and implied by me, it can't be done. Just like to share a 3'rd solution. This time we're going to use delegates.

  1. No extra line for Nothing check
  2. Variable A is not visible outside of the block and thus can't be used later on by mistake.

Implementation

Public Module Extensions

    Public Sub IfExists(Of T)(testExpr As T, trueDlg As Action(Of T), falseDlg As Action)
        If (Not testExpr Is Nothing) Then
            trueDlg.DynamicInvoke(New Object(0) {testExpr})
        Else
            falseDlg.DynamicInvoke(New Object(-1) {})
        End If
    End Sub

End Module

Usage

IfExists(GetSomeData(),
         Sub(A As Object)
             'We have something (A)
         End Sub,
         Sub()
             'We have nothing
         End Sub
    )

Shorter:

IfExists(GetSomeData(), Sub(A As Object)
                                'We have something (A)
                            End Sub, Sub()
                                         'We have nothing
                                     End Sub)

Or, the shortest version:

IfExists(GetSomeData(), Sub(A As Object) Debug.WriteLine(A.ToString()), Sub() Debug.WriteLine("Nothing"))
like image 94
Bjørn-Roger Kringsjå Avatar answered Oct 21 '22 05:10

Bjørn-Roger Kringsjå


No, unfortunately there is nothing like that currently in VB.NET. The closest thing you could do to approximate that behavior would be to write a function like this:

Public Function Assign(ByRef target As Object, value As Object) As Boolean
    target = value
    Return (target IsNot Nothing)
End Function

Then you could use it like this:

Dim A As SomeType
If Assign(A, GetSomeData()) Then
    ' ...
Else
    ' ...
End If

But, as you pointed out, that doesn't really solve either of your stated problems. It's still an extra line of code, and the variable is still not scoped to only be accessible within the block where it was properly assigned.

like image 35
Steven Doggart Avatar answered Oct 21 '22 04:10

Steven Doggart