473,503 Members | 12,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

? operator

Trying to figure out how to use the ? operator. It looked simple enough,
but of course it is never that way for me.

I am create a Parameter. One of the parameters my contain DBNull.value or
a string value. I set up the following senario....

string dbOwner = _dbOwner; // could have a value or a 0 length

I have my own method for creating a parameter. If I place DBNull.value in
this method, it works just fine. Likewise, if I place an actual string in
this field (5th parameter) it works just fine

CreateDataParameter(_internalCmd,"@procedure_owner ",
SqlDbType.NVarChar,
384,
((dbOwner.Length > 0) ? dbOwner: DBNull.Value),
DBUtility.ParameterDir.Input,
true);

I get the following compile error - "No implicit conversion between string
and System.DbNull.

Maybe the problem is not with the ? at all, maybe the compiler is
complaining about two defferent types, but it would seem that it would have
complained about this without the expression and just having DBNull.Value,
but it does not complain.

Any idea how to overcome?

Nov 15 '05 #1
6 1289
Jim Heavey <Ji*******@nospam.com> wrote:
Trying to figure out how to use the ? operator. It looked simple enough,
but of course it is never that way for me.

I am create a Parameter. One of the parameters my contain DBNull.value or
a string value. I set up the following senario....

string dbOwner = _dbOwner; // could have a value or a 0 length

I have my own method for creating a parameter. If I place DBNull.value in
this method, it works just fine. Likewise, if I place an actual string in
this field (5th parameter) it works just fine


What actually *is* the declared type of the 5th parameter though? I
can't see how both versions can work, unless you've got two different
overloads... Could you show the method declaration?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jim Heavey <Ji*******@nospam.com> wrote in
news:Xn*********************************@207.46.24 8.16:
Trying to figure out how to use the ? operator. It looked
simple enough, but of course it is never that way for me.


Jim,

Use DBNull.Value.ToString().

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #3
Try:

((dbOwner.Length > 0)? (object)dbOwner: (object)DBNull.Value)

I think the compiler is trying to figure out what type the ? operator should
return, and is not seeing any connection between dbOwner (String) and
DBNull.Value (DBNull). I suppose the compiler COULD find a common ancestor
in the inheritance hierarchy (Object), it forces you to be clear on your
intentions, which reinforces C#'s typesafety. You have to tell the compiler
that the result you are expecting an object to result from the operation.

--Matthew W. Jackson

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
Trying to figure out how to use the ? operator. It looked simple enough,
but of course it is never that way for me.

I am create a Parameter. One of the parameters my contain DBNull.value or
a string value. I set up the following senario....

string dbOwner = _dbOwner; // could have a value or a 0 length

I have my own method for creating a parameter. If I place DBNull.value in
this method, it works just fine. Likewise, if I place an actual string in
this field (5th parameter) it works just fine

CreateDataParameter(_internalCmd,"@procedure_owner ",
SqlDbType.NVarChar,
384,
((dbOwner.Length > 0) ? dbOwner: DBNull.Value),
DBUtility.ParameterDir.Input,
true);

I get the following compile error - "No implicit conversion between string
and System.DbNull.

Maybe the problem is not with the ? at all, maybe the compiler is
complaining about two defferent types, but it would seem that it would have complained about this without the expression and just having DBNull.Value,
but it does not complain.

Any idea how to overcome?

Nov 15 '05 #4
Then again I may be wrong. I'm not at a computer with C# right now.

Thinking about it, it seems like you are actually trying to call an
overloaded function...one which takes a string and one which takes DBNull
(or maybe an object).

You either need to

1) use a real if statement to call the functions, since they are actually
two different functions, and the ? operator can only be used that way if it
is the same function (with the same signature).

2) create a new overload of your function which accepts object, uses typeof
to determine the type, and call the appropriate overload. I would not
suggest this in this particular case because you should be able to bind to
the specfic funciton at compile time if you use an if structure rather than
an ?.

As far as I know, C# doesn't support a way to do the overload resolution at
run-time. I have not found a way to automatically call a function based on
a parameter's type---sort of a "virtual" parameter. A late-bound language
may allow this--i'm not sure.

