473,779 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

In C#, how do I code 'this = that'?

I want to make a MenuItem that I can 'click' programmaticall y. I
started by coding this:

class ClickableMenuIt em : MenuItem {

public void DoClick( EventArgs e ) {
OnClick( e );
}

ClickableMenuIt em( MenuItem mi ) {
// What do I do here?!?
// this = mi; <--- does not work
}
}

As you can see, I have no idea of how to tell ClickableMenuIt em to take
all of its fields and events from MenuItem upon construction.

How would you do this?

Aug 21 '06 #1
14 1940
In inheritance, the sub-class *is intrinsically composed of* the base
class, so you can't pass another one into the ctor for it to absorb.
You could theoretically pass one in as a template to copy, but I don't
think this is what you are trying to achieve.
However, you also shouldn't need to do any of this...

Assuming you make ctors available on ClickableMenuIt em to mimic the
ctors of MenuItem, then it should be enough to simple create and add
ClickableMenuIt em instances to your menus, instead of MenuItem
instances. The downside is that this might confuse the IDE a bit,
maybe.

Marc

Aug 21 '06 #2
Charles Jenkins a écrit :
I want to make a MenuItem that I can 'click' programmaticall y. I started
by coding this:

class ClickableMenuIt em : MenuItem {

public void DoClick( EventArgs e ) {
OnClick( e );
}

ClickableMenuIt em( MenuItem mi ) {
// What do I do here?!?
// this = mi; <--- does not work
}
}

As you can see, I have no idea of how to tell ClickableMenuIt em to take
all of its fields and events from MenuItem upon construction.

How would you do this?
Hi,

Have you checked the method MenuItem.Perfor mClick in MSDN ?...

As for your solution, I think you are a bit confused because you want
both inheritance and composition : you will have to choose. If you
inherit from MenuItem, then why not create a ClickableMenuIt em instance
whenever you need one, instead of creating a MenuItem instance that will
only be used to initialize a new ClickableMenuIt em instance ?...

OK, I guess that is because you used the designer to create your menu,
and you could only create MenuItem instances this way... Then there
would be three workarounds :
* isolate your ClickableMenuIt em in a library and add it to the
designer controls. Use inheritance. The constructor would have no
parameters, and you could use it exactly like a MenuItem.
* create your ClickableMenuIt em elements "by hand" in your form
constructor, after the call to InitializeCompo nents(). Use inheritance.
You would not want any parameter in your constructor either.
* let the designer create your MenuItem. Then create your
ClickableMenuIt em elements "by hand" in your form constructor, after the
call to InitializeCompo nents(). Use composition (which in this case
seems to be a rather long and fastidious if you do it properly...).

Hope this helps.

Mathieu
Aug 21 '06 #3
On 2006-08-21 10:16:34 -0400, "Marc Gravell" <ma**********@g mail.comsaid:
Assuming you make ctors available on ClickableMenuIt em to mimic the
ctors of MenuItem, then it should be enough to simple create and add
ClickableMenuIt em instances to your menus, instead of MenuItem
instances. The downside is that this might confuse the IDE a bit,
maybe.

Marc
Your advice is perfect, but I have had severe problems lately in that
any custom conrol I create works just fine for a while, but sooner or
later, they always confuse the IDE. The VS 2005 Designer gets in a
state where it will no longer display the custom control (or any form
which contains it).

Therefore, I am trying to find a solution that allow me to use only
standard controls on my form, with no visual inheritance, and then add
the behaviors I actually need at runtime. I was hoping to be able to do
something like this:

// Convert the control the designer understands to the one I actually need
ClickableMenuIt em usefulMenuItem = new ClickableMenuIt em(
sadlyLimitedMen uItem );
usefulMenuItem. DoClick();

But, you are absolutely right: If the Visual Studio 2005 designer
didn't suck, using inherited controls in the IDE would be the correct
solution

Aug 21 '06 #4
Aside from Mathieu's great point on "PerformCli ck" ...

It seems that what you are trying to do is a decorator pattern (perhaps a
look at the pattern will clear thing sup a bit?)

Cheers,

Greg
"Charles Jenkins" <no************ @not.here.comwr ote in message
news:2006082110 040716807-notmyaddress@no therecom...
>I want to make a MenuItem that I can 'click' programmaticall y. I started by
coding this:

