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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Enum Extentions 7
Create record. Linq to SQL 3
Compare and Null 4
Compare Values 9
Nullable class 2
Extension methods cannot be dynamically dispatched ... 3
Nullable Enum 2
How to use object? 3

Back
Top