--Matthew W. Jackson

"Matthew W. Jackson" <th********************@NOSPAM.NOSPAM> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
Try:

((dbOwner.Length > 0)? (object)dbOwner: (object)DBNull.Value)

I think the compiler is trying to figure out what type the ? operator should return, and is not seeing any connection between dbOwner (String) and
DBNull.Value (DBNull). I suppose the compiler COULD find a common ancestor in the inheritance hierarchy (Object), it forces you to be clear on your
intentions, which reinforces C#'s typesafety. You have to tell the compiler that the result you are expecting an object to result from the operation.

--Matthew W. Jackson

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
Trying to figure out how to use the ? operator. It looked simple enough, but of course it is never that way for me.

I am create a Parameter. One of the parameters my contain DBNull.value or a string value. I set up the following senario....

string dbOwner = _dbOwner; // could have a value or a 0 length

I have my own method for creating a parameter. If I place DBNull.value in this method, it works just fine. Likewise, if I place an actual string in this field (5th parameter) it works just fine

CreateDataParameter(_internalCmd,"@procedure_owner ",
SqlDbType.NVarChar,
384,
((dbOwner.Length > 0) ? dbOwner: DBNull.Value),
DBUtility.ParameterDir.Input,
true);

I get the following compile error - "No implicit conversion between string and System.DbNull.

Maybe the problem is not with the ? at all, maybe the compiler is
complaining about two defferent types, but it would seem that it would

have
complained about this without the expression and just having DBNull.Value, but it does not complain.

Any idea how to overcome?


Nov 15 '05 #5
Then again maybe not. Let me look at this again.

Could you give us the parameter list to your function?

Are you trying to resolve an overloaded function at runtime? I don't think
that C# supports any kind of virtual parameters. You will have to code this
yourself if this is want you want...it could be as easy as making a version
of your function that takes an object and uses a typeof to determine which
overload to call.

But upon looking at your code again I'm fairly certain that you are simply
trying to combine an if/else structure into a single line. This won't work
in your case (if you do have two versions of your function) because you
would be calling two different functions. The ? operator would only work if
you were calling the same function (with the exact same signature).

--Matthew W. Jackson

"Matthew W. Jackson" <th********************@NOSPAM.NOSPAM> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
Try:

((dbOwner.Length > 0)? (object)dbOwner: (object)DBNull.Value)

I think the compiler is trying to figure out what type the ? operator should return, and is not seeing any connection between dbOwner (String) and
DBNull.Value (DBNull). I suppose the compiler COULD find a common ancestor in the inheritance hierarchy (Object), it forces you to be clear on your
intentions, which reinforces C#'s typesafety. You have to tell the compiler that the result you are expecting an object to result from the operation.

--Matthew W. Jackson

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
Trying to figure out how to use the ? operator. It looked simple enough, but of course it is never that way for me.

I am create a Parameter. One of the parameters my contain DBNull.value or a string value. I set up the following senario....

string dbOwner = _dbOwner; // could have a value or a 0 length

I have my own method for creating a parameter. If I place DBNull.value in this method, it works just fine. Likewise, if I place an actual string in this field (5th parameter) it works just fine

CreateDataParameter(_internalCmd,"@procedure_owner ",
SqlDbType.NVarChar,
384,
((dbOwner.Length > 0) ? dbOwner: DBNull.Value),
DBUtility.ParameterDir.Input,
true);

I get the following compile error - "No implicit conversion between string and System.DbNull.

Maybe the problem is not with the ? at all, maybe the compiler is
complaining about two defferent types, but it would seem that it would

have
complained about this without the expression and just having DBNull.Value, but it does not complain.

Any idea how to overcome?



Nov 15 '05 #6
This worked....

(_dbOwner.Length==0 ? (object)DBNull.Value: (object)_dbOwner)

Thanks for your help
Nov 15 '05 #7

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

Similar topics

7
8016
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
3852
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
6
4399
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
5
2272
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
3
3251
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
7193
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,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.