Initialisation of a class library

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
Where can you put initialisation code for a class library? I've got a List of objects which I use in the class library and I want to initialise it with data. Where would I call this from?
 
Unless I'm mistaken, you can't initialise a class library because it isn't an object. The simplest way to initialise objects within a class library is to have a static (shared) method called Initialise, which you would call at some point before you would require the objects, in your forms contructor for example.
 
Cags said:
Unless I'm mistaken, you can't initialise a class library because it isn't an object. The simplest way to initialise objects within a class library is to have a static (shared) method called Initialise, which you would call at some point before you would require the objects, in your forms contructor for example.
Which is less than ideal, because you have to remember to do it in the form's constructor for every project which references the class library.
 
Just do something like this:
C#:
public static class MyClass
{
    static List<int> _list = null;

    public int[] MyArray
    {
        get
        {
            if (_list == null)
            {
                // _list has not been initialized yet...
                _list = new List<int>();
                _list.Add(100);
                _list.Add(200);
                _list.Add(300);
            }

            return _list.ToArray();
        }
    }
}
 
Gill Bates said:
Just do something like this:
C#:
public static class MyClass
{
    static List<int> _list = null;

    public int[] MyArray
    {
        get
        {
            if (_list == null)
            {
                // _list has not been initialized yet...
                _list = new List<int>();
                _list.Add(100);
                _list.Add(200);
                _list.Add(300);
            }

            return _list.ToArray();
        }
    }
}

You can't use Static like that in VB.net. You can only use it for variables within a procedure that you want to have retain their value across calls to that procedure.
 
You can only use it for variables within a procedure that you want to have retain their value across calls to that procedure.
Well, that's exactly what you want to do isn't it?

Static (shared) variables are shared across all instances of that class as well as static methods.

Visual Basic:
    Public Class TestClass

        Private Shared _list As System.Collections.Generic.List(Of Integer) = Nothing

        Public Shared ReadOnly Property MyArray() As Integer()

            Get
                If _list Is Nothing Then
                    Console.WriteLine("List created...")
                    _list = New System.Collections.Generic.List(Of Integer)
                    _list.Add(100)
                    _list.Add(200)
                    _list.Add(300)
                End If

                Return _list.ToArray()

            End Get

        End Property

    End Class
So, the array will be built the the first time you hit the "MyArray" property. All subsequent references to the "MyArray" property will use the shared private member "_list".

Example: you will only see the "List created..." statement once:
Visual Basic:
Console.WriteLine(TestClass.MyArray.Length)
Console.WriteLine(TestClass.MyArray.Length)
 
Static in C# == Shared in VB.

You can use type initializers ("Shared Sub New") for your classes in your class library to initialize the class library. For any class that needs to have shared members initialized, declare a sub new that is marked as Shared. The only limitation when using this technique is that the type initializers ("Shared Sub News") can not have circular dependencies (i.e. ClassA's type initializer can't in any way involve ClassB if ClassB's type initializer involves ClassA).

So, to answer the original question,
rbulph said:
Where can you put initializations code for a class library? I've got a List of objects which I use in the class library and I want to initialize it with data. Where would I call this from?
You would declare a Shared Sub New in the class that contains the list and initialize the list from that sub.

Or, you could use Gill Bates' lazy initialization technique, but I personally wouldn't put fourth the effort of implementing lazy initialization unless there is a decent chance that an object won't be used.
 
Thanks, I agree that marble_eater's approach is preferable. In either case it's all a bit counterintuitive because you're never using the classes to create objects, you're using them more like namespaces, but it works so that's the important thing.
 
Back
Top