473,406 Members | 2,220 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,406 software developers and data experts.

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 2133
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
3
by: Andy Lomax | last post by:
I'm using a library where the supplier has provided base class 'foo', and a reference counting class which wraps 'foo': ------------------- library code ----------------------- template<class T>...
8
by: Serv Lau | last post by:
Consider the following code: struct X { float f; }; struct Y { struct X x; }; void func(struct X *x) {} int main(void) { struct Y y; func(&y);
4
by: Chris | last post by:
I created a control that derives from the System.Windows.Forms.Control namespace. I then created an interface to which this new control must adhere, so that I could require future controls to...
23
by: Ren Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
7
by: S. Lortan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
7
by: William S Fulton | last post by:
I'm looking for the name of the following casting style in order to do some reading around on it and why it is sometimes used. unsigned long long ull = 0; void * ptr = 0; ull = *(unsigned...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
19
by: jan.loucka | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.