|
Hi all
I have 4 classes. About 80% of there functions are common. So I make an abstract baseclass to inherit from and add any function that is common. The question is how should i declare the functions in the base class and how must i declare the functions that are not applicable in the inherited class. This is what I have come up with.
Option 1:
Abstract class:
protected void Function(){...}
Inherited class:
if needed:
public new Function(){ base.Function() } or
public new Function(){ ...implement...}
if not needed:
do nothing
Option 2:
Abstract class:
public void Function(){...}
Inherited class:
if needed:
do nothing if implementation is not different or
public override void Function(){...implement...}
if not needed:
private new void Function(){} //hide the member
What is the correct / suggested way?
ANy help appreciated
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
Rather than straight-out answer your questions I'll offer some suggestions.
Use an abstract base class when the base classes logically follow to inherit from it.
An example is the TextWriter class; it is an abstract class and all of the classes that inherit from it logically follow in the chain (XmlWriter, StreamWriter).
Now I will say that you should re-think the design of your base class if it turns out you have many methods that shouldn't be used by the inheriting classes.
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Thanx James
I think it needs some redesign too
How I wish for multiple inheritance...
Cheers
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
You could fake it like Eiffel does
given three classes A, B, and C; where A and B are unrelated but C inherits from A and B the Eiffel compiler will do something like this.
interface IA<br />
{<br />
... methods, properties, etc of A ...<br />
}<br />
<br />
interface IB <br />
{<br />
... methods, properties, etc of C ...<br />
}<br />
<br />
class C : object, IA, IB<br />
{<br />
private A a;<br />
private B b;<br />
<br />
public void MethodOfA()<br />
{<br />
a.MethodOfA();<br />
}<br />
<br />
void IA.MethodOfA()<br />
{<br />
a.MethodOfA();<br />
}<br />
<br />
....<br />
}
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
James T. Johnson wrote:
You could fake it like Eiffel does
What the hell is Eiffel, presumably nothin' to do with a tower in Paris.
The quote is about the only thing I understood from the post . Thanx though
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
leppie wrote:
What the hell is Eiffel
Eiffel is another programming language that has been ported to .NET.
Like C++ it supports multiple inheritence, but unlike MC++ they kept that feature in there.
But since .NET only supports single inheritence they got around that by making an interface for the classes that are being inherited and use interface implementation to fake multiple inheritence.
To the language it all looks the same but to the CLR it is just implementation of interfaces.
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
What do you have against Eiffel mate ?
A guy from Paris.
|
|
|
|
|
|
Thanx had a look, not sure how it is suppose to help me
It does indeed have something to do with that tower in Paris.
From Eiffel website:
When creating Eiffel in 1985, we decided to pay homage to one of the greatest engineers of all time: Gustave Eiffel, the man who built the eponymous Tower in Paris,...
CHeers
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
how to write a DNS query for finding Mail server at a site
in c#
|
|
|
|
|
You could try the System.Net.Dns class's Resolve method, but I doubt that will perform the MX record look-up needed to get the mail server given a domain name (ie you would have to do a look-up using the mail servers name).
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
|
How do I forcibly unload a .NET dll [the dll holds a global hook proc and thus it loads in the address space of any program that is run]
My problem is that even after unhooking the hook and closing the hook-setting program, the dll is still in use [I think the dll because of it's being a clr run dll is having probs unloading itself from non-clr program address spaces]
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Review by Shog9
Click here for review[NW]
|
|
|
|
|
The correct term is an Assembly
You can't explicitly unload an Assembly; but you can unload the AppDomain that the assembly is loaded in. Unfortunately I'm not sure how well this would work; but its worth a shot.
After you unhook the hook, make a call to AppDomain.CurrentDomain.Unload() . That may or may not work though; so be sure to catch the CannotUnloadAppDomainException exception
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
|
Consider this C++ code:
//Thread 1
HWND target = ::SendMessage(hWnd, UWM_HERE_I_AM, (WPARAM)m_hWnd);
//Thread 2
LRESULT CTheOtherApp::OnHereIAm(WPARAM wParam, LPARAM)
{
other = new CWnd;
other.Attach((HWND)wParam);
return (LRESULT)m_hWnd;
}
Q1: How would I "register" the OnHereIam handler in a C# Program? (I read about AddMessageFilter in MSDN but its rather vague to me.)
Q2: What would the "return" statement look like? I mean how do you "cast" LRESULT in C#?
Q3: I guess if there is a way to register the OnHereIam message handler I won’t need to write a custom WndProc. But if I have too write one how is this usually done?
Thanks!
|
|
|
|
|
|
ke5in wrote:
Q1: How would I "register" the OnHereIam handler in a C# Program? (I read about AddMessageFilter in MSDN but its rather vague to me.)
You are better off overriding WndProc for this. AddMessageFilter should be the last option because of the overhead involved of calling the added message filters for *every* message passed to the UI thread, where-as WndProc only gets called for messages passed to that particular window.
The On* methods are called by the WndProc, not through any sort of magic but by putting something such as a switch statement in the WndProc and test for each message then calling the appropriate On* method.
ke5in wrote:
Q2: What would the "return" statement look like? I mean how do you "cast" LRESULT in C#?
The return type should be IntPtr.
ke5in wrote:
Q3: I guess if there is a way to register the OnHereIam message handler I won’t need to write a custom WndProc. But if I have too write one how is this usually done?
protected override void WndProc( ref Message m )
{
const int UWM_HERE_I_AM = 0x1234;
switch( m.Msg )
{
case UWM_HERE_I_AM:
m.Result = OnHereIAm( m.WParam, m.LParam );
return;
}
base.WndProc( ref m );
} Normally you would follow the event architecture that .NET already provides (event, delegate, and protected On* method for inheritors to override).
HTH,
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
I tried emailing this but just got a bounce-back in return:
> In C# I can create a class for a thread and get it running.
> But the thing that has me confused is the “event” which
> occurs in the application. How do I notify the thread that
> this “event” has occurred using “the event architecture that
> .NET already provides”?
Here is how I would set it up (ignore capitalization, I'm typing in outlook so it likes to 'correct' me). There are two different ways of doing this, the first is used if its just a notification that something happened ("I was clicked", "I finished processing the task you already know about", etc), the second is used if you need to supply some data to the listener.
First the notification method:
class MyThread : ....
{
public event EventHandler MyEvent;
protected virtual void OnMyEvent(EventArgs e)
{
FireMyEvent(e);
}
private void FireMyEvent(EventArgs e)
{
if( MyEvent != null )
{
MyEvent(this, e);
}
}
private void SomeCode()
{
SomeLengthyProcessing();
SomeMoreProcessing();
OnMyEvent(EventArgs.Empty);
EvenMoreProcessing();
}
} Now in the code that needs to receive that event:
.....
MyThread mt = new MyThread();
mt.MyEvent += new EventHandler(myMethodToExecuteWhenMyEventHappens);
.....
private void myMethodToExecuteWhenMyEventHappens(object sender, EventArgs e) {
}
In the cases where you have data to be returned you use the same pattern, but the type names change a bit
class MyEventEventArgs : EventArgs
{
private int myValue;
public MyEventEventArgs( int myValue )
{
this.myValue = myValue;
}
public int MyValue
{
get { return myValue; }
}
}
class MyThread : ....
{
public delegate void MyEventEventHandler(object sender, MyEventEventArgs e);
public event MyEventEventHandler MyEvent;
protected virtual void OnMyEvent(MyEventEventArgs e)
{
FireMyEvent(e);
}
private void FireMyEvent(MyEventEventArgs e)
{
if( MyEvent != null )
{
MyEvent(this, e);
}
}
private void SomeCode()
{
SomeLengthyProcessing();
SomeMoreProcessing();
MyEventEventArgs e = new MyEventEventArgs(42);
OnMyEvent(e);
EvenMoreProcessing();
}
} Now in the code that needs to receive that event:
.....
MyThread mt = new MyThread();
mt.MyEvent += new MyEventEventHandler(myMethodToExecuteWhenMyEventHappens);
.....
private void myMethodToExecuteWhenMyEventHappens(object sender, MyEventEventArgs e) {
int value = e.MyValue;
} A lot of this seems like its overkill, but once you have used the framework for a while you begin to appreciate this style of event handling (its possible to attach 100 listeners to an event, where as posting to a thread's message queue only notifys that thread).
HTH,
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
how can i create Wizards in C#? is it still by using property sheets and pages? or something else?
can somebody point me to the right direction or maybe share some code if wrote it already.
thanks
|
|
|
|
|
what i meant was wizard like application using C#, maybe by using form?
|
|
|
|
|
I created mine by using a combination of Forms and UserControls.
I think I'll clean the code up and post an article over this weekend
James
"And we are all men; apart from the females." - Colin Davies
|
|
|
|
|
Ok, I am taking my first step into control development using C#. Actually I have not even created one with VB yet (my native language) so I guess I am a newbie all the way around as far as control making is concerned. I am having issues with two areas:
1) My control (to start simple) is nothing more than a label control placed upon a panel. I want to be able to set the text of the label control via a property. My code is as follows:
public override string Text
{
get
{
return lblInfoText.Text;
}
set
{
lblInfoText.Text = value;
}
}
This allows me to set the text of the control from the property list, but once I run the application the text is gone. I assume this is because nothing is persisiting the text that I have entered in to the ComponentInit area of the form code. How do I get that to happen?
2) Because I am deriving my control from a panel there are several properties that I get automaticialy, but I don't want to exspose all of them to the outside world because they don;t make sense in my control (IE: BackgroundImage, AllowDrop, Autoscroll*, etc...) How do I remove them from the property list? I assume that it is an attribute, but I am not sure which one. I have tried setting the browseable one to false and it dfoes not seem to work.
Thanks for any guidence that you can offer.
|
|
|
|
|
from MSDN : When you derive a component or control from a base component that has design-time attributes, your component inherits the design-time functionality of the base class. If the base functionality is adequate for your purposes, you do not have to reapply the attributes. However, you can always override attributes or apply additional attributes to the derived component. The following code fragment shows a custom control that overrides the Text property inherited from Control by overriding the BrowsableAttribute attribute applied in the base class.
public class MyControl : Control {
[Browsable(false)]
public override string Text {...}
...
}
|
|
|
|
|
So I have to override them just to set the browsable attribute to false?
UGH!
|
|
|
|