开发者

vb.net - accessing an initiated class from another class

开发者 https://www.devze.com 2023-04-12 06:16 出处:网络
I have the following defined in my \"Form1\": Dim Database As New DatabaseManager So that I can access all my database functions anywhere in the main form (database connection is initialized via t

I have the following defined in my "Form1":

 Dim Database As New DatabaseManager

So that I can access all my database functions anywhere in the main form (database connection is initialized via the "private sub new()" function in the DatabaseManager class.

The works fine for all queries done in form1, however I have many other classes I'd like to be able to access the database from.

Does anyone know how to access the initiated cla开发者_如何学Pythonss in my example? Since right now it is initiated in the main form, if I try to access any functions inside any other class functions it does nothing (doesn't error out either).

I'm trying to figoure out how to dim a class one and I can access it from within any class and I can't figure it out.

Thanks!


I would encourage to use dependency injection if you want this.

In essence it would come down to this.

Private _DatabaseManager as DatabaseManager

Public Sub New(Byval DatabaseManager as DatabaseManager)
  InitializeComponent()
  _DatabaseManager = DatabaseManager
End Sub

TIf you do it like this you can give all your forms the same DatabaseManager or a different one like you please.

There is of course a lot more to it than that . But for that you will have to dig into Dependecy Injection and Inversion of control (DI/IoC)


One thing you could do is create a factory for the DatabaseManager and then just have all your other code call it from that factory. I'm very out of practice with VB syntax, but in C# it might look something like this:

public class DatabaseManagerFactory
{
    private static DatabaseManager _current = null;

    public static DatabaseManager Current
    {
        get
        {
            if (_current == null) _current = new DatabaseManager();
            return _current;
        }
    }
}

VB

Public Class DatabaseManagerFactory
    Private Shared _current As DatabaseManager = Nothing

    Public Shared ReadOnly Property Current As DatabaseManager
        Get
            If _current Is Nothing null Then _current = New DatabaseManager()
            Return _current
        End Get
    End Property
 End Class

The idea then is that anything in your application which needs to use a DatabaseManager would just call DatabaseManagerFactory.Current to get the one shared instance.

Note that in this case DatabaseManager isn't really a singleton, you can still instantiate one elsewhere in the application if you need to for some reason. If it should be an actual singleton then you'd want to make some modifications to the DatabaseManager class itself. Maybe give it a private constructor and implement this factory directly on the class? Something like this:

public class DatabaseManager
{
    private static DatabaseManager _current = null;

    public static DatabaseManager Current
    {
        get
        {
            if (_current == null) _current = new DatabaseManager();
            return _current;
        }
    }

    private DatabaseManager
    {
        // your initialization of the class
    }
}

VB

Public Class DatabaseManager
    Private Shared _current DatabaseManager = Nothing

    Public Shared ReadOnly Property Current As DatabaseManager
        Get
            If _current Is Nothing Then _current = New DatabaseManager()
            Return _current
        End Get
    End Property 

    Private Sub New()
        ' your initialization of the class
    End Sub
End Class

(I encourage anybody more familiar with VB syntax to edit this answer accordingly to better address the question.)


create and initiate databaseManager in your Form1, but declare it Friend instead of Dim. That way you can write a reference to it like : Form1.databaseManager

0

精彩评论

暂无评论...
验证码 换一张
取 消