473,401 Members | 2,139 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,401 software developers and data experts.

Cannot implicitly convert type

I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"

So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.

thnx,
Christoph
Nov 16 '05 #1
22 27822
Christopher,

What are the defined data types of BrokerName and BrokerId?

Telmo Sampaio

"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:eb**************@TK2MSFTNGP09.phx.gbl...
I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"

So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.

thnx,
Christoph

Nov 16 '05 #2
Christoph Boget <jc*****@yahoo.com> wrote:
I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"

So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.


I suspect that BrokerName is a string property - in which case, if the
condition is true, it's as if you're saying:

retval[curRowNum].BrokerName = SqlString.Null;

The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
> What are the defined data types of BrokerName and BrokerId?

'string' and 'int', respecitvely.

Christoph
Nov 16 '05 #4
> I suspect that BrokerName is a string property

It is.
- in which case, if the condition is true, it's as if you're saying:
retval[curRowNum].BrokerName = SqlString.Null;
I didn't think there would be an issue with setting a string property
to null. Or any data type to null. Is that not the case?
The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


And 'String.Null' is an object? Not a data type?

thnx,
Christoph
Nov 16 '05 #5
Christoph Boget <jc*****@yahoo.com> wrote:
I suspect that BrokerName is a string property


It is.
- in which case, if the condition is true, it's as if you're saying:
retval[curRowNum].BrokerName = SqlString.Null;


I didn't think there would be an issue with setting a string property
to null. Or any data type to null. Is that not the case?


SqlString.Null is of type SqlString, not string, and it's not the same
as a null reference.
The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


And 'String.Null' is an object? Not a data type?


SqlString.Null is a reference to a particular instance of SqlString.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
> The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


I've tried changing the code to this:

_brokerGroupId = toReturn.Rows[0]["BrokerGroupId"] == System.DBNull.Value ?
null : (int)toReturn.Rows[0]["BrokerGroupId"];

so it's not converting from an object (presumably SqlInt32.Null?) but now
I'm getting the error:

"Type of conditional expression can't be determined because there is no
implicit
conversion between '<null>' and 'int'"

I just am not understanding this. Why is there an issue setting a data type
to
null? There shouldn't be one...

thnx,
Christoph
Nov 16 '05 #7
Hi,
In addition to Jon comments you also have the problem that the terniary
operator return a different type in the true and false statement, this is
not valid and you have to solve it. Maybe that is the "implicitiness"

It's an error any way :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Christoph Boget <jc*****@yahoo.com> wrote:
I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"
So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.


I suspect that BrokerName is a string property - in which case, if the
condition is true, it's as if you're saying:

retval[curRowNum].BrokerName = SqlString.Null;

The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8
so... that answer your question...

"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
What are the defined data types of BrokerName and BrokerId?


'string' and 'int', respecitvely.

Christoph

Nov 16 '05 #9
Try this...

_brokerGroupId = (!toReturn.Rows[0].IsNull("BrokerGroupId") ?
(int)toReturn.Rows[0]["BrokerGroupId"] : -1);

There is no implicit conversion between null and int.

HTH,

Kyril

"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


I've tried changing the code to this:

_brokerGroupId = toReturn.Rows[0]["BrokerGroupId"] == System.DBNull.Value
?
null : (int)toReturn.Rows[0]["BrokerGroupId"];

so it's not converting from an object (presumably SqlInt32.Null?) but now
I'm getting the error:

"Type of conditional expression can't be determined because there is no
implicit
conversion between '<null>' and 'int'"

I just am not understanding this. Why is there an issue setting a data
type
to
null? There shouldn't be one...

thnx,
Christoph

Nov 16 '05 #10
Christoph Boget <jc*****@yahoo.com> wrote:
The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


I've tried changing the code to this:

_brokerGroupId = toReturn.Rows[0]["BrokerGroupId"] == System.DBNull.Value ?
null : (int)toReturn.Rows[0]["BrokerGroupId"];

so it's not converting from an object (presumably SqlInt32.Null?) but now
I'm getting the error:

"Type of conditional expression can't be determined because there is no
implicit
conversion between '<null>' and 'int'"

I just am not understanding this. Why is there an issue setting a data type
to null? There shouldn't be one...


