Mikkel Eriksen
Mikkel Eriksen

Reputation: 69

How to create a object reference in C#?

I recently starting creating my own classes and I decided to create a simpel one, but i ran into 1 problem. here's whats going on:

In my Form1 I have a

SqlCeConnection sqlCEconn = new SqlCeConnection(@"Data Source = C:\Dropbox\Visual\Database test2 -treeview\Database test2 -treeview\Database2.sdf");

and in my new class called test, I have this:

namespace Database_test2__treeview
{
class test
{
    public string testCHECK()
    {

        SqlCeConnection sqlCEconn = new SqlCeConnection();

        if (sqlCEconn.State == ConnectionState.Open)
        {
            return "Database connection: Open";
        }
        if (sqlCEconn.State == ConnectionState.Closed)
        {
            return "Database connection: Closed";
        }
        else
        {
            return "";
        }
    }
}
}

Later in Form1 im using my new class to check if the connection is open or not, by doing:

private void Form1_Load(object sender, EventArgs e)
    {
        sqlCEconn.Open();
        test testconnection = new test();

        Toolstrip1.Text = testconnection.testCHECK();

It's obvious that the return string will be "Closed", since my SqlCeConnection in test.cs is not linked to the SqlCeConnection in my Form1

how do I do that in a simple manner ?

thanks.

Upvotes: 0

Views: 7385

Answers (5)

Jorge Alvarado
Jorge Alvarado

Reputation: 2674

if you want the reference, then just send it as a parameter:

public string testCHECK(SqlCeConnection thisConnection)
{
    if (thisConnection.State == ConnectionState.Open)
    {
        return "Database connection: Open";
    }
    if (thisConnection.State == ConnectionState.Closed)
    {
        return "Database connection: Closed";
    }
    else
    {
        return "";
    }
}

Upvotes: 3

diogod
diogod

Reputation: 151

I think you want to open your connection on the test constructor. Another alternative could be passing the sqlCEconn that you create on Form1_Load as an argument to your test object

Upvotes: 0

Alex
Alex

Reputation: 8937

In your check you create new connection, meanwhile it should follow in your testCHECK as param

public string testCHECK(SqlCeConnection sqlCEconn)

and your call

Toolstrip1.Text = testconnection.testCHECK(sqlCEconn)

Upvotes: 0

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

You should create a class called SqlManager or something and manage connections and operations against the database from there.

That will allow multiple unrelated classes to work with exactly the same connection object.

Upvotes: 0

Oded
Oded

Reputation: 499382

You are creating a new connection object in your test class - if you want to check the state of the existing connection, you should pass it in to the test class or even the testCheck method:

class test
{
    public string testCHECK(SqlCeConnection sqlCEconn)
    {
        if (sqlCEconn.State == ConnectionState.Open)
        {
            return "Database connection: Open";
        }
        if (sqlCEconn.State == ConnectionState.Closed)
        {
            return "Database connection: Closed";
        }
        else
        {
            return "";
        }
    }
}

And call it like this:

sqlCEconn.Open();
test testconnection = new test();

Toolstrip1.Text = testconnection.testCHECK(sqlCEconn);

Upvotes: 1

Related Questions