Passing nullable string

T

tshad

I have a function that I want to pass data that gets data from my Sql
Database, so the value can be null but I get an error if I try it.

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<T>'

This works:

public void test(int? x){}

But this doesn't:

public void test(string? x){}

How can I get this to work?

I am using a dbReader and putting the data into a string or int or double
and then passing it to its functions.

Can you pass just a row of the dbReader?

Thanks,

Tom
 
A

Arne Vajhøj

tshad said:
I have a function that I want to pass data that gets data from my Sql
Database, so the value can be null but I get an error if I try it.

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<T>'

This works:

public void test(int? x){}

But this doesn't:

public void test(string? x){}

string is a reference type and is nullable, so just:

public void test(string x){}

will allow you to use null as parameter.

Arne
 
T

tshad

Makes sense.

Thanks,

Tom
Peter Duniho said:
That's a big hint to you. The error strongly implies that String is _not_
"non-nullable", which means it's already nullable. It would be pointless
to wrap String in Nullable<T>, since String itself (being a reference
type) is automatically "nullable".

Just use the String reference directly.

Pete
 

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

nullable types 2
Details needed 2
Nullable class 2
Nullable types giving error 13
How to use object? 3
Nullable Enum 2
How to add a constraint of Nullable Types to a generics method 2
Icomparer and Nullable field 6

Top