Yes there is, because int is a value type - and you can't set a value
type variable to null. If you want to box the int, you could use:

.... null : (object)(int)toReturn.Rows[0]["BrokerGroupId"];

which would get that part of the statement to compile, but then if
_brokerGroupId is a variable of type int, the assignment part will
fail.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
> so... that answer your question...

No, actually, it doesn't. Because I'm not *implicitly*
converting the types, I'm *explicitly* converting them.
So the error makes no sense.

thnx
Christoph
Nov 16 '05 #12
Christoph Boget <jc*****@yahoo.com> wrote:
so... that answer your question...


No, actually, it doesn't. Because I'm not *implicitly*
converting the types, I'm *explicitly* converting them.
So the error makes no sense.


It makes perfect sense, as I showed before. The problem is that the
conversion that it's talking about isn't the one you're performing
explicitly.

I suggest that to make it easier to understand why the compiler's
complaining, you take your one big statement and break it down into
lots of little ones with local variables. You'll then be able to very
easily see where the error is, if you still can't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
You are assigning a SqlString to a regular String, and a SqlInt to a regular
Integer. These are your implicit conversions.

Telmo Sampaio

"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:Ox**************@TK2MSFTNGP10.phx.gbl...
so... that answer your question...


No, actually, it doesn't. Because I'm not *implicitly*
converting the types, I'm *explicitly* converting them.
So the error makes no sense.

thnx
Christoph

Nov 16 '05 #14
> I suggest that to make it easier to understand why the compiler's
complaining, you take your one big statement and break it down into
lots of little ones with local variables. You'll then be able to very
easily see where the error is, if you still can't.


I broke the code down into the following:

if( curRow["BrokerGroupId"] == System.DBNull.Value )
{
// Error is on the line below
retval[curRowNum].BrokerGroupId = null;
}
else
{
retval[curRowNum].BrokerGroupId = (int)curRow["BrokerGroupId"];
}

and the error I get is the one mentioned previously:

"Type of conditional expression can't be determined because there is no
implicit conversion between '<null>' and 'int'"

It's still not apparent to me why I can't set an int data type to null.
Or, if I can, how. Because this just isn't working. You mentioned
"boxing" in your previous email but I don't see how I can box in
the above situation.

thnx,
Christoph
Nov 16 '05 #15
Christoph Boget wrote:
I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"

So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.

thnx,
Christoph


http://www.devhood.com/tutorials/tut...utorial_id=454

The chart at the bottom of this article lists the implicit datatype
conversions.

Nov 16 '05 #16
Christoph Boget <jc*****@yahoo.com> wrote:
I suggest that to make it easier to understand why the compiler's
complaining, you take your one big statement and break it down into
lots of little ones with local variables. You'll then be able to very
easily see where the error is, if you still can't.
I broke the code down into the following:

if( curRow["BrokerGroupId"] == System.DBNull.Value )
{
// Error is on the line below
retval[curRowNum].BrokerGroupId = null;
}
else
{
retval[curRowNum].BrokerGroupId = (int)curRow["BrokerGroupId"];
}

and the error I get is the one mentioned previously:

"Type of conditional expression can't be determined because there is no
implicit conversion between '<null>' and 'int'"


I doubt that you get that error now, as there's no conditional
expression in the above code.
It's still not apparent to me why I can't set an int data type to null.
Because it's a value type. You *really* need to know the basics of .NET
such as the differences between value types and reference types before
getting into ADO.NET though - you'll save yourself a lot of headaches
like this.
Or, if I can, how. Because this just isn't working. You mentioned
"boxing" in your previous email but I don't see how I can box in
the above situation.


You can't if you're trying to assign to an object type variable, you
could box the int - but I don't think that's going to help you here.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #17
> Because it's a value type. You *really* need to know the basics of .NET
such as the differences between value types and reference types before
getting into ADO.NET though - you'll save yourself a lot of headaches
like this.


Fair enough. I just couldn't see any way other than setting the variable
to null to essentially "reset" the variable/property to nothing such that it
has no value.

thnx,
Christoph
Nov 16 '05 #18
Christoph Boget <jc*****@yahoo.com> wrote:
Because it's a value type. You *really* need to know the basics of .NET
such as the differences between value types and reference types before
getting into ADO.NET though - you'll save yourself a lot of headaches
like this.


