Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Class Inheritance

C# Class Inheritance

Scheduled Pinned Locked Moved C#
helpcsharpdatabasedata-structuresoop
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LighthouseJ
    wrote on last edited by
    #1

    I have 4 classes. Class A is derived from System.Object, class B is derived from ArrayList (the collection class), class C and class D are derived from class B. All 4 classes of mine have ToString methods that each show a different thing. I used the 'override' reserved word in each declaration to replace the funcionality of the inherited method. I have a form which has a drop-down listbox that utilizes the ToString methods of classes' C and D only. I also have a TreeView that shows all 4 classes in a tree diagram, consider that class B and D are root items in the tree view and only contain objects of type A and C respectively. The problem is classes A and B work 100% fine, but only those two. However, classes C and D are the problem. In the form with the list box, I add objects of type class C using dlg.listbox.Add(new class C("some text", somevalue)); before showing the form. Class C has that overridden ToString method that returns a special string based on that string and that value given to it. On runtime, the box simply says (Collection) instead of what it should. Later, the same class C object shows up in the tree view, but instead of seeing the same (Collection), you see some text but only that text. Somewhere the object is returning that custom text element and not the value, and the ToString method for class C incorporates both like I mentioned earlier. I don't know how to fix this. I put breakpoints on every ToString method and the first two classes trigger fine but C and D's are never used. The project is too complex to just paste it all so if you have an idea and need more info, just ask and I'll be glad to answer.

    G R M 3 Replies Last reply
    0
    • L LighthouseJ

      I have 4 classes. Class A is derived from System.Object, class B is derived from ArrayList (the collection class), class C and class D are derived from class B. All 4 classes of mine have ToString methods that each show a different thing. I used the 'override' reserved word in each declaration to replace the funcionality of the inherited method. I have a form which has a drop-down listbox that utilizes the ToString methods of classes' C and D only. I also have a TreeView that shows all 4 classes in a tree diagram, consider that class B and D are root items in the tree view and only contain objects of type A and C respectively. The problem is classes A and B work 100% fine, but only those two. However, classes C and D are the problem. In the form with the list box, I add objects of type class C using dlg.listbox.Add(new class C("some text", somevalue)); before showing the form. Class C has that overridden ToString method that returns a special string based on that string and that value given to it. On runtime, the box simply says (Collection) instead of what it should. Later, the same class C object shows up in the tree view, but instead of seeing the same (Collection), you see some text but only that text. Somewhere the object is returning that custom text element and not the value, and the ToString method for class C incorporates both like I mentioned earlier. I don't know how to fix this. I put breakpoints on every ToString method and the first two classes trigger fine but C and D's are never used. The project is too complex to just paste it all so if you have an idea and need more info, just ask and I'll be glad to answer.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      What more do you have in the C and D classes? It seams like the listbox reads the value directly from a member. Do you have any public members that might be used by the listbox? --- b { font-weight: normal; }

      L 1 Reply Last reply
      0
      • L LighthouseJ

        I have 4 classes. Class A is derived from System.Object, class B is derived from ArrayList (the collection class), class C and class D are derived from class B. All 4 classes of mine have ToString methods that each show a different thing. I used the 'override' reserved word in each declaration to replace the funcionality of the inherited method. I have a form which has a drop-down listbox that utilizes the ToString methods of classes' C and D only. I also have a TreeView that shows all 4 classes in a tree diagram, consider that class B and D are root items in the tree view and only contain objects of type A and C respectively. The problem is classes A and B work 100% fine, but only those two. However, classes C and D are the problem. In the form with the list box, I add objects of type class C using dlg.listbox.Add(new class C("some text", somevalue)); before showing the form. Class C has that overridden ToString method that returns a special string based on that string and that value given to it. On runtime, the box simply says (Collection) instead of what it should. Later, the same class C object shows up in the tree view, but instead of seeing the same (Collection), you see some text but only that text. Somewhere the object is returning that custom text element and not the value, and the ToString method for class C incorporates both like I mentioned earlier. I don't know how to fix this. I put breakpoints on every ToString method and the first two classes trigger fine but C and D's are never used. The project is too complex to just paste it all so if you have an idea and need more info, just ask and I'll be glad to answer.

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        LighthouseJ wrote:

        the first two classes trigger fine but C and D's are never used.

        I believe you may need to make B's ToString() virtual. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

        L 1 Reply Last reply
        0
        • L LighthouseJ

          I have 4 classes. Class A is derived from System.Object, class B is derived from ArrayList (the collection class), class C and class D are derived from class B. All 4 classes of mine have ToString methods that each show a different thing. I used the 'override' reserved word in each declaration to replace the funcionality of the inherited method. I have a form which has a drop-down listbox that utilizes the ToString methods of classes' C and D only. I also have a TreeView that shows all 4 classes in a tree diagram, consider that class B and D are root items in the tree view and only contain objects of type A and C respectively. The problem is classes A and B work 100% fine, but only those two. However, classes C and D are the problem. In the form with the list box, I add objects of type class C using dlg.listbox.Add(new class C("some text", somevalue)); before showing the form. Class C has that overridden ToString method that returns a special string based on that string and that value given to it. On runtime, the box simply says (Collection) instead of what it should. Later, the same class C object shows up in the tree view, but instead of seeing the same (Collection), you see some text but only that text. Somewhere the object is returning that custom text element and not the value, and the ToString method for class C incorporates both like I mentioned earlier. I don't know how to fix this. I put breakpoints on every ToString method and the first two classes trigger fine but C and D's are never used. The project is too complex to just paste it all so if you have an idea and need more info, just ask and I'll be glad to answer.

          M Offline
          M Offline
          malharone
          wrote on last edited by
          #4

          LighthouseJ wrote:

          The project is too complex

          Here's a simple solution.. has the same class hierarchy as you described.. and it shows the .ToString() values. (I hope you're not confusing ListBox with ListView) - Malhar using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace Malhar.ControlGallery { /// /// Summary description for Form3. /// public class Form3 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button Load; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form3() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); this.Load = new System.Windows.Forms.Button(); this.SuspendLayout(); // // listBox1 // this.listBox1.Location = new System.Drawing.Point(8, 16); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(176, 95); this.listBox1.TabIndex = 0; // // Load // this.Load.Location = new System.Drawing.Point(192, 8); this.Load.Name = "Load"; this.Load.TabIndex = 1; this.Load.Text = "button1"; this.Load.Click += new System.EventHandler(this.button1_Click); // // Form3 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.Load); this.Controls.Add(this.listBox1); this.Name = "Form3"; this.Text = "Form3"; this.ResumeLayout(false); } #endregion private void button1_Click(object sender, System.EventArgs e) { listBox1.Items.Add(new A()); li

          L 1 Reply Last reply
          0
          • G Guffa

            What more do you have in the C and D classes? It seams like the listbox reads the value directly from a member. Do you have any public members that might be used by the listbox? --- b { font-weight: normal; }

            L Offline
            L Offline
            LighthouseJ
            wrote on last edited by
            #5

            class B has a String and a custom class type, C has a number and D has nothing more than the String and custom class intended for B. I didn't mention it before but I know about and utilized the base constructor. It was my understanding that I could add objects to the listbox Items collection and when the listbox is run, it shows strings from the ToString method from the specific object. The only other method I have besides class C's ToString is its constructor and a method used to gather TreeNode-derived objects from the items in its collection.

            G 1 Reply Last reply
            0
            • R Ravi Bhavnani

              LighthouseJ wrote:

              the first two classes trigger fine but C and D's are never used.

              I believe you may need to make B's ToString() virtual. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

              L Offline
              L Offline
              LighthouseJ
              wrote on last edited by
              #6

              I thought about that before and tried it but there was no difference in the outcome in runtime.

              1 Reply Last reply
              0
              • M malharone

                LighthouseJ wrote:

                The project is too complex

                Here's a simple solution.. has the same class hierarchy as you described.. and it shows the .ToString() values. (I hope you're not confusing ListBox with ListView) - Malhar using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace Malhar.ControlGallery { /// /// Summary description for Form3. /// public class Form3 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button Load; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form3() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); this.Load = new System.Windows.Forms.Button(); this.SuspendLayout(); // // listBox1 // this.listBox1.Location = new System.Drawing.Point(8, 16); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(176, 95); this.listBox1.TabIndex = 0; // // Load // this.Load.Location = new System.Drawing.Point(192, 8); this.Load.Name = "Load"; this.Load.TabIndex = 1; this.Load.Text = "button1"; this.Load.Click += new System.EventHandler(this.button1_Click); // // Form3 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.Load); this.Controls.Add(this.listBox1); this.Name = "Form3"; this.Text = "Form3"; this.ResumeLayout(false); } #endregion private void button1_Click(object sender, System.EventArgs e) { listBox1.Items.Add(new A()); li

                L Offline
                L Offline
                LighthouseJ
                wrote on last edited by
                #7

                Actually, I was wrong and it's a combobox, but class B contains the string and a custom class, and C has an int. My class structure and ToString() method definitions are basically the same thing, you say yours works so I don't know what's different to not work.

                1 Reply Last reply
                0
                • L LighthouseJ

                  class B has a String and a custom class type, C has a number and D has nothing more than the String and custom class intended for B. I didn't mention it before but I know about and utilized the base constructor. It was my understanding that I could add objects to the listbox Items collection and when the listbox is run, it shows strings from the ToString method from the specific object. The only other method I have besides class C's ToString is its constructor and a method used to gather TreeNode-derived objects from the items in its collection.

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Make sure that you haven't missed the override keyword on any of the ToString methods. The Object.ToString method is virtual, but if you don't override it, you will be creating a non-virtual method that will only be reachable when refering to the object using a reference of the specific type. When referenced as an object, the virtual method will be used instead. --- b { font-weight: normal; }

                  L 1 Reply Last reply
                  0
                  • G Guffa

                    Make sure that you haven't missed the override keyword on any of the ToString methods. The Object.ToString method is virtual, but if you don't override it, you will be creating a non-virtual method that will only be reachable when refering to the object using a reference of the specific type. When referenced as an object, the virtual method will be used instead. --- b { font-weight: normal; }

                    L Offline
                    L Offline
                    LighthouseJ
                    wrote on last edited by
                    #9

                    I double checked that several times and I did give all four instances of ToString the override keyword. It's very frustrating that it keeps happening. I also added marks in each ToString to display what class the ToString is being called from and the first three classes are fine, it's just that last one class can't show it's ToString in the combo box like it should. I'll keep hammering at it though, I know it's worked before. I figure I might have a "base.ToString()" instead of a "this.ToString()" or something set wrong like that.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups