.net - Runtime TypeCasting in C# -


ide: vs2010, c# .net 4.0

i wondering there way typecast object in way variable passed in eg:

#code block 1       private void rearrangeshapelocation(shape currentcontrol, double scaleprecentageheight, double scaleprecentagewidth)     {         //here need type cast control according type          //example code         int cx = (currentcontrol rectangleshape).location.x;         int cxo = (currentcontrol ovalshape).location.x;           //computation code.     }   

here shape class available in c# namespace

microsoft.visualbasic.powerpacks   

and rectangleshape , ovalshape child classes of shape class.

so in above code if pass

rearrangeshapelocation(somrectrangleshape1,14,34); rearrangeshapelocation(somovelshape1,15,15);   

as here passing rectangle , oval shape in same function location in function (see code block #1), need type cast shape object accordingly.

is there way in c# typecast shape object according child class whatever pass in rearrangeshapelocation function.

#code block 1       private void rearrangeshapelocationdynamic(shape currentcontrol, double scaleprecentageheight, double scaleprecentagewidth)     {         //here need type cast control according type          //example code         int cx = (currentcontrol typeof(currentcontrol)).location.x;      }       

i know can done using switch case don't want. know in c# might done using reflaction, don't know.

this not necessary , point of polymorphism. class derived shape have respective properties declared in shape. coordinates (which assume part of shape class) doesn't matter kind of shape being passed method.

you can cast base object specialized object, if sure object has correct type.

for example works:

shape r = new rectangleshape(); rectangleshape r1 = r rectangleshape(); 

while doesn't:

shape p = new polygonshape(); rectangleshape p1 = p rectangleshape(); 

i noticed shape class not define coordinates, simpleshape class does, derived shape. rely on location property in code, need make sure shape simpleshape.

you can change method declaration taking simpleshape parameter:

private void rearrangeshapelocation(simpleshape currentcontrol, double scaleprecentageheight, double scaleprecentagewidth) {     int cx = currentcontrol.location.x; }   

or can leave , throw exception if wrong passed:

private void rearrangeshapelocation(shape currentcontrol, double scaleprecentageheight, double scaleprecentagewidth) {     if (!(currentcontrol simpleshape))         throw new argumentexception("currentcontrol not simpleshape");      int cx = (currentcontrol simpleshape).location.x; }   

or extend method handling lineshape well:

private void rearrangeshapelocation(shape currentcontrol, double scaleprecentageheight, double scaleprecentagewidth) {     int cx = 0;      if (currentcontrol simpleshape)     {         cx = (currentcontrol simpleshape).location.x;     }     else if (currentcontrol lineshape)     {         cx = (currentcontrol lineshape).x1;     } }   

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -