locked
Sub classing Class with implemented interface RRS feed

  • Question

  • I have interface IEntityLoader with the method of
    void Load(SqlDataReader reader)

    public Interface IEntityLoader
    {
      void Load(SqlDataReader reader);
    }

    By using this interface I implement Item class

    public class Item: IEntityLoader
    {
      //Properties
      public string ItemCode{get;set;}
      public string ItemName{get;set;}
    
       //Load object from Method implemented from the interface
       void Load(SqlDataReader reader)
        {
         this.ItemCode = Convert.ToString(dataReader["Code"]);
    
         this.ItemCode = Convert.ToString(dataReader["Name"]);
        }
    }
    



    Now I'm inherit Item class to the ItemDet class

    public class ItemDet:Item
    {  
    
    }





    In this class I have property like ItemRate. Now I want to load all 3 properties to the ItemDet class.
    Even I override the Load method in ItemDet base class properties like ItemCode and ItemName doesn't get loaded.
    What's the point I have missed here.

    Saturday, October 5, 2013 4:27 AM

Answers

  • I got the solution

    public class Item : IEntityLoader {

    //This is the implemented method of the IEntityLoader interface.
    void IEntityLoader.Load(SqlDataReader reader)
    {
       //Virtual method need to be called inside the implemented Load method
        Load(reader);
    }

    virtual void Load(SqlDataReader reader) { ItemCode = Convert.ToString(reader["Code"]); ItemName = Convert.ToString(reader["Name"]); } } public class ItemDet : Item { public override void Load(SqlDataReader reader) { base.Load(reader); ItemRate = Convert.ToString(reader["Rate"]); } }


    • Marked as answer by Sanyon_d Monday, October 7, 2013 3:07 AM
    Monday, October 7, 2013 3:07 AM

