Variable accessible to all function within class

T

tedqn

I keep getting the error of not shared member or something. Basically
within a class, I have a constructor that takes in an sql connection
string and init a connection object to be accessible by all members
instead of init several connections locally for each member function .
Help!

public class CommonUtilObj
{ public SqlConnection g_objConnection; //global connection variable

// constructor takes in a connection string and open the sql
connection to be used by methods
public CommonUtilObj(String connStr)
{
g_objConnection = new SqlConnection(connStr);
g_objConnection.Open();
}

public void DeleteAd(int adID)
{
SqlCommand objCommand = g_objConnection.CreateCommand();
 
U

Uchiha Jax

Can you provide the rest of the code as I can't see any serious problems.

If the SqlConnection only needs to be accessed by the other members of the
class you can mark it as private instead of public.
However this implementation wont work with any static methods in the class.
Could this be the problem?

Jax
 
T

tedqn

I commented out the rest of the code and recompiled to be sure but
still get the error. On the aspx page that called it, the class init
works fine but when calling

CommonUtilObj.Close()

BC30469: Reference to a non-shared member requires an object reference.


namespace CommonUtil
{
public class CommonUtilObj
{ private SqlConnection g_objConnection;

public CommonUtilObj(String connStr)
{
g_objConnection = new SqlConnection(connStr);
g_objConnection.Open();
}

// close initialized objects
public void Close()
{ g_objConnection.Close();

}
}
}
 
U

Uchiha Jax

Oh, right.
No you can't do that if you call:

CommonUtilObj.Close();

Then it will look for a static member called close. The Close method is
instance based in your class.
To get access you need to have an object reference.

For example.

CommonUtilObj objRef = new CommonUtilObj("conn string");
objRef.Close(); // this will work

If you want it global in the sense that the rest of your work doesn't have
the reference to objRef (say it's out of scope) then do this:

namespace CommonUtil
{
public class CommonUtilObj
{

private static SqlConnection g_objConnection; // static

public static void InitObjRef(string conn) // static
{
g_objConnection = new CommonUtilObj(conn);
}

private CommonUtilObj(String connStr)
{
g_objConnection = new SqlConnection(connStr);
g_objConnection.Open();
}

// close initialized objects
public static void Close() // static
{
g_objConnection.Close();
}

Then you can do this:

CommonUtilObj.InitObjRef("conn string");
CommonUtilObj.Close(); /// this will work.

If you're looking to do this i'd seriously recommomend using the singleton
pattern instead of the example given here (as i'm just pointing out the
difference).
http://www.yoda.arachsys.com/csharp/singleton.html

You'll have to work out a better way of passing the connection string into
the singleton as the examples given in the link dont have any arguments in
the constructor, i'd recommend doing this through a configuration file as
it'll be easier in the longrun.

HTH

Jax
 
T

tedqn

Jax, thank you so much. As you pointed out, it was a silly mistake in
the aspx page itself - CommonUtilObj.Close() was supposed to be a ref
variable like "objRef.Close()" that you mentioned. Interestingly, the
server where the site hosts has .net version 1 on it. I cannot have two
separate SqlReaders opened at the same time using a single connection.
ie.

While(oReader.Read()) {
// call a function that init another Reader that pulls something
else
}
 
G

Guest

You might want to consider not having a "global" connection but a global
connection string. Open a new connection just when you need it, and close it
immediately. This way you are taking advantage of connection pooling. In a
web application, this is even more critical.
 
G

Guest

What is the code for a global connection string and where is it placed?

I can't find a way to declare a variable outside a class.
 
K

Kai Brinkmann [MSFT]

I suggest you place the connection string in your application configuration
file.

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daag.asp
(search for 'Using XML Application Configuration Files')

and
http://msdn.microsoft.com/library/d...n-us/cpguide/html/cpconconfigurationfiles.asp

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top