class ClickableMenuIt em : MenuItem {

public void DoClick( EventArgs e ) {
OnClick( e );
}

ClickableMenuIt em( MenuItem mi ) {
// What do I do here?!?
// this = mi; <--- does not work
}
}

As you can see, I have no idea of how to tell ClickableMenuIt em to take
all of its fields and events from MenuItem upon construction.

How would you do this?

Aug 21 '06 #5
On 2006-08-21 10:38:52 -0400, Mathieu Cartoixa
<mathieu.cartoi xa@_NO_hotmail_ SPAM_.comsaid:

Thanks for trying to help me!
Have you checked the method MenuItem.Perfor mClick in MSDN ?...
I am using the .NET Compact Framework, which seems to be designed for
frustration, becaue the method you NEED is never available. For
instance, MenuItem.Perfor mClick() does not exist in the Compact
Framework.

As for your solution, I think you are a bit confused because you want
both inheritance and composition : you will have to choose. If you
inherit from MenuItem, then why not create a ClickableMenuIt em instance
whenever you need one, instead of creating a MenuItem instance that
will only be used to initialize a new ClickableMenuIt em instance ?...

OK, I guess that is because you used the designer to create your menu,
and you could only create MenuItem instances this way... Then there
would be three workarounds :
Great guesses! You are really sharp!

Because the VS 2005 designer is not reliable for custom controls, I am
trying to put plain old controls (with no visual inheritance) onto my
forms, then add the behaviors I need at runtime. I need
ClickableMenuIt em to inherit from MenuItem so that it can call
OnClick(); but I need each new ClickableMenuIt em to get its state from
the MenuItem that is actually available. I want to be able to do
something like this:

// Use what we have to create what we actually need
ClickableMenuIt em cmi = new ClickableMenuIt em( menuItem );
cmi.DoClick();
* isolate your ClickableMenuIt em in a library and add it to the
designer controls. Use inheritance. The constructor would have no
parameters, and you could use it exactly like a MenuItem.
I think this would require the kind of inheritance that would lead me
to have problems with the designer. In order to add ClickableMenuIt ems
using the designer, I would have to create a ClickableContex tMenu,
right?
* create your ClickableMenuIt em elements "by hand" in your form
constructor, after the call to InitializeCompo nents(). Use inheritance.
You would not want any parameter in your constructor either.
* let the designer create your MenuItem. Then create your
ClickableMenuIt em elements "by hand" in your form constructor, after
the call to InitializeCompo nents(). Use composition (which in this case
seems to be a rather long and fastidious if you do it properly...).
These workarounds still require me to make the ClickableMenuIt em to get
its state (fields and event handlers) from the MenuItem I want to
impersonate. That leads me back to this question. Given that I cannot
say "this = that," how do I copy all the MenuItem's fields and events
from the MenuItem I have into the ClickableMenuIt em that I want?

Oddly enough, I really *did* attempt to combine inheritance and
composition, just like you guessed, in the hope that an inherited
control could gain access to the protected methods of the member
control's instance---

class ClickableMenuIt em : MenuItem {

private MenuItem m_mi;

public ClickableMenuIt em( MenuItem mi ) {
m_mi = mi;
}

public void DoClick( EventArgs e ) {
m_mi.OnClick( e );
}
}

Of course, that does not work. I can call OnClick() in my own instance
(where it would do no good) but not in m_mi (which has the handlers I
want to fire). That approach would have been wrong and very wasteful,
but if it had worked, I could have lived with it.

Have a great day, and thanks!

Aug 21 '06 #6
On 2006-08-21 12:13:55 -0400, "Greg Young"
<dr************ *******@hotmail .comsaid:
Aside from Mathieu's great point on "PerformCli ck" ...

It seems that what you are trying to do is a decorator pattern (perhaps
a look at the pattern will clear thing sup a bit?)
I have a book on C# Design Patterns, so I will do just as you say and
read up on the Decorator pattern.

Thanks!

Aug 21 '06 #7
Charles Jenkins wrote:
On 2006-08-21 10:16:34 -0400, "Marc Gravell" <ma**********@g mail.comsaid:
>Assuming you make ctors available on ClickableMenuIt em to mimic the
ctors of MenuItem, then it should be enough to simple create and add
ClickableMenuI tem instances to your menus, instead of MenuItem
instances. The downside is that this might confuse the IDE a bit,
maybe.

Marc


