470,590 Members | 2,091 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,590 developers. It's quick & easy.

how to casting a object inherit form Form to another object in c# 2.0

Hi,
I want to implement a common Form with special interface, such as MovePoint(double,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterface
{
void MovePoint(double,double);
}
}
then create a Form inherit form MyWindow:
namespace ABC.Test.A
{
public AWindow:Form,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public BWindow:AWindow,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public void Test()
{
BWindow oB = new BWindow();
AWindow oA = (AWindow)oB; //casting exception
IMyWindow oI = (IMyWindow)oB; //casting exception
AWindow oA = oB as AWindow; // 0k but it's null
IMyWindow oI = oB as IMyWindow;
//why oA == null and oI == null?????
if(oA != null)
{
oA.MovePoint(1.0d,3.0d);
}
if(oI != null)
{
oI.MovePoint(1.0d,2.3d);
}

}
}

thanks!

harvie

2006-4-29

Apr 29 '06 #1
2 2019
using System;
using System.Collections;
public interface IA
{
void Add();
}
public class A:IA
{
public void Add()
{
Console.WriteLine("A:IA out put");
}
}
public class B:IA
{
public void Add()
{
Console.WriteLine("B:IA out put");
}
}
public interface IB
{
void Work();
}

public class C:A,IB
{
new public void Add()
{
Console.WriteLine("C:A,IB out put");
}
public void Work()
{
Console.WriteLine("C:work");
}
}

public class D:System.Windows.Forms.Form,IA
{
public void Add()
{
Console.WriteLine("D out put");
}
}
public class MyClass
{
public static void Main()
{
D d = new D();
IA a = (IA)d;
a.Add();
d.Add();//ok
//d.Show();

A a = new A();
a.Add();
IA ia = (IA)a;
ia.Add();
B b = (B)ia;// compiler error
b.Add();// error
C c = new C();//ok
c.Add();
IA ic = (IA)c;
ic.Add();
IB ib = (IB)c;
ib.Work();
A ca = (A)c;
ca.Add();

RL();
}

#region Helper methods

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}

private static void RL()
{
Console.ReadLine();
}

private static void Break()
{
System.Diagnostics.Debugger.Break();
}

#endregion
}
说明可以将*类直接强制转换为父类 并调用父类的接口;
we can cast the child class to parent class, and call the parent class's
methods.
但是如果强制转换父类为*类的话, 使能够转换,也会发生问题(这个在C ++*大家都明白的道理);
it's dangerous to cast a child class to parent class.
也可以把*类强制转化为父类的接口 ,并调用接口类*声明的接口;
we can cast a child class to parent interface, and can invoke the interface.
如果是通过Asembly提供的反射机制生成 类在进行强制转换的时候,在C#2* 编译错误,C#1*还没有测试,有兴 的也可以看看;
it throw compiler error information in C# 2.0, when i cast a class which
created by assembly reflect.
接下来的问题就是*究一下plugin技术 看看人家怎么实现的了:)

Hi,
I want to implement a common Form with special interface, such as
MovePoint(double,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterface
{
void MovePoint(double,double);
}
}
then create a Form inherit form MyWindow:
namespace ABC.Test.A
{
public AWindow:Form,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public BWindow:AWindow,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public void Test()
{
BWindow oB = new BWindow();
AWindow oA = (AWindow)oB; //casting
exception
IMyWindow oI = (IMyWindow)oB; //casting
exception
AWindow oA = oB as AWindow; // 0k but it's
null
IMyWindow oI = oB as IMyWindow;
//why oA == null and oI == null?????
if(oA != null)
{
oA.MovePoint(1.0d,3.0d);
}
if(oI != null)
{
oI.MovePoint(1.0d,2.3d);
}
}
}
thanks!

harvie

2006-4-29

Apr 29 '06 #2
> public class B:IA ...
A a = new A();
a.Add();
IA ia = (IA)a;
ia.Add();
B b = (B)ia;// compiler error
b.Add();// error
You are trying to convert an A to a B .. an A is not a B and therefor fails
...

B is always an IA
an IA is not always a B

an A is never a B
"harvie wang" <qu*****@gmail.com> wrote in message
news:5e**************************@news.microsoft.c om... using System;
using System.Collections;
public interface IA
{
void Add();
}
public class A:IA
{
public void Add()
{
Console.WriteLine("A:IA out put");
}
}
public class B:IA
{
public void Add()
{
Console.WriteLine("B:IA out put");
}
}
public interface IB
{
void Work();
}

public class C:A,IB
{
new public void Add()
{
Console.WriteLine("C:A,IB out put");
}
public void Work()
{
Console.WriteLine("C:work");
}
}

public class D:System.Windows.Forms.Form,IA
{
public void Add()
{
Console.WriteLine("D out put");
}
}
public class MyClass
{
public static void Main()
{
D d = new D();
IA a = (IA)d;
a.Add();
d.Add();//ok
//d.Show();

A a = new A();
a.Add();
IA ia = (IA)a;
ia.Add();
B b = (B)ia;// compiler error
b.Add();// error
C c = new C();//ok
c.Add();
IA ic = (IA)c;
ic.Add();
IB ib = (IB)c;
ib.Work();
A ca = (A)c;
ca.Add();

RL();
}

#region Helper methods

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args); }

private static void RL()
{
Console.ReadLine(); }

private static void Break() {
System.Diagnostics.Debugger.Break();
}

#endregion
}
????????????????,????????;
we can cast the child class to parent class, and call the parent class's
methods.
???????????????,??????,??????(???C++?????????);
it's dangerous to cast a child class to parent class.
?????????????????,????????????;
we can cast a child class to parent interface, and can invoke the
interface.
?????Asembly?????????????????????,?C#2??????,C#1?? ????,?????????;
it throw compiler error information in C# 2.0, when i cast a class which
created by assembly reflect.
????????????plugin??,??????????:)

Hi,
I want to implement a common Form with special interface, such as
MovePoint(double,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterface
{
void MovePoint(double,double);
}
}
then create a Form inherit form MyWindow:
namespace ABC.Test.A
{
public AWindow:Form,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public BWindow:AWindow,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public void Test()
{
BWindow oB = new BWindow();
AWindow oA = (AWindow)oB; //casting
exception
IMyWindow oI = (IMyWindow)oB; //casting
exception
AWindow oA = oB as AWindow; // 0k but it's
null
IMyWindow oI = oB as IMyWindow;
//why oA == null and oI == null?????
if(oA != null)
{
oA.MovePoint(1.0d,3.0d);
}
if(oI != null)
{
oI.MovePoint(1.0d,2.3d);
}
}
}
thanks!

harvie

2006-4-29


Apr 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by JustSomeGuy | last post: by
3 posts views Thread by Andy Lomax | last post: by
8 posts views Thread by Serv Lau | last post: by
4 posts views Thread by Chris | last post: by
23 posts views Thread by Ren Nordby | last post: by
7 posts views Thread by S. Lortan | last post: by
7 posts views Thread by William S Fulton | last post: by
9 posts views Thread by Jess | last post: by
19 posts views Thread by jan.loucka | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.