1

I have a base class (written using C#.net) which uses datasets to pull data from DB and the connection string is in App.config file. So after writing the base class it has been compiled into dll.

And to use this base class for different project I have to override the DB connection string, so first is it possible to do and if possible, can anyone give me an example for it?

2
  • If the connection string is written to the App.config file, then it would be unique to that project. What exactly is your question about this base class you created? Commented Aug 18, 2011 at 14:05
  • @Ramhound as I told its a base class to be written for using in other projects and base class talks with the DB to do some actions so when I want to use it with another projects I want a function that give access to connection string in base class and I can override such function ... if you worry about the table you using for it is, then it also generic i.e. like log table that is most probably generic to all project ... I think I'll try one of the Answer below ... Commented Aug 18, 2011 at 14:12

2 Answers 2

3

I would suggest giving your class an overloaded constructor, like this:

private readonly string connectionString;

public Foo() : this(Settings.Default.DbConnectionString) {
}

public Foo(string connectionString) {
    this.connectionString = connectionString;
}

Then the derived class can just pass the "overridden" connection string into the constructor.

I think this is cleaner than using polymorphism with a virtual property etc - especially as presumably the connection string isn't going to change over the course of the object's lifetime. You're not really changing the behaviour (which is what polymorphism's good for) - you're changing the initialization (which is what constructor parameters are good for).

Sign up to request clarification or add additional context in comments.

2 Comments

I am using a static class and when I tried to do this, it returns me an error saying Error-1: 'Cntrs.CountersCache.CountersCache()': static constructor cannot have an explicit 'this' or 'base' constructor call Error-2: 'Cntrs.CountersCache.CountersCache(string)': a static constructor must be parameterless any suggestion ???
@dvlpr: If you're using a static class, then it can't be a base class in the first place... you can't derive from a static class. I would suggest that you move away from using a static class for this in the first place.
0

Make the connection string a protected virtual property in your base class

class Base {
    protected virtual string ConnectionString { get { ... } } // Get from config.
}

class Sub {
    protected override string ConnectionString { get { ... } } // return the new value.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.