A
Andrzej Kaczmarczyk
Hi C# gurus
I have a conversion problem.
I have a class that I can't change - it's a DataColumn class;
I have a wrapper class around it.
public MyDataColumn : DataColumn
{
public bool MyFlag;
}
I'd like to do something like this:
DataTable table;
foreach (MyDataColumn column in table.Columns) {
// do something;
}
---
previously I had following construct:
public MyDataColumn {
private DataColumn wrappedColumn;
private MyDataColumn(DataColumn column)
{ this.wrappedColumn = column; }
public static implicit MyDataColumn(DataColumn column)
{ return new MyDataColumn(column); }
}
this way I was able to do the following:
DataTable table;
foreach (DataColumn column in table.Columns) {
MyDataColumn wrapped = column; // no explicit conversion required
// do something
}
----
however I thought that it would be more elegant to inherit directly (as in
the first example). The problem is obvious, the code compile without
problems (there are predefined converters from base to inheriting class when
you define class.) However it will not execute also obvious how can a base
constructor build a child class object.
The solution would be to use the converters, but here the compiler prostest,
that it can't compile a converter when there is one present it doesn't
matter if this is explicit or implicit converter, according to standard
(found somewhere on the web), one cannot define a conversion from a base
class.
Does anyone know how to solve it? If the issue is unsolvable why is it
compiling?
thanks,
CUIN Kaczy
I have a conversion problem.
I have a class that I can't change - it's a DataColumn class;
I have a wrapper class around it.
public MyDataColumn : DataColumn
{
public bool MyFlag;
}
I'd like to do something like this:
DataTable table;
foreach (MyDataColumn column in table.Columns) {
// do something;
}
---
previously I had following construct:
public MyDataColumn {
private DataColumn wrappedColumn;
private MyDataColumn(DataColumn column)
{ this.wrappedColumn = column; }
public static implicit MyDataColumn(DataColumn column)
{ return new MyDataColumn(column); }
}
this way I was able to do the following:
DataTable table;
foreach (DataColumn column in table.Columns) {
MyDataColumn wrapped = column; // no explicit conversion required
// do something
}
----
however I thought that it would be more elegant to inherit directly (as in
the first example). The problem is obvious, the code compile without
problems (there are predefined converters from base to inheriting class when
you define class.) However it will not execute also obvious how can a base
constructor build a child class object.
The solution would be to use the converters, but here the compiler prostest,
that it can't compile a converter when there is one present it doesn't
matter if this is explicit or implicit converter, according to standard
(found somewhere on the web), one cannot define a conversion from a base
class.
Does anyone know how to solve it? If the issue is unsolvable why is it
compiling?
thanks,
CUIN Kaczy