2

Hi I have an Class Form and Class SpecificForm. Class SpecificForm inherirts Form. Specific Form has an override ToString method that writes a string that makes a sense. However when I call the ToString or object of class Form I get the following output:

FormProxy923a5e0e9d7a46b7baa4dfe2173af18c

Any ideas why I am not able to get the custom string even though I am overriding it.

Any ideas and suggestions are greatly appreciated!

 /// <summary> /// Override of tostring /// </summary> public override string ToString() { if (this.Description == null) return this.Form.ToString(); return this.Description; } 
4
  • 4
    Can you post both your ToString method and how you are using it. Commented Oct 3, 2011 at 22:27
  • 6
    Instead of describing your code, post your code. That way we will actually know what it really is. Commented Oct 3, 2011 at 22:27
  • Do you use some dependency injection framework for the forms? Commented Oct 3, 2011 at 22:37
  • "class SpecificForm inherits Form"... "when I call the ToString of class Form"... What am I missing? Commented Oct 3, 2011 at 22:41

4 Answers 4

2

I am not sure if I understand the question, but if I got it right, you are wondering why instances of type Form does not behave like instances of type ExtendedForm.

Overrides only replace the methods on objects that are of 'child' types, not the methods on the 'parents'. First of all, it's not how inheritance is supposed to work, but it's also not practically possible, i.e. what if you have two extended classes; which ToString override should be used?

The following should explain how it works.

 public class ClassBase { public override string ToString() { return "base"; } } public class ClassA : ClassBase { public override string ToString() { return "A"; } } public class ClassB : ClassBase { public override string ToString() { return "B"; } } public class ClassC : ClassA { // No override } 

This would produce the following

 ClassBase baseClass = new ClassBase(); baseClass.ToString() // Returns "base" ClassA aClass = new ClassA(); aClass.ToString(); // Returns "A" ClassB bClass = new ClassB(); bClass.ToString(); // returns "B" ClassC cClass = new ClassC(); cClass.ToString(); // returns "A" because no override exists 

Keep in mind that it does not matter what kind of reference you have. Say you have a reference to a ClassBase, which is actually referencing a ClassA, then the ClassA override would still be called

 ClassBase baseClass = new ClassA(); baseClass.ToString(); // Returns "A" because baseClass is actually a ClassA 
Sign up to request clarification or add additional context in comments.

Comments

1

You don't see your new method because the Form class is not a SpecificForm class. Inheritance only moves in one direction. If your base type also took on the attributes of the child type, what would happen when two child types both add members with the same name?

Comments

0

You aren't being clear on what the problem is, code examples always help, but I will cover the two I can think of:

Form form = new Form(); form.ToString(); //returns FormProxy923a5e0e9d7a46b7baa4dfe2173af18c 

This is to be expected, as SpecificForm is not involved here.

Form form = new SpecificForm(); form.ToString(); //returns FormProxy923a5e0e9d7a46b7baa4dfe2173af18c 

This is likely caused by missing the override keyword in your ToString() definition, it should look like this:

public override string ToString() { //Do stuff } 

If you are missing the override the default behavior is to create a non-virtual method, which only affects variables of type SpecificForm in your example.

Comments

0

Not sure if I understood your question. If you are asking why this part:

return this.Description; 

doesn't execute instead of this part:

if (this.Description == null) return this.Form.ToString(); 

I can only say, "this.Description" was not set in your coding path before this part gets executed.

2 Comments

Yes description was not set, but then Form.ToString() should call the override method. I have checked in the child classes and all of them are overriding the ToString() method.
Form is not your class, correct? Or is it? It is a bit confusing with the System.Windows.Forms class ---> msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.