tags:

views:

6801

answers:

4
    private string? typeOfContract
    {
      get { return (string?)ViewState["typeOfContract"]; }
      set { ViewState["typeOfContract"] = value; }
    }

Later in the code I use it like this:

    typeOfContract = Request.QueryString["type"];

I am getting the following error at the declaration of typeOfContract line stating "The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'"

Any ideas? Basically, I want to make sure that "type" exists in the queryString before performing an action.

Thanks in advance.

+25  A: 

System.String is a reference type and already "nullable".

Nullable<T&gt> and the ? suffix are for value types such as Int32, Double, DateTime, etc.

Joe
Man i love this place... that was quick... thank you
Mike Fielden
One thing to note: Nullable<T> is a value type itself, but the "struct" generic type constraint only includes non-nullable value types - so you can't do Nullable<Nullable<int>>.
Jon Skeet
+1  A: 

You are making it complicated. string is already nullable. You don't need to make it more nullable. Take out the ? on the property type.

jop
+2  A: 

string cannot be the parameter to Nullable because string is not a value type. String is a reference type.

string s = null;

is a very valid statement and there is not need to make it nullable.

private string typeOfContract
    {
      get { return ViewState["typeOfContract"] as string; }
      set { ViewState["typeOfContract"] = value; }
    }

should work because of the as keyword.

siz
+1  A: 

String is a reference type, so you don't need to (and cannot) use Nullable<T> here. Just declare typeOfContract as string and simply check for null after getting it from the query string. Or use String.IsNullOrEmpty if you want to handle empty string values the same as null.

csgero