Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 15th, 2004, 01:10 PM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default global variable in c#

hey,
i need help with c#.
i want to define a global variable that all classes know this variable without doing 'new' in every class where i want to use this variable.

this is my program - tell me whats wrong or what should i do? (pseudo code)

public class AA
{
    private static SqlConnection sqlCon;
    public AA()//constructor
    {
    sqlCon = new SqlConnection();
    sqlCon.ConnectionString = ".....";
    }

       public SqlConnection con2
    {
    get{return sqlCon;}
    set{SqlCon = value;}
    }

    }
}


when i try to use sqlCon in other classes there is an error:
 "Object reference not set to an instance of an object"

class BB
{
AA a;
  public BB()//constructor
  {
   . . . .
  }

  void func1()
  {
   a.con2.open(); <----------- this is the error line
   ..........
   ..........
   }
}


thanking you in advance
koby


 
Old April 15th, 2004, 01:42 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

That's because you only declared the private SqlConnection as static. You should change the public property con2 to static as well.

However, I am not so sure if this is a wise design decision. With this design, all classes that use AA (nice name, BTW ;) ) share *one* global connection object. I think this will result in poor performance, as all requests are queued up. If one of the classes that use AA open a DataReader, the connection is blocked until you close the reader.

A better approach is to have a public connection string that other classes can use. Alternatively, create a class that abstracts the data access code for you. e.g.:
Code:
public class MyDataClass()
{
  public DataSet ExecuteSQL(string sql)
  {
    string connectionString = AA.ConnectionString;
    // Create connection here with the ConnectionString from AA
    // return a dataset constructed with the connection created in
    // this method
  }
}
If you're trying to abstract your data access code from your UI or business layer, take a look here: http://msdn.microsoft.com/library/de...ml/daab-rm.asp
This is the Data Access Application Block from Microsoft and demonstrates a nice (and ready to use) way to implement a data access class.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Pumpkin by Tricky (Track 5 from the album: Maxinquaye)

 
Old April 17th, 2004, 01:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

take a look at this http://p2p.wrox.com/topic.asp?TOPIC_ID=2064.

HTH.

Always:),
Hovik Melkomian.
 
Old April 22nd, 2004, 05:56 AM
Authorized User
 
Join Date: Apr 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi there (hope this what you mean)

My connection string is stored in a public static string variable attached to my mainform. my mainform is declared public, so you can see all the form variables ans so forth. Then all you have to is SqlConnection cnfind = new SqlConnection(mdiMain.strConnectString);

This way you create a new connection and the danger of a deadlock is smaller

Hope it hels

jan

dude_in_africa





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
Global Variable techfreak123 XSLT 0 August 3rd, 2006 07:45 AM
global variable melvik C# 37 March 2nd, 2005 02:07 PM
global variable? milk_vanilla Classic ASP Basics 3 January 8th, 2005 06:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.