Groups | Blog | Home
all groups > dotnet general > july 2007 >

dotnet general : Casting base class to inherited one


Bruce Wood
7/30/2007 8:52:59 PM
[quoted text, click to view]

Who told you that? It's certainly not true. C# allows you to write
your own conversion operator, either implicit or explicit, that copies
the members from your base class object into a new, derived class
object, and return the derived object.

The only hitch, of course, is that you end up with a copy, but there's
no way around that.
Bruce Wood
7/30/2007 9:43:56 PM
[quoted text, click to view]

Could you post the code you wrote? Both your definition of the
implicit conversion operator and your use of it?
Bruce Wood
7/30/2007 9:51:01 PM
[quoted text, click to view]

Forget it. I just read the C# spec on line, here:

http://msdn2.microsoft.com/en-us/library/aa664464(VS.71).aspx

It says quite clearly that I'm wrong: you cannot provide implicit or
explicit conversions either up or down the class hierarchy.

Which means that the only option left open to you is a copy
constructor: your derived class must have a constructor that accepts a
member of your base class, like this:

public class Derived : Base
{
public Derived(Base baseObject)
{
...
}
}

or, you could make it a static method in your derived class:

public static Derived DerivedFromBase(Base baseObject) { ... }

Bummer about the conversions, though. I guess it causes some sort of
problem within the CLR....
jan.loucka NO[at]SPAM gmail.com
7/31/2007 12:00:00 AM
[quoted text, click to view]

This is my inherited class:

public class IntraMapsLayer : layerObj
{
public IntraMapsLayer(superMapObj map) : base (map)
{
this.currentSuperMapObj = map;
}

public static implicit operator IntraMapsLayer(layerObj layer)
{
IntraMapsLayer newLayer = new IntraMapsLayer(this.map);
newLayer.bandsitem = layer.bandsitem;
newLayer.classitem = layer.classitem;
newLayer.connection = layer.connection;
newLayer.connectiontype = layer.connectiontype;
newLayer.data = layer.data;
newLayer.debug = layer.debug;
newLayer.dump = layer.dump;
//newLayer.extent = layer.extent;
newLayer.filteritem = layer.filteritem;
newLayer.footer = layer.footer;
newLayer.group = layer.group;
newLayer.header = layer.header;
//newLayer.index = layer.index;
newLayer.labelangleitem = layer.labelangleitem;
newLayer.labelcache = layer.labelcache;
newLayer.labelitem = layer.labelitem;
newLayer.labelmaxscale = layer.labelmaxscale;
newLayer.labelminscale = layer.labelminscale;
newLayer.labelrequires = layer.labelrequires;
newLayer.labelsizeitem = layer.labelsizeitem;
//newLayer.map = layer.map;
newLayer.maxfeatures = layer.maxfeatures;
newLayer.maxscale = layer.maxscale;
//newLayer.metadata = layer.metadata;
newLayer.minscale = layer.minscale;
newLayer.name = layer.name;
//newLayer.numclasses = layer.numclasses;
//newLayer.numitems = layer.numitems;
//newLayer.numjoins = layer.numjoins;
//newLayer.numprocessing = layer.numprocessing;
newLayer.offsite = layer.offsite;
newLayer.plugin_library = layer.plugin_library;
newLayer.plugin_library_original = layer.plugin_library_original;
newLayer.postlabelcache = layer.postlabelcache;
newLayer.requires = layer.requires;
newLayer.sizeunits = layer.sizeunits;
newLayer.status = layer.status;
newLayer.styleitem = layer.styleitem;
newLayer.symbolscale = layer.symbolscale;
newLayer.template = layer.template;
newLayer.tileindex = layer.tileindex;
newLayer.tileitem = layer.tileitem;
newLayer.tolerance = layer.tolerance;
newLayer.toleranceunits = layer.toleranceunits;
newLayer.transform = layer.transform;
newLayer.transparency = layer.transparency;
newLayer.type = layer.type;
newLayer.units = layer.units;
return newLayer;
}


this implicit operator does not even compile. I haven't mentioned it
yet but we're bound to .NET 1.1 unfortunatelly
jan.loucka NO[at]SPAM gmail.com
7/31/2007 12:00:00 AM
[quoted text, click to view]

I didn't get what was the copy constructor supposed to do - can u
explain it to me?
Scott M.
7/31/2007 1:07:33 AM
[quoted text, click to view]

So does VB .NET 2005.

jan.loucka NO[at]SPAM gmail.com
7/31/2007 3:47:32 AM
Hi,
We're building a mapping application and inside we're using open
source dll called MapServer. This dll uses object model that has quite
a few classes. In our app we however need to little bit modify come of
the classes so they match our purpose better - mostly add a few
methods etc.
Example: Open source lib has classes Map and Layer defined. The
relationship between them is one to many.
We created our own versions (inherited) of Map called OurMap and our
version of Layer called OurLayer. The only difference between them is
that they have some extra methods (because we need them to do little
bit extra).
Is there any way how can I convert the base class (either Map or
Layer) to our inherited one (OurMap or OurLayer)?
I know that implicit or explicit conversion operators don't work
between inherited and base class but I was just wondering if there is
another way how to do this or if there's something wrong with our
object design?
Thanks for any help
jan.loucka NO[at]SPAM gmail.com
7/31/2007 4:00:03 AM
[quoted text, click to view]

When I try that the compiler complains:
C:\MapServer\IntraMaps60\SpatialEngineWS\Layers
\IntraMapsLayer.cs(208): 'DMS.IntraMaps.IntraMapsLayer.implicit
operator DMS.IntraMaps.IntraMapsLayer(layerObj)': user-defined
conversion to/from base class
I googled it and I found some articles that you cannot implicitly
convert from base class to it's child.
Am I doing something wrong?
AddThis Social Bookmark Button