|
Hi,
how can I call the default constructor of a class from an overloaded one? Example:
public class MainForm : System.Windows.Forms.Form {<br />
public MainForm() {<br />
InitializeComponent();<br />
PrepareSomeStuff();<br />
SomeMoreStuff();<br />
}<br />
<br />
public MainForm(string Somestring) {<br />
mSomestring = Somestring;<br />
InitializeComponent();<br />
PrepareSomeStuff();<br />
SomeMoreStuff();<br />
}<br />
}
Any ideas are welcome.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
|
Thats what I was looking for. But still one question: Is the code of the default ctor executed before or after execution of the code in the overload.
Nevermind. I go figure out myself
Thanks for your reply.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
You can use the base keyword. Here is an example:
public class BaseClass
{
public BaseClass()
{
Console.WriteLine("Hello from base");
}
}
public class DerivedClass : BaseClass
{
public DerivedClass() : base()
{
Console.WriteLine("Hello from derived");
}
public DerivedClass(string s) : base()
{
Console.WriteLine("Hello from derived");
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
I actually knew this and I was not looking for a way of doing this in a base/derived class hierarchy. Sorry if I couldn't make myself understood in my initial post. My quest was rather how to call the default ctor of the same class from an overload. Thanks anyway for replying.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
The way I've always done this kind of thing is to create an initialise method and call that. So your code would look like:
public class MainForm : System.Windows.Forms.Form
{
public MainForm()
{
InitialiseCommon();
}
public MainForm(string Somestring)
{
mSomestring = Somestring;
InitialiseCommon();
}
private void InitialiseCommon()
{
InitializeComponent();
PrepareSomeStuff();
SomeMoreStuff();
}
}
At least this was the way I was taught to do it in my C++ lectures years and years ago when I was at university. I've just updated the idea for C# and it works nicely.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
You can just stick all of your initilization code within your base class and in your derived class call base within the constructor. This way, if your InitialiseCommon method changed names you would only have to change it in your base class, not in every class that it is called. See my post for an example.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Nick Parker wrote:
You can just stick all of your initilization code within your base class and in your derived class call base within the constructor
My understanding was that it was common functionality within the same class, no inheritance involved (although the example given was possibly misleading in this respect as it was inheriting from the Form class, it wasn't part of what was needed.)
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
I didn't catch that he didn't want to use inheritance.
- Nick Parker My Blog | My Articles
|
|
|
|
|
I've done this as well. Was just wondering whether there is a cleaner way of doing it in C#.
Thanks for your reply.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
It's really a matter of consistency vs. performance. Putting all the common code in your default ctor and calling that first will ensure that your code is always code and you only need to change it in one place. If you use public ClassName(string name) : this() , it results in two whole extra IL instructions!
Get the idea?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everyone,
I am trying to do two things with the RTF control that I thought would be simple.
Firstly when the user presses the return key, the contents of the textbox is sent to be displayed, however this causes the text insertion cursor to move down one line, and stay there. I can't seem to get it move, even using Clear(); and ResetText();
if (e.KeyCode == Keys.Return)
{
if (this.rtfCompose.Text.Length != 0)
{
// Clear
this.rtfCompose.Clear();
this.rtfCompose.ResetText();
}
}
However that doesnt move the cursor back up to 0,0.
Also, when the user presses Return, the text gets sent to another RTF control for display. However, once the text goes beyond the size of the control, everything that's added cannot be seen unless the user scrolls. How would it be possible to scroll the existing text up and keep the new text (Added to the bottom) in view?
Thanks!
|
|
|
|
|
You might want to mention in which event your checking for the return key. This should actually be processed in the control's ProcessDialogKey method - which fires before the rich textbox recieves it. This means you have to extend the RichTextBox class and override that method. Don't forget to read the documentation about it, however, instead of just using it. There's things you need to understand about it, including when you override a control method you typically call it on the base class at some point (especially if it's not a message, character, whatever that you want to handle - the base class may need to process it).
As far as scrolling to the bottom, this is actually quite easy but you need to P/Invoke SendMessage and SetScrollPos , which are documented in MSDN[^]. You first send WM_VSCROLL (0x0115) using SendMessage to the other RichTextBox , using it's Handle property (the HWND for that window), along with the scroll information as defined in the WM_VSCROLL documentation. You may also need to call SetScrollPos to set the scroll position for the RichTextBox .
One final approach is to use ScrollToCaret , a public method defined by TextBoxBase (the base class for TextBox and RichTextBox ). Several people have had problems getting this to work and have had to use the approach above; as such, this has been covered here in the forums in the past, so if you'd like to read more about these threads, click "Search comments" immediately above the message board and try searching (I recommend using the keyword "WM_VSCROLL").
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
> You might want to mention in which event your checking for the return key.
I'm using the KeyDown event.
Overriding ProcessDialogKey is something I will research into. However P/Invoking and more advanced things like that are pretty new to me, so it'll take some learning about the basics before I'll be able to do that.
Thanks!
|
|
|
|
|
Hi all~
I would like to ask
I have written a dll by MFC
I would like to written a C# library as interface to use this MFC dll
How to achieve this result?
Is there anything need to change in the MFC code?
Thanks
|
|
|
|
|
Either go through all the problem of adding class factories to your unmanaged MFC DLL in order to use the CallingConvention.ThisCall calling convention with the DllImportAttribute (see a previous post here[^] for the gory details), or - much more easily - write an assembly using Managed C++. In a mixed-mode MC++ assembly, you still have access to all the "unmanaged" C/C++ APIs, including your MFC classes. Wrap those in managed (__gc ) classes and compile your MC++ DLL with the /clr switch. This will produce an assembly that can be used by other managed projects, since all managed code compiles down to Intermediate Language (IL).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks~
I would like one more question
If the arguement type of a method in MFC dll is "LPCTSTR"
how do I specify the method in C# using this way?
#region DLLImports
[DllImport("test.dll")]
private static extern Set(?? str); // ?? is the "LPCTSTR" in MFC dll
#endregion
Thanks
|
|
|
|
|
It's a string , but it's a platform-dependent string; ANSI in Windows, and Unicode in Windows NT.
So change the DllImportAttribute like so:
[DllImport("test.dll", CharSet=CharSet.Auto)]
private static extern dont-forget-the-return-value Set(string str); Also, unless this is an exported function, what you have won't work. Methods of classes defined in MFC must use the ThisCall CallingConvention so that the first argument (the class pointer) is pushed onto the stack so that the method is executed on that pointer.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
HI~
if the return type is "LSCTSTR"
then is the return type is "string" in C# dll?
but it throws "NotSupportException"
Thanks~
|
|
|
|
|
Yes, but you must make sure that 1) CharSet.Auto is set in the DllImportAttribute and that 2) you use CallingConvention.ThisCall for methods (functions defined on a class). There's many reasons a NotSupportedException could be thrown, so you're just going to have to read those links I gave you before to articles about P/Invoke.
At least having a basic understanding is important to solving your problem.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
HI Heath Stewart ~
I have one more question ^.^
If I have implement a MFC dll which is used to show a string on the screen. The dll contain a variable which is stored a string.
Then I write a C# program to invoke the "set" method to set the string variable for the dll.
After a run the C# program, the variable has been updated. However, the string on the screen does not change. I know the dll needs to be refreshed. How to do this?
OnPaint() or using SendMessage() may be used to refresh the screen. But how?
Thank you for answering the questions!
|
|
|
|
|
The C# code shouldn't refresh the screen - the MFC implementation should handle that, which means instead of setting a variable you should call a method (like SetText ); the method implementation should set the variable and invalidate the client region. It's a matter of encapsulation, and this is a much better class design - the class itself should take care of what it manages.
In order to repaint the client region, all you need to do is invalidate it. CWnd::Invalidate would be a logical choice.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
yes, I just use C# program call the method inside the dll.
The method in the dll is set the value of a member variable.
The dll in fact is a screen plugin. The member variable may be a string that is to be shown on the screen.
I would like to change the member variable so that the screen will also be changed to achieve showing a message dynamically.
I can change the member variable but the screen would be change. How can I solve this?
Is the CWnd::Invalidate already existed in the CWnd class?
Or it is needed to be implemented and how to implement it?
|
|
|
|
|
As I said before, if the MFC class encapsulates the string variable, then it should also encapsulate the functionality to update the screen.
If you want to know more about CWnd , look it up on MSDN[^] or ask in the VC++ forum.
Microsoft MVP, Visual C#
My Articles
|
|
|
|