PC Review


Reply
Thread Tools Rate Thread

2.0Beta1: Determine T from T?

 
 
n8wrl@arrl.net
Guest
Posts: n/a
 
      21st Mar 2005
Good afternoon!

I have a method that adds a SQL parameter with the right SQLtype based
on a CLR type:

public void AddParam<T>(string name, T value)
{
...
}

I translate 'T' to a valid DbType. The problem is when T is Nullable,
as in

AddParam<int?>(name, x);

How can I get the 'int' from 'int?' inside AddParam() so I can
translate the type & handle NULLs properly?

Things I've tried:

* Nullable.ToObject<T>(value) returns a boxed object.
* value ?? value returns a T?
* Nullable.ValueOrDefault<T>(value) returns a boxed object.
* Try a Nullable-specific AddParam<T>(...) where T : Nullable but that
complains about static structures as a constraint.
* Assign value directly to the SQL procedure but I get a "No mapping
exists from object type System.Nullable to a known managed provider
native type."

Thanks!

-Brian

 
Reply With Quote
 
 
 
 
Sylvain Lafontaine
Guest
Posts: n/a
 
      21st Mar 2005
Why aren't you using a SqlTypes like SqlInt32 instead of « int? » ?

int? has not be introduced with the intent of replacing SqlTypes like
SqlInt32.

S. L.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Good afternoon!
>
> I have a method that adds a SQL parameter with the right SQLtype based
> on a CLR type:
>
> public void AddParam<T>(string name, T value)
> {
> ...
> }
>
> I translate 'T' to a valid DbType. The problem is when T is Nullable,
> as in
>
> AddParam<int?>(name, x);
>
> How can I get the 'int' from 'int?' inside AddParam() so I can
> translate the type & handle NULLs properly?
>
> Things I've tried:
>
> * Nullable.ToObject<T>(value) returns a boxed object.
> * value ?? value returns a T?
> * Nullable.ValueOrDefault<T>(value) returns a boxed object.
> * Try a Nullable-specific AddParam<T>(...) where T : Nullable but that
> complains about static structures as a constraint.
> * Assign value directly to the SQL procedure but I get a "No mapping
> exists from object type System.Nullable to a known managed provider
> native type."
>
> Thanks!
>
> -Brian
>



 
Reply With Quote
 
BrianS
Guest
Posts: n/a
 
      21st Mar 2005
At some point I have to interface SQL types to 'real' types on business
objects - types clients are familiar with, like int, string, etc. This
is the place I chose to do that.

Thanks for the reply!

 
Reply With Quote
 
Alexander Shirshov
Guest
Posts: n/a
 
      22nd Mar 2005
Brian,

Probably I'm missing something, but I don't understand what are you trying
to achieve.

Since you're passing a value, you already have type information in it and
you can simply call GetType():

void AddParam(string name, object value)
{
if (value.GetType() == typeof(string))
{
...
}
....
}

If you want to give callers some degree of control over parameter's data
type (like in AddParam("int_column", "123")) then you could add an overload
with datatype parameter:

void AddParam(string name, object value, Type dataType)


HTH,
Alexander

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Good afternoon!
>
> I have a method that adds a SQL parameter with the right SQLtype based
> on a CLR type:
>
> public void AddParam<T>(string name, T value)
> {
> ...
> }
>
> I translate 'T' to a valid DbType. The problem is when T is Nullable,
> as in
>
> AddParam<int?>(name, x);
>
> How can I get the 'int' from 'int?' inside AddParam() so I can
> translate the type & handle NULLs properly?
>
> Things I've tried:
>
> * Nullable.ToObject<T>(value) returns a boxed object.
> * value ?? value returns a T?
> * Nullable.ValueOrDefault<T>(value) returns a boxed object.
> * Try a Nullable-specific AddParam<T>(...) where T : Nullable but that
> complains about static structures as a constraint.
> * Assign value directly to the SQL procedure but I get a "No mapping
> exists from object type System.Nullable to a known managed provider
> native type."
>
> Thanks!
>
> -Brian
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Mar 2005
Alexander Shirshov <(E-Mail Removed)> wrote:
> Probably I'm missing something, but I don't understand what are you trying
> to achieve.
>
> Since you're passing a value, you already have type information in it and
> you can simply call GetType():
>
> void AddParam(string name, object value)
> {
> if (value.GetType() == typeof(string))
> {
> ...
> }
> ....
> }


Note that that's a lot slower (and less readable, IMO) than:

if (value is string)

or

string valAsString = value as string;
if (valAsString != null)
{
....
}

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Alexander Shirshov
Guest
Posts: n/a
 
      22nd Mar 2005

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Note that that's a lot slower (and less readable, IMO) than:
>
> if (value is string)


Is uniformity important for readability? "is" will not work for value types.

And I don't think the performance is an issue for the original poster. DB
operations are many orders of magnitude slower and I don't think anyone
would be adding query parameters by thousands.


 
Reply With Quote
 
Daniel O'Connell [C# MVP]
Guest
Posts: n/a
 
      22nd Mar 2005

"Alexander Shirshov" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> "Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Note that that's a lot slower (and less readable, IMO) than:
>>
>> if (value is string)

>
> Is uniformity important for readability? "is" will not work for value
> types.
>


I believe you are mistaken. "as" will not work for value types, but is will
work perfectly well. "is" will(atleast in the 2.0 beta) issue a warning
informing you that the expression will always be true if you apply is to a
value type variable(ie meaning a non-boxed value type), but it still works.


 
Reply With Quote
 
BrianS
Guest
Posts: n/a
 
      22nd Mar 2005
You're probably right. I was trying to use generics to avoid boxing and
a dozen type-specific methods that are all pretty much the same. But
since It's assigned to SqlParameter.Value we box anyway.

The other problem is even if value is an int?, for example, 'if (value
is Nullable)' returns false, so it's not easy to figure that out in a
generic <T> way.

Thanks for the input!

-Brian

 
Reply With Quote
 
Alexander Shirshov
Guest
Posts: n/a
 
      22nd Mar 2005
Yes, I quoted and commented not the sentence I intended. Thanks for
correcting.

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:(E-Mail Removed)...
>
> "Alexander Shirshov" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>
>> "Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Note that that's a lot slower (and less readable, IMO) than:
>>>
>>> if (value is string)

>>
>> Is uniformity important for readability? "is" will not work for value
>> types.
>>

>
> I believe you are mistaken. "as" will not work for value types, but is
> will work perfectly well. "is" will(atleast in the 2.0 beta) issue a
> warning informing you that the expression will always be true if you apply
> is to a value type variable(ie meaning a non-boxed value type), but it
> still works.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine best way to sum WLMPilot Microsoft Excel Misc 4 22nd Dec 2008 02:03 AM
Anyway to determine Thomas R Grassi Jr Windows XP General 5 24th Oct 2006 07:05 PM
how to determine SID jg Windows XP Help 2 5th Mar 2006 01:21 AM
[Help]How do we determine? Wang.Hua Windows XP General 0 2nd Nov 2004 03:34 AM
TopologiLinux 4.0Beta1 4.0.x - A Linux distribution designed to coexist with Windows. Gordon Darling Freeware 0 3rd Oct 2003 09:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:52 PM.