Null

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have a method as follows:

public static bool Check(string value) {

And I am calling it:

Rule.Check(User.Level.Value.ToString() ?? "")

Where Level is of type Enum UserLevel:

public UserLevel? Level { get; set; }

When I call the method Check I get an error:
Nullable object must have a value.

I tried to add ?? "" but it is not working.

I know that in this case User.Level is null.

What am I doing wrong?

Thanks,
Miguel
 
shapper said:
Hello,

I have a method as follows:

public static bool Check(string value) {

And I am calling it:

Rule.Check(User.Level.Value.ToString() ?? "")

Where Level is of type Enum UserLevel:

public UserLevel? Level { get; set; }

When I call the method Check I get an error:
Nullable object must have a value.

I tried to add ?? "" but it is not working.

I know that in this case User.Level is null.

What am I doing wrong?

Thanks,
Miguel


Try calling Rule.Check(User.Level == null ? "" :
User.Level.Value.ToString()).

The problem is that you are calling .Value on a null (User.Level).
 
shapper said:
Hello,

I have a method as follows:

public static bool Check(string value) {

And I am calling it:

Rule.Check(User.Level.Value.ToString() ?? "")

Where Level is of type Enum UserLevel:

public UserLevel? Level { get; set; }

When I call the method Check I get an error:
Nullable object must have a value.

I tried to add ?? "" but it is not working.

I know that in this case User.Level is null.

What am I doing wrong?

Thanks,
Miguel

The nullable variable has a HasValue property that you can use to
determine if there is a value or not. If there is no value, using the
Value property causes an exception.

Rule.Check(User.Level.HasValue ? User.Level.Value.ToString() : string.Empty)
 
Back
Top