Your advice is perfect, but I have had severe problems lately in that
any custom conrol I create works just fine for a while, but sooner or
later, they always confuse the IDE. The VS 2005 Designer gets in a state
where it will no longer display the custom control (or any form which
contains it).
Are your custom controls and the containing controls in the same
project? The VS2003 designer had problems with that, so the VS2005
designer might have them, too. It typically happened when I had a user
control and one that contains it open inside the designer at the same time.
The solution was to factor the user controls into another library.

Also, whenever you encounter such an behavior, don't save anything,
close all open windows then and close VS. Then delete the bin and obj
folder. Reopen VS and recompile the solution. Should work fine again.

Its not a perfect solution, but I can live with this workaround since
VS2002 :-(
HTH,
Andy
Aug 21 '06 #8
Your advice is perfect, but I have had severe problems lately in that
any custom conrol I create works just fine for a while, but sooner or
later, they always confuse the IDE. The VS 2005 Designer gets in a
state where it will no longer display the custom control (or any form
which contains it).
I'm still using VS2003, but when I experience problems like this I find
that they are usually related to compilation of dependant assemblies.
Are your custom controls in a DLL that is then used by the project
experiencing the difficulties?

Come to think of it, I've even seen this behaviour when the custom
controls are defined in the same project. My solution: close all
designer windows without saving anything, close the solution, recompile
all solutions from the "bottom up": library DLLs, then control DLLs,
then data layer, then business layer, then UI. That seems to sort it
out.

Of course, VS 2005 may have different problems....

Aug 21 '06 #9
On 2006-08-21 12:51:12 -0400, "Bruce Wood" <br*******@cana da.comsaid:
I'm still using VS2003, but when I experience problems like this I find
that they are usually related to compilation of dependant assemblies.
Are your custom controls in a DLL that is then used by the project
experiencing the difficulties?

Come to think of it, I've even seen this behaviour when the custom
controls are defined in the same project. My solution: close all
designer windows without saving anything, close the solution, recompile
all solutions from the "bottom up": library DLLs, then control DLLs,
then data layer, then business layer, then UI. That seems to sort it
out.

Of course, VS 2005 may have different problems....
I have tried the shutdown/delete bin and obj/restart routine, and it
does not help; but your advice to manually rebuild each assembly in
turn is interesting. The next time I cannot avoid doing a custom
control, if the problem recurs, I will try that procedure.

Thank you, Bruce and Andreas!

Aug 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2209
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes is: > def f(this:that=other): > print this This means that f() has a 'this' parameter, of type 'that'. And 'other' is the default value.
8
1430
by: Bernd Fuhrmann | last post by:
Hi! I just tried to write a little program, but GCC (Mingw 3.3.1) refused to compile it. So I'd like to know if there's something wrong with my code or if it is a bug in GCC. ---snip--- namespace Namespace {} template <class TSomeType>
0
1636
by: Chua Wen Ching | last post by:
Hi there. I had couple of Java codes with me that i not sure how would i code this in C#. I always use jcreator without ide. So a bit confuse over c#. Okay!
2
8464
by: Chua Wen Ching | last post by:
Hi there. I had 3 questions to ask. 1) Public Event ButtonClicked(MessageBoxNumber As Integer, OKButton As Boolean, CancelButton As Boolean, ExitButton As Boolean)
3
2066
by: joshblair | last post by:
Hello, Has anyone ever seen or created such a code generator? I'm looking for a sample of a code generator that will generate code (preferably one that uses C# and the XMLTextWriter) to create an XML document structure based on an XML file as input. I have to build some classes that allow me to generate some very complex/large/nasty XML documents for use in B2B exchange of data (like
5
1438
thatos
by: thatos | last post by:
The following program was supposed to read a number of years from a file called "friday.in" then print out how many of the thirteenth were on each day from monday to sunday on to a file called "friday.out". Here is the code which I used . import java.io.*; import java.util.*; public class friday{ public static void main(String args) throws IOException{ BufferedReader in = new BufferedReader(new FileReader("friday.in"));...
9
1223
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, please reference: http://pastebin.com/mef78b3d As shown in the reference I am dynamically building my controls in my class. I'd like to be able to attach javascript calls as attributes to each of my controls if applies. for example, String varString = "alert('i'm a textbox')";
3
2595
by: Al Dykes | last post by:
Can some post a Python code fragment that will to make a PC with Skpye installed to make a Skype call, given a valid phone # string. I'm not asking for code that handles the audio once the connection is made.
0
9636
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9930
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5373
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2869
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.