All replies

  • I'm not exactly sure what your question is so I played around with your code and added some to hopefully reveal what you need.

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ItemNameTest { class Item : IEntityLoader { //Properties public string ItemCode { get; set; } public string ItemName { get; set; }

    public Item() //base constructor

    {

    this.ItemCode = "base Constructor";

    this.ItemName = "fund";

    }

    public Item(string SetItemName) //overloaded constructor { this.ItemName = SetItemName; } //Load object from Method implemented from the interface public void Load(SqlDataReader reader) { this.ItemCode = "Code"; //Convert.ToString(dataReader["Code"]); this.ItemCode = "Name"; // Convert.ToString(dataReader["Name"]); } } public interface IEntityLoader { void Load(SqlDataReader reader); } public class ItemDet : Item { private void TestMethod() { ItemName = "ItemDet Set"; } } }



    • Edited by PaulDAndrea Saturday, October 5, 2013 6:33 AM
    Saturday, October 5, 2013 6:30 AM
  • I think the problem might be that you don't have any code in the ItemDet class.

    A hint for the future - if you want help with something that is not working then you will need to show what is not working!  Showing one thing asking for help with something else wastes your time and ours. 


    Paul Linton

    Saturday, October 5, 2013 6:35 AM
  • di d you forget to use the Read() method?

            void Load(SqlDataReader reader)
            {
                if (reader.Read() == true)
                {
                    this.ItemCode = Convert.ToString(dataReader["Code"]);
                    this.ItemCode = Convert.ToString(dataReader["Name"]);
                }
            }


    jdweng

    Saturday, October 5, 2013 6:36 AM
  • "Even I override the Load method in ItemDet base class properties like ItemCode and ItemName doesn't get loaded. "

    You probably don't call the "base" version of Load. The code should look something like the following:

    public class Item : IEntityLoader {
        public virtual void Load(SqlDataReader reader) {
            ItemCode = Convert.ToString(reader["Code"]);
            ItemName = Convert.ToString(reader["Name"]);
        }
    }
    
    public class ItemDet : Item {
        public override void Load(SqlDataReader reader) {
            base.Load(reader);
            ItemRate = Convert.ToString(reader["Rate"]);
        }
    }
    

    • Proposed as answer by BonnieBMVP Saturday, October 5, 2013 5:01 PM
    Saturday, October 5, 2013 7:32 AM
  • I came back to the code and played with it some more.... Now I also see others have posted comments simular so hear is what I have

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Security.Cryptography;
    namespace ItemNameTest
    {
        class Item : IEntityLoader
        {
            //Properties
            public string ItemCode { get; set; }
            public string ItemName { get; set; }
            public Item() //base constructor
            {
            }
            public Item(string SetItemName) //overloaded constructor
            {
                this.ItemName = SetItemName;
            }
            //Load object from Method implemented from the interface
            public void Load(SqlDataReader reader)
            {
                this.ItemCode = Convert.ToString(reader["Code"]);
                this.ItemCode = Convert.ToString(reader["Name"]);
            }
        }
        public interface IEntityLoader
        {
            void Load(SqlDataReader reader);
        }
        public class ItemDet : Item
        {
            private void TestMethod()
            {
                ItemName = "ItemDet Set";
                SqlDataReader reader = new SqlDataReader();
                Load(reader);
            }
        }
    }

    • Proposed as answer by PaulDAndrea Saturday, October 5, 2013 6:06 PM
    Saturday, October 5, 2013 8:37 AM
  • Thanks for your reply Mike. This is the exact  thing I want to do. The ItemDet class also same like my one. But in my case, This Item class implemented from the IEntityLoader interface. And the Load method is in the IEntityLoader interface. But when I'm trying to implement this method as public virtual error shows  "The modifier 'virtual' is not valid for this item".

    In my interface this method is

    void Load(SqlDataReader reader)

    What's thing I've done wrong here.

    Saturday, October 5, 2013 1:06 PM
  • Where is the Read() method?    Do you want this code to read the line of the database or are you doing the Read() some place else in the code?   Without seeing where you are calling the constructor for the reader and where you are actually reading the datarow from the reader it is impossible to determine why you are getting this error.

    jdweng

    Saturday, October 5, 2013 1:10 PM
  • "But when I'm trying to implement this method as public virtual error shows  "The modifier 'virtual' is not valid for this item". "

    Where are you putting the virtual keyword? It should be the Item class, that error may indicate that you have added 'virtual' to the interface. Interfaces do not need virtual/override keywords.

    Saturday, October 5, 2013 1:38 PM
  • I put it in the item class. It shows the error as mentioned.
    Sunday, October 6, 2013 1:42 AM
  • Is this what you need

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Security.Cryptography;
    namespace ItemNameTest
    {
        public class Item : IEntityLoader
        {
            //Properties
            public string ItemCode { get; set; }
            public string ItemName { get; set; }
            public Item() //base constructor
            {
            }
            public Item(string SetItemName) //overloaded constructor
            {
                this.ItemName = SetItemName;
            }
            //Load object from Method implemented from the interface
            public virtual void Load(SqlDataReader reader)
            {
                this.ItemCode = Convert.ToString(reader["Code"]);
                this.ItemCode = Convert.ToString(reader["Name"]);
            }
        }
        public interface IEntityLoader
        {
            void Load(SqlDataReader reader);
        }
        public class ItemDet : Item
        {
            public override void Load(SqlDataReader reader)
            {
                ItemCode = Convert.ToString(reader["Code"]);
                ItemName = Convert.ToString(reader["Name"]);
            }
        }
    }
    Edit: this compiles ok on my end


    • Edited by PaulDAndrea Sunday, October 6, 2013 2:53 AM
    • Proposed as answer by PaulDAndrea Sunday, October 6, 2013 3:00 AM
    Sunday, October 6, 2013 2:22 AM
  • Then show your code and indicate the line where the error occurs.
    Sunday, October 6, 2013 5:25 AM
  • I got the solution

    public class Item : IEntityLoader {

    //This is the implemented method of the IEntityLoader interface.
    void IEntityLoader.Load(SqlDataReader reader)
    {
       //Virtual method need to be called inside the implemented Load method
        Load(reader);
    }

    virtual void Load(SqlDataReader reader) { ItemCode = Convert.ToString(reader["Code"]); ItemName = Convert.ToString(reader["Name"]); } } public class ItemDet : Item { public override void Load(SqlDataReader reader) { base.Load(reader); ItemRate = Convert.ToString(reader["Rate"]); } }


    • Marked as answer by Sanyon_d Monday, October 7, 2013 3:07 AM
    Monday, October 7, 2013 3:07 AM
  • Aha, so I guess that the source of the error was that you were trying to put "virtual" on the explicit implementation of the Load member of the interface.

    Note that if you use explicit implementation like this then it's probably a good idea to make Load protected. Normally there's no need for anyone but the data layer to call Load and the data layer will call Load through the interface probably.

    Monday, October 7, 2013 5:51 AM