Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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?
Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
Unless I'm mistaken' date=' 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.[/quote']

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.

Posted
Just do something like this:
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();
       }
   }
}

Posted
Just do something like this:
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.

Posted
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.

 

    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:

Console.WriteLine(TestClass.MyArray.Length)
Console.WriteLine(TestClass.MyArray.Length)

  • Leaders
Posted

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,

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.

[sIGPIC]e[/sIGPIC]
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...