473,387 Members | 1,834 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,387 software developers and data experts.

converting from base class [quite long]

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
Nov 17 '05 #1
10 1793
Andrzej Kaczmarczyk wrote:

<snip>
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?


It is compiling because it's perfectly possible (as far as the compiler
knows) for every element in table.Columns to be a MyDataColumn. It just
happens that it's not true here.

You need to use one of your previous solutions.

Jon

Nov 17 '05 #2
or he could test each column:

DataTable table;
DataColumn column;
MyDataColumn myColumn;

for(int i = 0; i < table.Columns.Count; i++)
{
myColumn = table.Columns[i] as MyDataColumn;
if(myColumn != null)
{
// do stuff
}
}

HTH

Ollie Riches
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Andrzej Kaczmarczyk wrote:

<snip>
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?


It is compiling because it's perfectly possible (as far as the compiler
knows) for every element in table.Columns to be a MyDataColumn. It just
happens that it's not true here.

You need to use one of your previous solutions.

Jon

Nov 17 '05 #3
Andrzej,

See inline:
I have a wrapper class around it.

public MyDataColumn : DataColumn
{
public bool MyFlag;
}
This isn't a wrapper class. You have extended the DataColumn class. If
you wrapped it, you would have an instance of the DataColumn inside of the
MyDataColumn class.

I'd like to do something like this:

DataTable table;
foreach (MyDataColumn column in table.Columns) {
// do something;
}
The only way to do this is to construct the table from scratch. You
will have to add the instances of your type (MyDataColumn) to the columns
collection (exposed through the Columns property) by calling the overload of
the Add method which takes a DataColumn instance. You should then be able
to fill this DataTable normally.

Later, when accessing the columns, you will have to cast to your type in
order to use your type's specific properties.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

---

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

Nov 17 '05 #4
Ollie Riches wrote:
or he could test each column:

DataTable table;
DataColumn column;
MyDataColumn myColumn;

for(int i = 0; i < table.Columns.Count; i++)
{
myColumn = table.Columns[i] as MyDataColumn;
if(myColumn != null)
{
// do stuff
}
}


As I understand it, none of the columns in table.Columns actually
*will* be a MyDataColumn - he just wanted to do the conversion
automatically. Of course, I could have missed what's going on...

Jon

Nov 17 '05 #5
good point :)

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Ollie Riches wrote:
or he could test each column:

DataTable table;
DataColumn column;
MyDataColumn myColumn;

for(int i = 0; i < table.Columns.Count; i++)
{
myColumn = table.Columns[i] as MyDataColumn;
if(myColumn != null)
{
// do stuff
}
}


As I understand it, none of the columns in table.Columns actually
*will* be a MyDataColumn - he just wanted to do the conversion
automatically. Of course, I could have missed what's going on...

Jon

Nov 17 '05 #6
Well, the OP could always place his own DataColumn-derived type into the
table in the first place by calling the Add overload on
DataColumnCollection, passing his instance of MyDataColumn in the first
place.

Then, all of the DataColumn instances will be MyDataColumn, and he can
loop through the columns with a foreach as he wishes.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Ollie Riches wrote:
or he could test each column:

DataTable table;
DataColumn column;
MyDataColumn myColumn;

for(int i = 0; i < table.Columns.Count; i++)
{
myColumn = table.Columns[i] as MyDataColumn;
if(myColumn != null)
{
// do stuff
}
}


As I understand it, none of the columns in table.Columns actually
*will* be a MyDataColumn - he just wanted to do the conversion
automatically. Of course, I could have missed what's going on...

Jon

Nov 17 '05 #7
Thx for all aneswer, pretty fast you're :)

Jon, you're right none of the columns are MyDataColumns, I want the
conversion to happen implicitly. IMO however it should not compile, if the
pregenerated converters don't work, and they never will.

.... just tested something ...
DataTable table = new DataTable();
foreach ( int a in table.Columns ) {
// do something
}
this will compile as you say, I was sure it wouldn't :/
anyway,
Nicholas you're right it is an extension not wrapping (the wrapper is the
previous way I did that) I mistyped.

and also you are right about the solution:
I did the following

MyDataTable table
{
DataTable table; // wrapping. implicit conversion, and so on
private List<MyDataColumn> columns; // small optimize
public List<MyDataColumn> Columns{
if (columns == null) {
columns = new List<MyDataColumn>();
foreach (DataColumn col in this.table.Columns) {
this.colums.Add(new MyDataColumn(col)); // explicit conversion don
in single place
}
}
return this.columns;
}
}
this way I am now able to do:
DataTable table;
MyDataTable myTable = table;
foreach (MyDataColumn column in myTable.Columns)
{ // do something }

it DOES move the problem one level higher in the hierarchy but doing the
same steps for table and moving it one further step to DataSet effectively
reduces the number in-code casts to single occurence of

DataSet dataSet;
MyDataSet myDataSet = dataSet;

foreach (MyDataTable table in myDataSet.Tables) {
foreach (MyDataColumn column in table.Columns) {
// do something
}
}

CUIN Kaczy
Nov 17 '05 #8
> Well, the OP could always place his own DataColumn-derived type into
the table in the first place by calling the Add overload on
DataColumnCollection, passing his instance of MyDataColumn in the first
place. Can't do that. The dataset is a typed one, which means it is pregenerated by
VisualStudio, I am left with some kind of wrapping/inheriting to access them
and extend.

Then, all of the DataColumn instances will be MyDataColumn, and he can
loop through the columns with a foreach as he wishes.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

CUIN Kaczy
Nov 17 '05 #9
Andrzej Kaczmarczyk <ak**********@visualsystems.com.pl> wrote:
Thx for all aneswer, pretty fast you're :)

Jon, you're right none of the columns are MyDataColumns, I want the
conversion to happen implicitly. IMO however it should not compile, if the
pregenerated converters don't work, and they never will.


How exactly would the compiler know that? The IEnumerable interface
doesn't say what kind of object it's going to return...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
Jon, you're right none of the columns are MyDataColumns, I want the
conversion to happen implicitly. IMO however it should not compile, if
the
pregenerated converters don't work, and they never will.
How exactly would the compiler know that? The IEnumerable interface
doesn't say what kind of object it's going to return...


Good point, I forgot foreach is a layer over IEnumerable. Somehow it occured
to me that it is an array of know types or a list of know types ...

Following that hole, I though that compiler should know all available
conversions ...
Following that hole, why it does create the conversion that can't be correct
(the default conversion cannot create chiold object from base, simply
because it doesn't know what the child will be, for that one would need
explict wrapping)
Following that hole, this mechanism forbids me from doing conversion becuase
there is already conversion that by definition can't be correct.

But yes, you're right...

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


CUIN Kaczy
Nov 17 '05 #11

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

Similar topics

9
by: Tom Evans | last post by:
My basic question: If I have a specific interface which I know is going to be implemented by a number of classes, but there is no implementation commonality between them, what is the preferred...
3
by: Michael | last post by:
Hi All, I need a little help converting a class to vb.net. I have the following to convert: internal class BBBSDIForm : BBBForm { public BBBSDIForm(FormInfo fi, FormMgr formMgr) : base(fi,...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
59
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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,...

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.