Operator Overloading?


J

JP Wrye

Hello All,

I'm wondering if the following is possible. I'm explicitly overloading
SqlParameter, but get the following error when I try to convert to and from
arrays.

Cannot convert type 'EntityPersistence.DataFieldParameter[]' to
'System.Data.SqlClient.SqlParameter[]'

Thanks,
JP
 
Ad

Advertisements

P

Patrik Löwendahl [C# MVP]

How does your overloading code look like?

Sounds like you're trying to do an invalid cast in the operator logic.
 
J

JP Wrye

static public explicit operator SqlParameter(DataFieldParameter
dataFieldParameter)

{

SqlParameter _sqlParameter = new SqlParameter();

_sqlParameter.DbType = dataFieldParameter.DbType;

_sqlParameter.Direction = dataFieldParameter.Direction;

_sqlParameter.ParameterName = dataFieldParameter.ParameterName;

_sqlParameter.Precision = dataFieldParameter.Precision;

_sqlParameter.Scale = dataFieldParameter.Scale;

_sqlParameter.Size = dataFieldParameter.Size;

_sqlParameter.SourceColumn = dataFieldParameter.SourceColumn;

_sqlParameter.SourceVersion = dataFieldParameter.SourceVersion;

_sqlParameter.Value = dataFieldParameter.Value;



return _sqlParameter;

}



----------------------------------------------------------------------------
-------



DataFieldParameter _dataFieldParameter = new DataFieldParameter();

DataFieldParameter[] _dataFieldParameters =
EntityBuilder.GetParameters(obeject);



SqlParameter _sqlParameter = (SqlParameter)_dataFieldParameter; //Works.

SqlParameter[] _sqlParameter = (SqlParameter[])_dataFieldParameters;
//Doesn't Work.









Patrik Löwendahl said:
How does your overloading code look like?

Sounds like you're trying to do an invalid cast in the operator logic.

--
Patrik Löwendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"

JP Wrye said:
Hello All,

I'm wondering if the following is possible. I'm explicitly overloading
SqlParameter, but get the following error when I try to convert to and from
arrays.

Cannot convert type 'EntityPersistence.DataFieldParameter[]' to
'System.Data.SqlClient.SqlParameter[]'

Thanks,
JP
 
J

Jon Skeet [C# MVP]

JP Wrye said:
I'm wondering if the following is possible. I'm explicitly overloading
SqlParameter, but get the following error when I try to convert to and from
arrays.

Cannot convert type 'EntityPersistence.DataFieldParameter[]' to
'System.Data.SqlClient.SqlParameter[]'
Just because a conversion is possible between type A and type B doesn't
mean there's a conversion between A[] and B[]. You'll need to create a
new array and copy the entries from one into the other.
 
Ad

Advertisements

P

Patrik Löwendahl [C# MVP]

The type SqlParameter and SqlParameter[] is not treated as equal types.

SqlParameter[] is an array with ElementType of SqlParameter, that differs
from the type SqlParameter.

You will have to explicitly write code for that conversion.

--
Patrik Löwendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"


JP Wrye said:
static public explicit operator SqlParameter(DataFieldParameter
dataFieldParameter)

{

SqlParameter _sqlParameter = new SqlParameter();

_sqlParameter.DbType = dataFieldParameter.DbType;

_sqlParameter.Direction = dataFieldParameter.Direction;

_sqlParameter.ParameterName = dataFieldParameter.ParameterName;

_sqlParameter.Precision = dataFieldParameter.Precision;

_sqlParameter.Scale = dataFieldParameter.Scale;

_sqlParameter.Size = dataFieldParameter.Size;

_sqlParameter.SourceColumn = dataFieldParameter.SourceColumn;

_sqlParameter.SourceVersion = dataFieldParameter.SourceVersion;

_sqlParameter.Value = dataFieldParameter.Value;



return _sqlParameter;

}



-------------------------------------------------------------------------- --
-------



DataFieldParameter _dataFieldParameter = new DataFieldParameter();

DataFieldParameter[] _dataFieldParameters =
EntityBuilder.GetParameters(obeject);



SqlParameter _sqlParameter = (SqlParameter)_dataFieldParameter; //Works.

SqlParameter[] _sqlParameter = (SqlParameter[])_dataFieldParameters;
//Doesn't Work.









Patrik Löwendahl said:
How does your overloading code look like?

Sounds like you're trying to do an invalid cast in the operator logic.

--
Patrik Löwendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"

JP Wrye said:
Hello All,

I'm wondering if the following is possible. I'm explicitly overloading
SqlParameter, but get the following error when I try to convert to and from
arrays.

Cannot convert type 'EntityPersistence.DataFieldParameter[]' to
'System.Data.SqlClient.SqlParameter[]'

Thanks,
JP
 

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

Similar Threads

operator overloade? 5
operator overloading 1
overload for "operator == " 1
overloading operator== 4
Operator Overloading 1
Operator overloading 3
Overloading operator++ 4
operator overloading ! 0

Top