|
I have 3 classes
class A{
public virtual void M(){
Console.WriteLine( "A" );
}
}
class B:A{
public virtual void M(){
Console.WriteLine( "B" );
}
}
class C:B{
public virtual void M(){
Console.WriteLine( "C" );
}
}
Now i want to call from C:M() the method of A -
How can i do it ?
|
|
|
|
|
Cast C as A :
((A)C).M();
|
|
|
|
|
In case that my type declaration is
A a = new A();
A b = new B();
A c = new C();
Your solution will not work.
You can check it if you want.
|
|
|
|
|
I checked : if you declare your 3 variables as A (as you wrote it), then the three following lines
a.M();
b.M();
c.M();
return these lines :
A
A
A
Then if you try these commands :
((A)a).M();
((B)b).M();
((C)c).M();
you get the following :
A
B
C
Anyway, as told by other members, it is really a poor design, and maybe you should consider changing the way you deal with your requirements.
Edit: this is true only if you declare M methods as virtual rather than override.
modified on Thursday, September 2, 2010 10:46 AM
|
|
|
|
|
Assuming those are actually overridden (You missed the 'override' keyword), you can't. That's the point of overriding a method.
Even if you cast it as the base class, it'll still call the overriding one on the child class.
If you NEED to call the base class, then you'll have to change your design.
EDIT: Actually, there may be a way to hack around it with reflection, but that would be REALLY bad practice.
|
|
|
|
|
Sorry, i forgat to add the ''override' keyword.
But the virtual table contain an pointer to all the base classes method - so why i cant ?
|
|
|
|
|
yes, but where does it point when you do an override?
|
|
|
|
|
Because it leads to unpredictable behaviour. As each override is a specialisation of the method it derives from, it makes on sense to do this; which base would you be interested in, the class it derives from, the one that derives from, how about object?
|
|
|
|
|
You can't or you shouldn't, however you can:
public class C : B{
public void BaseM(){
base.M();
}
public override void M(){
}
}
Also:
public interface ISillyInterface1{
void M();
}
public interface ISillyInterface2{
void M();
}
public class A : ISillyInterface1, ISillyInterface2{
void ISillyInterface1.M(){
}
void ISillyInterface2.M(){
}
}
|
|
|
|
|
Your question is not clear.. What you want to do? Your code just replaces the base-class methods without using the "new" keyword...
When your method M is an override of Method M in class B and C: what you want, is not possible without changing the design. But if you want to "replace" the method you can through casting: Look at the following example.
using System;
namespace CallBaseClass
{
class Program
{
static void Main(string[] args)
{
A a = new A();
A b = new B();
A c = new C();
a.M();
b.M();
c.M();
((A)a).M();
((B)b).M();
((C)c).M();
Console.ReadKey();
}
}
class A
{
public virtual void M()
{
Console.WriteLine("A");
}
}
class B : A
{
public new void M()
{
Console.WriteLine("B");
}
}
class C : B
{
public new void M()
{
Console.WriteLine("C");
}
}
}
|
|
|
|
|
That would be a poor design and is not allowed in C#.
|
|
|
|
|
class A {
public virtual void M(bool pDoBaseCall) {}
}
class B : A {
public override void M(bool pDoBaseCall) {
if (pDoBaseCall) {
base.M(pDoBaseCall);
} else {
}
}
}
class C : A {
public override void M(bool pDoBaseCall) {
if (pDoBaseCall) {
base.M(pDoBaseCall);
} else {
}
}
}
C c = new C();
c.M(true);
c.M(false);
You can achieve a finer level of control with an enum instead of a bool, or even dynamic code which determines depth of inheritance on run time... But this design hurts many good practices and principles, I recommend going back to drawing board and try again...
|
|
|
|
|
I have an aspx page. In that page I am displaying div when click on link. Div contains iframe and i am assigning url to the frame like below.
<asp:linkbutton id="lkbtn" text="Create" onclientclick="return ShowDiv(this)" runat="server">
<asp:label text="Create" runat="server" forecolor="Chocolate" font-bold="true">
| X
|
|
// Function to Show the Div //
function ShowDiv(CtrlName)
{
document.getElementById('divUserCtrl').style.display = 'inline';
frames['ifrm'].window.location = "CreateProject.aspx?ProjCat=G";
return false;
}
when click on linkbutton can able to display div and redircting to CreateProject page. But in CreateProject when click on a button i need to close this div. How can I do this in c#. I tried like Page.RegisterStartupScript("Reload", "window.close();"); But no luck
|
|
|
|
|
You may get an answer here but you will be more likely to succeed here[^]. If you decide to post there, link to your question above so you don't get flamed for cross posting!
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
dear all
how to scale or zoomx and zoomy for one image size? for example, my image size is 1624, 1224, but my pictureBox is 680,580, how to scale and zoomx and zoomy, make sure all image will be centered and display in the pictureBox. thanks
|
|
|
|
|
Try google search[^].
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.aktualiteti.com
|
|
|
|
|
Hi,
You should look at the SizeMode property of the PictureBox class.
This is really a simple issue, you should look for properties of objects you're using before posting on this forum. If you give yourself a little effort, people here will be more likely to help you.
Regards.
|
|
|
|
|
Could someone tell me please, what is the difference between the following two blocks of code? When would you assign an initial value for a field in the field definition and when would you do it in a constructor?
public class MyClass
{
private int myField;
public MyClass()
{
myField = 123;
}
}
public class MyClass
{
private int myField = 123;
public MyClass()
{
}
}
Thanks in advance
|
|
|
|
|
Hi Dewald,
It's more a matter of design choice, since there's no real functional difference between both your propositions.
Personnaly I'd rather use the first one. I only assign values to constants at declaration. I find this way much clearer (maybe because I always do this way). Moreover, it allows consistence of design if you want to set up the following :
public class MyClass
{
private int myField;
public MyClass() : this(123) {}
public MyClass(int field)
{
myField = field;
}
}
Perhaps you can try both ways, compile, and see what is the difference in the generated IL Code (with the IL disassembler). I never did such a test myself.
Hope this helps. Regards.
|
|
|
|
|
First option, as it allows me to put a breakpoint on the assignment
I are Troll
|
|
|
|
|
I always use the first method for the resons given already and also I may have several constuctors which may require a different initial value to be set e.g.
public class MyClass
{
public const int DefaultMyField = 123;
private int myField;
public MyClass()
: this(DefaultMyField)
{ }
public MyClass(int myField)
{
this.myField = myField;
}
}
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
I tend toward the first as well, for consistency.
public class X
{
int y ;
int z ;
public X
(
int y
)
{
this.y = y ;
this.z = 42 ;
return ;
}
}
z could be initialized where it's declared, but y can't, so I initialize z in the same place y is initialized.
|
|
|
|
|
The following part of the C# language spec may be of some interest:
§17.4.5 Variable initializers (skipped the example)
The default value initialization described in §17.4.3 occurs for all fields, including fields that have variable
initializers. Thus, when a class is initialized, all static fields in that class are first initialized to their default
values, and then the static field initializers are executed in textual order. Likewise, when an instance of a
class is created, all instance fields in that instance are first initialized to their default values, and then the
instance field initializers are executed in textual order. When there are field declarations in multiple partial
type declarations for the same type, the order of the parts is unspecified. However, within each part the field
initializers are executed in order.
All of this happens before any code in the ctor is run, as detailed in:
§17.4.5.2 Instance field initialization
The instance field variable initializers of a class correspond to a sequence of assignments that are executed
immediately upon entry to any one of the instance constructors (§17.10.2) of that class. The variable
initializers are executed in the textual order in which they appear in the class declaration. The class instance
creation and initialization process is described further in §17.10.
|
|
|
|
|
dear all
i am a newbe, this is a basic issue. i want to get a form's width height, and other property in another form. so how i can achive this point? thanks.
|
|
|
|
|
Hi,
Your question is not clear. Could you tell us what you've done so far to get what you want, and which issues you encountered ?
|
|
|
|