9

I'm a C# programmer but am converting some code from C# to VB.NET. In c# I can simply use (int)blah.getValue() where getValue() returns an Integer?

Doing a DirectCast() in VB.NET doesn't work though, saying Integer? cannot be converted to Integer.

Ideas?

1
  • 1
    Why would you need to cast it? If getValue() returns Integer?, then the .Value property is of type Integer. Just use blah.getValue().Value. Or also use blah.getValue().GetValueOrDefault(<default value here>) Commented Jun 18, 2014 at 14:43

8 Answers 8

11

Use the value property to get the actual integer value.

Dim intNullable As Integer?

If intNullable.HasValue Then
 Return intNullable.Value
End If
Sign up to request clarification or add additional context in comments.

2 Comments

+1. For bonus marks, add an Else clause that throws InvalidOperationException "Nullable object must have a value." That's what happens when the C# (int) cast encounters a null value.
This is the correct answer. (An Else clause is superfluous when you test '.HasValue')
9

Integer? is a nullable type, so you may have to convert it to a nullable Integer.

You want to use CType function like so, 'Integer' or 'Integer?' Depending on your situation

Val = CType(Source,Integer?)

2 Comments

CType sorted it with just Integer. Thanks!
-1 This answer is not totally correct. There's a different behaviour for null. (int) blah.getValue() throws an exception if blah.getValue() is null. CType(blah.getValue(), Integer) returns zero if blah.getValue() is null.
1

CInt(ValueToBeInteger)

Comments

1

If you use CType or CInt or another conversion function to convert the nullable integer to a regular integer, a null value will be converted to zero.

Check to see if the nullable type has a value as in @Rhapsody 's answer.

You can also use the nullable type methods .GetValueOrDefault() and .GetValueOrDefault(x). The overload without any parameters returns the default value of the datatype if the value is null. E.g. Integer default value is zero; Boolean default value is False. The overload with a parameter allows you to specify a default value to use if the value is null.

Nullable.GetValueOrDefault Method

Comments

1
myNonNullableVar = if(myNullableVar, 0)

will set 0 if integer? is null or return the actual value.

In C#, it would be:

 myNonNullableVar = myNullableVar ?? 0;

Comments

0

You should use CType function like this:

CType(blah.getValue(), Integer)

Comments

0
dim a as integer? = 1
dim b as integer = a.value

Remember to check if a.hasvalue (returns boolean, true if a has value).

Comments

-1

@SLC: It is very simple to get values of Nullable Int32? of C# into Integer.

Example:

    ID(Nullable<Int32?>)

You have to do: ID.value

where ID is a NullableObject.

And it will return an integer in VB.NET.

2 Comments

The language sample provided looks like it's in C# and the OP was specifically asking for VB.NET. I'm not sure this answer provides any additional information to the wealth of answers already available on this question.
Nullable<Int32?> makes no sense. Perhaps you meant to type Nullable<Int32> (without the question mark)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.