4 sintax problems in C#: can you help?
1) how do you invoke a constructor *inside* some other consrtuctor
class X{
public X(int i) {...}
public X(int j, string s) { X(j); ... } //wont compile
}
2) is it possible to define methods with default values à là C++?
class X{
public X(int i) {...}
public void abc(int j, int k=0, string s="") { ... }
/*
overloaded forms:
public void abc(int j) { ... }
public void abc(int j, int k) { ... }
public void abc(int j, int k, string s) { ... }
*/
}
3) how do you run code as soon as a class is loaded? something like:
class X{
static { //run as soon as the class is loaded
int i=0;
...
}
public X(int i) {...}
}
4) how to force the redefinition of a static method in a derivate class?
//>>>>>abstract class to avoid instanciation
public abstract class DataStorage {
protected static IDbConnection cn;
protected static DataStorage db;
protected IDataReader dr;
protected IDbCommand cmd;
//create & ini cn HERE(provide a conn strin here!)
//>>>>>abstract to force its redefinition in a derivate class - wont compile bc its static...
protected static abstract bool IniciateConnection();
//create & ini dr, cmd, ...
//>>>>>abstract to force its redefinition in a derivate class
protected abstract void IniciateInstanceObjects();
private DataStorage() { //private to create singleton?
IniciateInstanceObjects();
}
public static DataStorage GetInstance() {
if (cn==null) IniciateConnection();
db=new DataStorage();
return db;
}
//...
}
//here are the derivate classes with overriden méthods - static ones wont compile
public class DataStorageMSA : DataStorage {
protected static override bool IniciateConnection() { //compile error
cn = new OleDbConnection();
cn.ConnectionString="Provider=...";
}
protected override void IniciateInstanceObjects() { //create & ini dr,cmd,...
dr = new OleDbDataReader();
cmd = new OleDbCommand();
}
}
public class DataStorageSQL : DataStorage {
protected static override bool IniciateConnection() { //compile error
cn = new SqlConnection();
cn.ConnectionString="";
}
protected override void IniciateInstanceObjects() { //create & ini dr,cmd,...
dr = new SqlDataReader();
cmd = new SqlCommand();
}
}
any help will be appreciated!...