From: tshad on
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



From: Arne Vajhøj on
tshad wrote:
> 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
From: Peter Duniho on
On Mon, 15 Dec 2008 16:35:54 -0800, tshad <tfs(a)dslextreme.com> wrote:

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

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
From: tshad on
Makes sense.

Thanks,

Tom
"Peter Duniho" <NpOeStPeAdM(a)nnowslpianmk.com> wrote in message
news:op.ul8ckugb8jd0ej(a)petes-computer.local...
> On Mon, 15 Dec 2008 16:35:54 -0800, tshad <tfs(a)dslextreme.com> wrote:
>
>> 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>'
>
> 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