473,654 Members | 3,022 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

CreateDataParam eter(_internalC md,"@procedure_ owner",
SqlDbType.NVarC har,
384,
((dbOwner.Lengt h > 0) ? dbOwner: DBNull.Value),
DBUtility.Param eterDir.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 1297
Jim Heavey <Ji*******@nosp am.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jim Heavey <Ji*******@nosp am.com> wrote in
news:Xn******** *************** **********@207. 46.248.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.To String().

Hope this helps.

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

((dbOwner.Lengt h > 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*******@nosp am.com> wrote in message
news:Xn******** *************** **********@207. 46.248.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

CreateDataParam eter(_internalC md,"@procedure_ owner",
SqlDbType.NVarC har,
384,
((dbOwner.Lengt h > 0) ? dbOwner: DBNull.Value),
DBUtility.Param eterDir.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******** ******@TK2MSFTN GP09.phx.gbl...
Try:

((dbOwner.Lengt h > 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*******@nosp am.com> wrote in message
news:Xn******** *************** **********@207. 46.248.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

CreateDataParam eter(_internalC md,"@procedure_ owner",
SqlDbType.NVarC har,
384,
((dbOwner.Lengt h > 0) ? dbOwner: DBNull.Value),
DBUtility.Param eterDir.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******** ******@TK2MSFTN GP09.phx.gbl...
Try:

((dbOwner.Lengt h > 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*******@nosp am.com> wrote in message
news:Xn******** *************** **********@207. 46.248.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

CreateDataParam eter(_internalC md,"@procedure_ owner",
SqlDbType.NVarC har,
384,
((dbOwner.Lengt h > 0) ? dbOwner: DBNull.Value),
DBUtility.Param eterDir.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.Lengt h==0 ? (object)DBNull. Value: (object)_dbOwne r)

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
8026
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 something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example, in the second case, assume that a,b,c,d represent 2-bit integers, and e represents an 8-bit...
1
3867
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 "BitString.h"
6
4424
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
2290
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
3
3272
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, obj2 ;
0
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1593
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.