? operator

J

Jim Heavey

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?
 
C

Chris R. Timmons

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.
 
J

Jon Skeet [C# MVP]

Jim Heavey said:
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?
 
M

Matthew W. Jackson

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
 
M

Matthew W. Jackson

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
 
M

Matthew W. Jackson

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
 
J

Jim Heavey

This worked....

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

Thanks for your help
 

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

Top