How do I do that?
The scenario:
abstract class DataAccess
{
public abstract string ConnectionString { get; set; }
public static DataTable ExecuteSql(string sql)
{
// ...
}
public static object ExecuteStoredProc(string storedProcName, ...)
{
// ...
}
}
abstract class DataAccessDb1 : DataAccess
{
public override string ConnectionString = "SetDbSpecificConnectionStringHere";
public static DataTable GetStuff()
{
// Call the method with the ConnectionString set HERE.
return ExecuteSql("select * from stuff");
}
}
I know it’s know possible to set the connection string like in the derived class, but I want to keep it static, therefore I won’t set the property in every method in the derived class… Any ideas?
Yes: pass the connection string in as a parameter into the method.
DataAccessDb1is invalidstaticand polymorphism don’t mixSo basically, if you want polymorphic behaviour, you should be using instance members. If you don’t really need polymorphic behaviour, pass any variations (such as the connection string) as a parameter for the method.