Cannot implicitly convert type

C

Christoph Boget

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
 
T

Telmo Sampaio

Christopher,

What are the defined data types of BrokerName and BrokerId?

Telmo Sampaio
 
J

Jon Skeet [C# MVP]

Christoph Boget said:
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.
 
C

Christoph Boget

What are the defined data types of BrokerName and BrokerId?

'string' and 'int', respecitvely.

Christoph
 
C

Christoph Boget

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
 
J

Jon Skeet [C# MVP]

Christoph Boget said:
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.
And 'String.Null' is an object? Not a data type?

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

Christoph Boget

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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 said:
Christoph Boget said:
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.
 
K

Kyril Magnos

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 said:
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
 
J

Jon Skeet [C# MVP]

Christoph Boget said:
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.
 
C

Christoph Boget

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
 
J

Jon Skeet [C# MVP]

Christoph Boget said:
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.
 
T

Telmo Sampaio

You are assigning a SqlString to a regular String, and a SqlInt to a regular
Integer. These are your implicit conversions.

Telmo Sampaio
 
C

Christoph Boget

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
 
A

Adlai Stevenson

Christoph said:
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/tutorial_details.aspx?tutorial_id=454

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

Jon Skeet [C# MVP]

Christoph Boget said:
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.
 
C

Christoph Boget

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
 
J

Jon Skeet [C# MVP]

Christoph Boget said:
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.
 
C

Christoph Boget

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
 

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