|
Suppose I have Class:
1) class Shape{}
2) class Square : Shape {}
3) class Circle : Shape {}
and Method:
1) void f(Shape s){}
2) void f(Square s){}
3) void f(Circle c){}
Now, I declare 3 variables:
1) Shape a1 = new Shape();
2) Square a2 = new Square();
3) Circle a3 = new Circle();
If I call f(a1), it will go to method 1.
If I call f(a2), it will go to method 2.
If I call f(a3), it will go to method 3.
Right?
But If I call f((Shape)a2), it will go to method 1.
Is there anyway to make if I call f((Shape)a2), and it will go to method 2 directly?
|
|
|
|
|
I'm not sure what you want to do, but here's my two cents:
If your parameter is cast to Shape, and Square is derived from Shape, first the runtime / compiler will look in the class Square if it can find a method with the f(Shape s) signature.
Since it can only find the f(Square s) method, it'll go up on class (which is Shape) and there it WILL find the wanted menthod.
If you want class Square to have it's own f(Shape s) method, you'll have to overload the original one, i.e. recreate the f(Shape s) method in the Square class...
|
|
|
|
|
Actually the method f is not a member method of Shape, Square, Circle.
It is static method of another class, which can takes "Shape" or "Square" or "Circle" as parameter.
Actually, what I am doing now is:
public static f(Shape s){
if (s is Square){
f((Square)s);
}
else if (s is Circle){
f((Circle)s);
}
}
public static f(Square s){
MessageBox.Show("It is a Square"):
}
public static f(Circle s){
MessageBox.Show("It is a Circle"):
}
It works, but the problem is when there is a lot of derived class, the f(Shape c) method will be very bulky.
|
|
|
|
|
I Really don't understand what you're trying to do here. WHy define a static class where you process objects that have nothing to do with that class (it is static, so the class that holds the static method can't do anything with the passed object???? The whole idea behind OO is that you put things that belong together in a class, so if you want to edit a Square, use the f in Square.
Then, there's the "virtual" keyword as well, if you define f as virtual in Shape and pass it a Square it will try to locate the f method in Shapes subclasses.....
|
|
|
|
|
Why write 3 different overloads of the method if you are casting all the parameters to Shape anyway.
The whole point of method overloads is that you can theoretically pass it any kind of Shape derived object(or whatever for that matter) and the runtime will work out the right overload to call. Maybe I am not clearly understanding what you are trying to do, but this seems a little bit silly.
|
|
|
|
|
Seems to me you have some misunderstanding about the concepts:
First of all the methods "f" belong to which class(es)?
Second: The typecast (shape)a2 is redundant (don´t remember if it is syntactically ok but if so it has nonsense anyway)a2 is a Square so it IS a Shape anyway
how inheritance and polymorphism works...
I guess you wanted to do something like this:
You have the classes and their respective methods:
class Shape<br />
{<br />
virtual void f{} to treat the Shape object (Whatever it be) who owns it<br />
}<br />
<br />
class Square : Shape <br />
{<br />
override void f{}
<br />
}<br />
<br />
class Circle : Shape <br />
{<br />
override void f{}
}
if you do
Shape a1 = new Shape();
Square a2 = new Square();
Circle a3 = new Circle();
Logically,
a1.f() will call the Shape´s f method; a2.f() the Square´s, etc
BUT ALSO if you do this....( and that´s the nice part)
Shape a1 ;<br />
Square a2 = new Square();<br />
Circle a3 = new Circle();<br />
a1= a2;<br />
a1.f();
a1= a3;<br />
a1.f();
which is something similar to what you said and is the usual way in which these kind of things are accomplished
This happens because the fmethod is declared virtual in Shape class an is overriden in the child classes otherwise, if they weren´t virtual then the calls of a1.f() always would call the Shape´s f method
Notice this approach is specially useful in this case
Class TestShapes{
void DoSomethingWithShapes(Shape s)
{
s.f();
//it will call the proper f method regarding if s is in fact a Square or a Circle or a general Shape (if this class is conceived to have instances ,thing that generally shouldn´t be taht way)
}
}
Maybe it helps
|
|
|
|
|
Hello all,
My requirement is to add icons to the ContextMenu items. Any ideas how this can be achieved using C# ? There are lot many articles about how to associate icons with menus but none talk about context menus. Any help would be appreciated.
Thanks and regards,
Amit
|
|
|
|
|
In the standard Contextmenu you can't add an Image. You'll either have to write a derived class with it's own OnDraw method or build an Enxtender.
Or you download one of the existing open source menu / Context Menu systems, like Magic Library or Sandbar...
|
|
|
|
|
I am writing a form-based program with 2 forms: main & child.
The main form call the child form as:
frmChild child = new frmChild();
child.ShowDialog();
The problem is that in the child form, I want to change some properties of the main form (like button.Text, labels, etc). I also want to call some functions declared in the frmMain class.
I tried to use the Intelli Sense of VS in child, but when I type frmMain., it doesn't show up both the form main controls and the public functions declared in the frmMain class.
Can anyone intruct me how to do this ?
Thank you very much for your help.
|
|
|
|
|
assume your parent forms class is "MyParentForm"
what you want to do in your child for is this:
<br />
MyParentForm frmParent = (MyParentForm)this.Parent<br />
this way you get to all the properties and public fields/methods of your parent form
|
|
|
|
|
Sorry but it doesn't work at all.
VS didn't complain anything during compilation. However, during runtime, I got an exception saying:
Object reference not set to an instance of an object.
The watch window shows that frmParent (in your example) is an undefined value.
============
assume your parent forms class is "MyParentForm"
what you want to do in your child for is this:
MyParentForm frmParent = (MyParentForm)this.Parent
this way you get to all the properties and public fields/methods of your parent form
|
|
|
|
|
when you declare the child form in the parent form you must initialize the parent property of the child form before calling show() or showdialog()
EG. ChildForm frm = new ChildForm() ;
frm.parent = this ;
frm.ShowDialog() ;
|
|
|
|
|
The Parent object is not available in the Constructor yet (because it won't be set until the ShowDialog method is called). I usually set my references to the parent form in the Layout Event and add a LayoutCalled bool to the class so the references will only be set once....
|
|
|
|
|
Thanks for your advice, gurus
It's working now with a few tweaks.
|
|
|
|
|
Hello,
I have a question.
I'm develop chat tool.
I usually use MSMessenger. The send message dialog of this have two areas, write message board and message log board.
And the bordor move smoothly.
I would like to develop this function using C#.
I look for some controls, try to several way, but I couldn't.
If you have some solutions, please tell me on detail.
regards,
yu-yu
|
|
|
|
|
Is the Splitter control what you're looking for?
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Hello John,
Yes, like Splitter control.
But VS.NET 2003 don't have splitter control of Up-Down.
I found it of Right-Left.
yu-yu,
|
|
|
|
|
Hi,
I have one question, how can I get the logic drive space infomation?
As my program will do some file copy functions, so I need to check whether it has enough space to paste. I hope to know the how much it remains and its total space..
I've checked the Directory class, no this method I can use, so could anyone help me?
Thank you.
|
|
|
|
|
You can choose to use the native Win32 API functions through P/Invoke. Or you can go the modern and more elegant way of using the Windows Management interface classes of the .NET framework.:
<br />
using System.Management.Instrumentation;<br />
using System.Management;<br />
<br />
ManagementClass mcDriveClass = new ManagementClass("Win32_LogicalDisk");<br />
ManagementObjectCollection mocDrives = mcDriveClass.GetInstances();<br />
foreach(ManagementObject moDrive in mocDrives)<br />
{<br />
<br />
if (int.Parse(moDrive.Properties["DriveType"].Value.ToString()) == 3 || int.Parse(moDrive.Properties["DriveType"].Value.ToString()) == 4)<br />
{<br />
String sDeviceId = moDrive.Properties["DeviceId"].Value.ToString();<br />
long dSize = long.Parse(moDrive.Properties["Size"].Value.ToString());<br />
long dFree = long.Parse(moDrive.Properties["FreeSpace"].Value.ToString());<br />
}<br />
}<br />
mocDrives.Dispose();<br />
mcDriveClass.Dispose();<br />
For more info on the Win32_LogicalDisk management class see
Win32_LogicalDisk class @ MSDN[^]
|
|
|
|
|
Thanks,
but I don't know why I can't include System.Management namespace.
Actually, is it no any build-in class that we can use to get the volume of drive?
do you have any example about using Win32 API for this problem?
|
|
|
|
|
you need to add the reference to System.Management.dll
Right click on "References" in Solution Explorer choose "Add Reference" and find the dll in the list. I don't see why you would want to use the unmanaged way as it is less elegant then the .NET way but you need to use GetLogicalDriveStrings function from kernel32.dll and GetDiskFreeSpaceEx function also from kernel32.dll. Look these up on MSDN.
|
|
|
|
|
Hi All,
I have a C# Windows Form, which contains a DataGrid, and one of the column in the DaatrGrid is Checkbox.
What I need to do is - when checkbox is checked, all other checkboxes should be unchecked.
For ex: if there are 10 rows in DataGrid, and Row - 2 has checkbox checked. Now when "Checkbox column of Row - 5" is clicked, it should un-check "Checkbox column of Row - 2" . Please let me know how can I capture check_event and loop through all rows.
Any help or pointers
Thanks in Advance
Ruchi
|
|
|
|
|
Actually, it's not a CheckBox . A check box is merely drawn by the DataGridBoolColumn using either ControlPaint.DrawMixedCheckBox or ControlPaint.DrawCheckBox , depending on whether or not the column allows nulls (DBNull ).
If you want to use an actual CheckBox and expose the event (better to encapsulate the CheckBox and expose only what's needed), you'll have to derive your own DataGridColumnStyle . See the documentation for that class in the .NET Framework SDK, which also includes a sample using a DateTimePicker control.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Oh, I almost forgot: if you're using a DataSet or DataTable for binding, you can handle the DataTable.ColumnChanging or DataTable.ColumnChangeed event and then enumerate the DataRow</coe>s in the <code>DataTable and change what you need accordingly.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks so much for your response. I will work towards this pointer
Ruchi
|
|
|
|