10

A really boring question, sorry, but I really don't know that yet ;) I've tried always string.empty, but with a decimal this produces an error.

Is there any function? Unfortunately, for the simplest questions, there are no answers on google

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Florian Müller
  • 7,448
  • 25
  • 78
  • 120

3 Answers3

21

Your title (and tag) asks about an "int", but your question says that you're getting an error with a "decimal". Either way, there is no such thing as "empty" when it comes to a value type (such as an Integer, Decimal, etc.). They cannot be set to Nothing as you could with a reference type (like a String or class). Instead, value types have an implicit default constructor that automatically initializes your variables of that type to its default value. For numeric values like Integer and Decimal, this is 0. For other types, see this table.

So you can check to see if a value type has been initialized with the following code:

Dim myFavoriteNumber as Integer = 24
If myFavoriteNumber = 0 Then
    ''#This code will obviously never run, because the value was set to 24
End If

Dim mySecondFavoriteNumber as Integer
If mySecondFavoriteNumber = 0 Then
    MessageBox.Show("You haven't specified a second favorite number!")
End If

Note that mySecondFavoriteNumber is automatically initialized to 0 (the default value for an Integer) behind the scenes by the compiler, so the If statement is True. In fact, the declaration of mySecondFavoriteNumber above is equivalent to the following statement:

Dim mySecondFavoriteNumber as Integer = 0


Of course, as you've probably noticed, there's no way to know whether a person's favorite number is actually 0, or if they just haven't specified a favorite number yet. If you truly need a value type that can be set to Nothing, you could use Nullable(Of T), declaring the variable instead as:

Dim mySecondFavoriteNumber as Nullable(Of Integer)

And checking to see if it has been assigned as follows:

If mySecondFavoriteNumber.HasValue Then
    ''#A value has been specified, so display it in a message box
    MessageBox.Show("Your favorite number is: " & mySecondFavoriteNumber.Value)
Else
    ''#No value has been specified, so the Value property is empty
    MessageBox.Show("You haven't specified a second favorite number!")
End If
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Just a side note: actually you *can* assign Nothing to a value type in VB.Net. But in that case, Nothing does not mean 'null' but 'default(T)', so for integers it's the same as 0. – jeroenh Nov 22 '10 at 23:40
  • @jeroenh: That's correct. Notice that I said they can't be set to `Nothing` *as you could with a reference type*. Setting a value type to `Nothing` will cause it to be initialized back to its default type. The point is that there is no such `null` or "empty" state for value types; they always contain a value. – Cody Gray - on strike Nov 23 '10 at 00:52
  • Note more recently, `Dim mySecondFavoriteNumber as Integer?` is the same as `Dim mySecondFavoriteNumber as Nullable(Of Integer)` – Mark Schultheiss Sep 06 '13 at 13:31
3

Maybe what you are looking for is Nullable

    Dim foo As Nullable(Of Integer) = 1
    Dim bar As Nullable(Of Decimal) = 2

    If foo = 1 Then
        If bar = 2 Then
            foo = Nothing
            bar = Nothing
            If foo Is Nothing AndAlso bar Is Nothing Then Stop
        End If
    End If
dbasnett
  • 11,334
  • 2
  • 25
  • 33
0

Well, the default value for a number would be 0, but you can also try this:

int x = 123;
String s = "" + x; 

and then check the length or if the string 's' is empty.

CDspace
  • 2,639
  • 18
  • 30
  • 36
  • This method doesn't work in .Net 4.5.2... The length of an uninitialized integer is 1 and you need to convert to a string with ToString. I think this answer might've been in C :) – seadoggie01 Aug 02 '18 at 11:57