|
You have not told the compiler how the classes related to each other.
grivix wrote: // How do you call from here // the method mb() of the class B ?
As you have writtent the code, you don't. Instances of class B and C are not fields of class A so there is no parent/child relationship, the reference to B and C will go out of scope at the end of the static Main() method.
Make the b and c fields of class A . Move the Main() method to its own Program class because it seems out of place in A . (I don't know what A is supposed to do, but I'd say with 99.99% certainty the Main() method does not belong there).
Somewhere in class A you can construct b and c with a reference to this . Classes B and C have a field of type A . In the constructors of B and C you can now store the reference to the parent (A ) as it was passed in to the constructor.
Now, in the method mc() you can reference A (the parent) which can reference B .
I'll leave the question of encapsulation and information hiding as an exercise to you (because you generally shouldn't be using public fields)
Does this help?
|
|
|
|
|
pay attention to everything Colin said, and then remember that in C# the class that is inherited from is called the "base" class, not the "parent" class. The "parent/child" relationship is a design-level description of the flow of data, whereas the actual implementation is not "parent/child" but "base/derived"
...whenever you get the kinks worked out of your logic, remember that when you are wanting to call an appropriate method in the base class, you reference it as "base.Method();"
|
|
|
|
|
Alaric_ wrote: then remember that in C# the class that is inherited from is called the "base" class, not the "parent" class. The "parent/child" relationship is a design-level description of the flow of data, whereas the actual implementation is not "parent/child" but "base/derived"
Actually, I think he was describing an association rather than generalisation. Of couse, I could be wrong because his description was rather hazy. I had actually written half the reply before thinking that he was actually wanting that rather than derivation because of the way he wanted to use class B from class C.
|
|
|
|
|
I'm trying to make work my program with Colin's recommendations, which are very clear.
What i wanted to mean with "parent/child relationship" is the relation of an instance object (child) with the object where the instance is created as a field (parent). This is a different concept of base/derived relationship.
Thanks Alaric
Let the life to be fluent in you. SER
|
|
|
|
|
Your recommendatios are very helpful.
Thanks Colin
Let the life to be fluent in you. SER
|
|
|
|
|
A bit more of a complete answer to the one I gave before. I've repeated my previous description and annotated the source code. The numbers refer to the description below.
Of course, I may be wrong about what you are trying to achieve as from your description I think you are talking about an association parent/child relationship. But you may, as it has been pointed out, be talking about generalisation (classes deriving from base classes).
public class Program
{
public static void Main()
{
A a = new A();
a.DoStuff();
}
}
public class A
{
private B b;
private C c;
public A()
{
this.b = new B(this);
this.c = new C(this);
}
public void DoStuff()
{
c.mc();
}
public void CallMB()
{
this.b.mb();
}
}
public class B
{
private A a;
public B(A a)
{
this.a = a;
}
public void mb()
{
Console.WriteLine("I'm in class B");
Console.Read();
}
}
public class C
{
private A a;
public C(A a)
{
this.a = a;
}
public void mc()
{
this.a.CallMB();
}
}
As you have writtent the code, you don't. Instances of class B and C are not fields of class A so there is no parent/child relationship, the reference to B and C will go out of scope at the end of the static Main() method.
(1) Make the b and c fields of class A.
(2) Move the Main() method to its own Program class because it seems out of place in A. (I don't know what A is supposed to do, but I'd say with 99.99% certainty the Main() method does not belong there).
(3) Somewhere in class A you can construct b and c with a reference to this.
(4) Classes B and C have a field of type A.
(5) In the constructors of B and C you can now store the reference to the parent (A) as it was passed in to the constructor.
(6) Now, in the method mc() you can reference A (the parent) which can reference B.
(7) I'll leave the question of encapsulation and information hiding as an exercise to you (because you generally shouldn't be using public fields)
|
|
|
|
|
This is a very academic answer. I couldn't ask more.
Thanks Colin
|
|
|
|
|
Please anybody can give the XML FAQs for interviews.
Thanks in advance
RR
rr
|
|
|
|
|
FAQ #1: How can I find XML FAQs?
Answer: JFGI ( Just Fracking Google It)
|
|
|
|
|
This is probably a very silly question. I am opening a form(b) from within another form(a) and I am trying to get a variable from form(a) and display its contents in form(b). But I don't know how? I will also be updating the variables in form(a) from form(b)?
I have searched for ideas, but not sure what to search for?
Thanks in advance,
Phil
"Rules are for the obedience of fools and the guidance of wise men"
|
|
|
|
|
Create a public property in form B that sets the variable in form B
from Form B to A is more tricky, or you'll need to pass the object A itself to B or you'll have to create your own event.
thus in form A you'll have something like:
MyForm B = new MyForm();<br />
B.MyProperty = variablefromA;<br />
B.showDialog();
to pass the object A to form B you could pass it through the constructor (MyForm B = new MyForm(this); )
I hope for you this isn't homework...?
V.
Stop smoking so you can: enjoy longer the money you save.
|
|
|
|
|
There are generally two methods used:
1. Send a reference to form A to the constructor of form B, and store the reference in form B. Then you can access form A using the reference.
2. Make the variables in form A static, then you can access them from anywhere by specifying the class name of the form. This of course limits you to never create more than one instance of the form.
---
b { font-weight: normal; }
|
|
|
|
|
Take a look at the following article[^].
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
You beat me to it!
|
|
|
|
|
|
Hi
if I publish a version there is a version number, which increments each publish circle.
I'd like to show this number in an info-box.
But how to get it? (it ist not the Application.ProductVersion. This vesion is the number of the AssemblyInfo.cs)
Thanks
Ariadne
|
|
|
|
|
Hi ariadne,
Is this the version number you are looking for?
Assembly thisAssembly = Assembly.GetExecutingAssembly();
Console.WriteLine(thisAssembly.GetName().Version);
needs using System.Reflection;
HTH
Russ
|
|
|
|
|
No, sorry, it delivers the same as Application.ProductVersion. I need the Revison# from the published version.
Ariadne
|
|
|
|
|
Remove the .Version from the second line, and check all the methods. They are all there.
|
|
|
|
|
Hi everyone,
I'm trying to build a new set of controls for an application I'm writing. I've created a control that behaves a bit like a combo box and I've added it to a control which, in turn, I have added to a form. I designed the dropdown bit as a form so that it can be displayed outside the area of the control that I added it to. I can't find the Coordinates of the control within the main form so that I can tell the dropdown bit where to display.
Does this make sense? Does anyone know how to read these coordinates out?
Thanks in advance,
Russell
|
|
|
|
|
Hello,
I think I made a control which sounds like you descriped.
I used the 'PointToScreen' Method of my control and added the Hight of my Control as a startpoint for the Window.
MyControl c;
c.PointToScreen(new System.Drawing.Point(0, 0));
Hope that helps.
All the best,
Martin
|
|
|
|
|
Thanks very much,
just the ticket, looking good now.
Russell
|
|
|
|
|
Hi all,
I'm wanting to stretch a label dynamically.
"Autosize" only works for one line.
In order to do this I've done a manual word wrap and counted the number of lines,
Then changed the height of the label to no of lines * height.
It works after a fashion - but is there a better way to do this ?
In particular - the width of the label is in points.
The length of the string is the number of characters.
Relating the two depends on the fontsize and other spacing.
Any thoughts would be much appreciated
Cheers
Mike
|
|
|
|
|
hope this'll help you to get a better
Graphics gs = label1.CreateGraphics();
label1.Size= gs.MeasureString("label Caption",label1.Font).ToSize();
sameer
|
|
|
|
|