Fair enough. I just couldn't see any way other than setting the variable
to null to essentially "reset" the variable/property to nothing such that it
has no value.


There's no such concept as setting a variable to have no value. Even
the null value is a perfectly good value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #19
Fair enough. I just couldn't see any way other than setting the variable to null to essentially "reset" the variable/property to nothing such that it has no value.

There's no such concept as setting a variable to have no value. Even
the null value is a perfectly good value.


So there is no way to "unset" a variable?

thnx,
Christoph
Nov 16 '05 #20
Christoph Boget <jc*****@yahoo.com> wrote:
There's no such concept as setting a variable to have no value. Even
the null value is a perfectly good value.


So there is no way to "unset" a variable?


No. Why would you want to?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #21
> > > There's no such concept as setting a variable to have no value. Even
the null value is a perfectly good value.

So there is no way to "unset" a variable?

No. Why would you want to?


Well, as it is no doubt apparent by now, I'm trying to conver Sql* data
types to c# data types. I'm doing this in a data access class that is
reading
(and writing) data to a datastore. When reading a new record, if that new
record has a NULL value in a column where the previous record had data,
I need a way to "unset" the value of the class' member variable that
represents
that column. And since I'm having issues converting, say, SqlString.Null to
a string (as was evidenced throughout this thread) I was just hoping that
there
was some other way to "unset" the variable.

thnx,
Christoph
Nov 16 '05 #22
Christoph Boget <jc*****@yahoo.com> wrote:
> There's no such concept as setting a variable to have no value. Even
> the null value is a perfectly good value.
So there is no way to "unset" a variable?

No. Why would you want to?


Well, as it is no doubt apparent by now, I'm trying to conver Sql*
data types to c# data types. I'm doing this in a data access class
that is reading (and writing) data to a datastore. When reading a new
record, if that new record has a NULL value in a column where the
previous record had data, I need a way to "unset" the value of the
class' member variable that represents that column. And since I'm
having issues converting, say, SqlString.Null to a string (as was
evidenced throughout this thread) I was just hoping that there was
some other way to "unset" the variable.


No - you need to have a value which represents NULL, basically -
whichis exactly what SqlInt.Null is for, but it only applies to the
SqlInt type.

The good news for you is that C# is going to get nullable value types
(I'm not sure at what performance/space penalty) - the bad news is that
Whidbey is still a fair way from being released.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #23

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

Similar topics

0
by: Terminal882003 | last post by:
Hi, Here I have a question about MSCommLib. I need to dynamically add activeX controls that requires run-time license for MSCommLib. The following is the actual code: AxMSCommLib.AxMSComm...
3
by: Anita C | last post by:
I have the foll. code to update the value of an attribute: xmlDocument.Load("abc.xml"); XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc); xmlAttrib.Value =...
1
by: Svyatoslav | last post by:
Hi, I have a problem with XmlNodes and my stack. It looks something like this: //declarations XmlNode node, new_node; Stack MyStack = new Stack(); //code MyStack.Push(node);
6
by: juli | last post by:
I declared: public delegate void PaintEventHandler(object objSender,PaintEventArgs pea); and this.Paint+=new PaintEventHandler(MyPaintHandler); and the: static void MyPaintHandler(object...
2
by: Jeff | last post by:
I get the following error: Cannot implicitly convert type 'Factory.Stack.pArrayStack' to 'Factory.Stack.StackDefs.ImStack' at compile time. I thought perhaps there was a mismatch between the...
2
by: Patrick Olurotimi Ige | last post by:
When i convert:- this code from VB to C# Why do i get error "Cannot implicitly convert type 'object' to 'bool' VB --- If cmdcommand.Parameters("ReturnValue").Value = 1 Then lblStatus.Text =...
9
by: Andy Sutorius | last post by:
Hi, I am receiving the error when compiling the project, "cannot implicitly convert type object to string". The error points to this line of code and underlines the dtrRecipient:...
3
by: Patrick Olurotimi Ige | last post by:
compiling the code below i get the error:- Cannot implicitly convert type 'object'to 'System.Xml.XmlDocument' I'm getting the error on this line:- myXml.Document =...
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
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
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
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.