473,566 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(doubl e,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterf ace
{
void MovePoint(doubl e,double);
}
}
then create a Form inherit form MyWindow:
namespace ABC.Test.A
{
public AWindow:Form,AB C.Test.IMyWindo w
{
public void MovePoint(doubl e x,double y)
{
//do work
}
}
public BWindow:AWindow ,ABC.Test.IMyWi ndow
{
public void MovePoint(doubl e 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 2144
using System;
using System.Collecti ons;
public interface IA
{
void Add();
}
public class A:IA
{
public void Add()
{
Console.WriteLi ne("A:IA out put");
}
}
public class B:IA
{
public void Add()
{
Console.WriteLi ne("B:IA out put");
}
}
public interface IB
{
void Work();
}

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

public class D:System.Window s.Forms.Form,IA
{
public void Add()
{
Console.WriteLi ne("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.WriteLi ne(text.ToStrin g(), args);
}

private static void RL()
{
Console.ReadLin e();
}

private static void Break()
{
System.Diagnost ics.Debugger.Br eak();
}

#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(doubl e,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterf ace
{
void MovePoint(doubl e,double);
}
}
then create a Form inherit form MyWindow:
namespace ABC.Test.A
{
public AWindow:Form,AB C.Test.IMyWindo w
{
public void MovePoint(doubl e x,double y)
{
//do work
}
}
public BWindow:AWindow ,ABC.Test.IMyWi ndow
{
public void MovePoint(doubl e 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.micros oft.com... using System;
using System.Collecti ons;
public interface IA
{
void Add();
}
public class A:IA
{
public void Add()
{
Console.WriteLi ne("A:IA out put");
}
}
public class B:IA
{
public void Add()
{
Console.WriteLi ne("B:IA out put");
}
}
public interface IB
{
void Work();
}

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

public class D:System.Window s.Forms.Form,IA
{
public void Add()
{
Console.WriteLi ne("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.WriteLi ne(text.ToStrin g(), args); }

private static void RL()
{
Console.ReadLin e(); }

private static void Break() {
System.Diagnost ics.Debugger.Br eak();
}

#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.
????????????plu gin??,????????? ?:)

Hi,
I want to implement a common Form with special interface, such as
MovePoint(doubl e,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterf ace
{
void MovePoint(doubl e,double);
}
}
then create a Form inherit form MyWindow:
namespace ABC.Test.A
{
public AWindow:Form,AB C.Test.IMyWindo w
{
public void MovePoint(doubl e x,double y)
{
//do work
}
}
public BWindow:AWindow ,ABC.Test.IMyWi ndow
{
public void MovePoint(doubl e 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
1903
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
2327
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> refcount { ... // reference counting wrapper; like shared_ptr }; class foo {
8
5123
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
1846
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 provide certain functionality. In my form code, I'm not going to be sure of what types of controls the form will be using until run-time. So, I...
23
3490
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 the Inherit statement).
7
13641
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
2641
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 long long*)&ptr; As opposed to the more usual casting:
9
2451
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 have a derived class object and then upcast it to its base class, which cast operator should I use? Is it static_cast, or, can I simply cast it...
19
2214
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 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...
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.