473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

2.0Beta1: Determine T from T?

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

Nov 16 '05 #1
8 1440
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.

<n8***@arrl.net> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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

Nov 16 '05 #2
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!

Nov 16 '05 #3
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

<n8***@arrl.net> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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

Nov 16 '05 #4
Alexander Shirshov <al*******@omnitalented.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.
Nov 16 '05 #6

"Alexander Shirshov" <al*******@omnitalented.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.
Nov 16 '05 #7
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

Nov 17 '05 #8
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:OY**************@TK2MSFTNGP15.phx.gbl...

"Alexander Shirshov" <al*******@omnitalented.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nathan Sokalski | last post by:
I want to determine the operating system using ASP (or VBScript inside of ASP). I tried to get it using the Request. ServerVariables(EnvironmentVariable) method. On the web page about ASP in which...
3
by: Shahid Juma | last post by:
Hello All, This may be a trivial question, but I was wondering how can you determine if a value contains an Integer or Float. I know there is a function called IsNumeric, however you can't...
5
by: Hennie de Nooijer | last post by:
Hi, This is a diffcult issue to explain. I hope to make my problem clear to you. SITUATION I'm building A SLA Query for a customer. This customer has an awkward way to determine the SLA results...
3
by: lucpustjens | last post by:
Hello, I want te determine the client operating system, because I need to kno the default cookie directory. If there is a way to determine the cooki directory directly, than it is also good. ...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
9
by: Adam | last post by:
Can someone please help!! I am trying to figure out what a font is? Assume I am working with a fixed font say Courier 10 point font Question 1: What does this mean 10 point font Question 2:...
3
by: Phil Thompson | last post by:
Riverbank Computing is pleased to announce the release of PyQt v4.0beta1 available from http://www.riverbankcomputing.co.uk/pyqt/. PyQt is a comprehensive set of Qt bindings for the Python...
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
6
by: Jana | last post by:
Greetings Access Gurus! I am working on an app to send batch transactions to our bank, and the bank requires that we place an effective date on our files that is 'one business day in the future,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.