1

I'm trying to dynamically create a connection string during compile time:

private static string m_ConnectionString
{
    get 
    {
        string connectionString = @"Data Source=" + myLibrary.common.GetExeDir() + @"\Database\db.sdf;";
        return connectionString;
    }
}
public static string ConnectionString = m_ConnectionString;

I keep running into type initialization errors. ConnectionString ends up null at runtime. I was trying to avoid having to set the connection string for all applications that use my code library. The location of the database is different for each project that uses the library. I have code that can determine the correction string, but was wanting to run it during class initialization. Is this even possible?

6
  • It is possible, and there's nothing obviously wrong with your code. Likely there's something wrong with the way it's being used. Can you reproduce the problem in a small, stand-alone test that you can post here? Commented Mar 12, 2010 at 3:46
  • 1
    Getting the obvious out of the way, are you actually recompiling this library with each of the other projects (not just referencing the library's dll)? Commented Mar 12, 2010 at 3:51
  • No, I'm not. I should have said "during class initialisation" maybe. Commented Mar 12, 2010 at 4:05
  • The code you are showing should work unless there is something wrong in: ** myLibrary.common.GetExeDir()** Could you post more details about it? Commented Mar 12, 2010 at 4:47
  • 1
    also: Does the code compile? and if yes: could you send the stack trace or the exception details? Commented Mar 12, 2010 at 4:52

3 Answers 3

2

I agree with Oliver's approach to discover the error, but would like to add that you could put this in a static constructor. This would achieve your requirement of "during class initialization".

public static string ConnectionString { get; private set; }

static MyClass()
{
    ConnectionString = @"Data Source=" + myLibrary.common.GetExeDir() + @"\Database\db.sdf;";
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm still getting null for ConnectionString until runtime. You see, I have another custom object that is constructed and set to a static during initialisation, and that object's constructor tries to use the ConnectionString. Apparently, at the time this object is create, ConnectionString is still null, and I get TypeInitializationException. If the connection string was a constant, I don't believe this would happen, but because it is dynamic, there is a problem.
I think what I'm trying to do is impossible. I went ahead moved the creation of the other object to each project's runtime. However, the static constructor code was still helpful.
As others have said, GetExeDir can not be known until runtime. I would suggest taking a look at the code that uses ConnectionString to see if you can refactor it to use the same technique as the class containing ConnectionString. As soon your code invokes MyClass.ConnectionString, it will have a value (unless there is an exception while assigning it).
2

Just set a breakpoint and step into your function and try to find out what is going wrong.

Maybe there will be some exception be thrown which you actually don't see (in some underlying code). To find these cases you should go in Visual Studio to Debug - Exceptions and check all the boxes in the list. Maybe you can find this way, why you get a null instead of a string.

Comments

1

That code is executed at runtime, not compile time. I think you're going down the wrong track.

Another program running as a pre build event could modify the source code of the resources.resx file, prior to compilation. You then get your connection string as a resource string.

Kind of a hack, but not the worst thing I've ever seen. My versions numbers are incremented in a similar way.

3 Comments

I guess I don't really mean compile time. I really mean "during initialisation", but before consuming projects create any object from my library. If it didn't belong to a static class, I could use a constructor.
@LonnieBest Ah. I thought it was odd that GetExeDir on the build machine was relevent once the software is deployed elsewhere.
Since assemblies aren't loaded until a class in them is referenced then you won't save much time unless you explicitly load the assembly from the client code. In which case you could just call the initializer code yourself.

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.