Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Constructor firing Inheritance in C#
Message
De
20/01/2006 00:50:27
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01088705
Message ID:
01088776
Vues:
11
Rick,

The secret is to use a virtual property, and then it can be overridden in the sub-class. It has to be a "real" property with get/set, not merely a public variable member. Try this:
public class BizObj
{
   public string m_TableName="";

   public virtual string TableName
   {
      get {return this.m_TableName;}
      set {this.m_TableName = value;}
   }

   private DataSet oData;

   public BizObj()
   {
      LoadData();
   }

   public void LoadData()
   {
      SqlConnection oConn = new SqlConnection("...");
      SqlDataAdapter oAdapter = new SqlDataAdapter("SELECT * FROM "+this.TableName,oConn);
      oAdapter.Fill(oData);

   }
}

public class AuthorBizObj: BizObj
{
   public override string TableName
   {
      get {return "Authors;}
   }
}
~~Bonnie

>I'm having a problem, and here is a simplified example of code that demonstrates it:
>
>
>
>public class BizObj
>{
>   public string TableName="";
>   private DataSet oData;
>
>   public BizObj()
>   {
>      LoadData();
>   }
>
>   public void LoadData()
>   {
>      SqlConnection oConn = new SqlConnection("...");
>      SqlDataAdapter oAdapter = new SqlDataAdapter("SELECT * FROM "+this.TableName,oConn);
>      oAdapter.Fill(oData);
>
>   }
>}
>
>public class AuthorBizObj: BizObj
>{
>   public AuthorBizObj()
>   {
>      this.TableName="Authors";
>   }
>}
>
>
>Now, because the base class constructor fires first, LoadData is called before the TableName can be set to "Authors".
>
>So I try to override the TableName field in the following manner, hoping that it will be set before the constructor methods fire:
>
>
>public class AuthorBizObj: BizObj
>{
>   public string TableName="Authors";
>
>   public AuthorBizObj()
>   {
>   }
>}
>
>
>But I get a compile warning: "The keyword new is required on AuthorBizObj.TableName because it hides inherited member BizObj.TableName."
>
>I'm not trying to hide the member, I'm trying to override it.
>
>How can I make sure that the TableName field has the value "Authors" before the BizObj constructor fires?
>
>This question is strictly about inheritance.
>
>You might say "Pass a value for TableName to the constructor" , but I think that the Authors bizobj should know its table, not have its name passed into it.
>
>You might ask "Why do you want to call LoadData in the constructor?" or say "Call LoadData after you have instantiated AuthorBizObj, not in the constructor" - I'm actually not creating bizobjs, this is just some sample code that demonstrates the problem, I'm trying to override a member field, not figure out where LoadData should be called.
>
>Thanks,
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform