Satheesh

Satheesh

  • NA
  • 1
  • 0

instanciating connection object

Jul 6 2006 5:08 AM

Hi All,


i am trying to create a connection object in my form. See the code given below

 public class Formx:System.Windows.Forms.Form
 {    
  string strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\personal\\dotnet\\Db1.mdb";   
  OleDbConnection con = new OleDbConnection(strConnect);
}
I have assigned the connection string in the variable strConnect. Now if i create the connection object in the class then it throws error. B'coz strConnect is an instance variable and it cannot be used to initialize other instance variables outside the method(in c#). so we have to make the variable either const or static. and it will not throw that error. am ok with that..

But, if i create the object in the constructor(as mentioned below) i am not getting that error even if the variable strConnect is not defined as static or Const.
even in this case we can apply the rule that instance variables cannot be used to initialize other instantce variable outside the method rt?? but how come it is working fine here.  

 public class Formx:System.Windows.Forms.Form
 {    
  string strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\personal\\dotnet\\Db1.mdb";     
  
  public Formx()
  {
   OleDbConnection con = new OleDbConnection(strConnect);
   
  }
  
  Can anyone explain me what is going behind the scenes??

Thanks in Advance,
Satheesh