Newbie: Static Method Instantiating Itself To Acess Non-static Property's. Ok?

E

Electric Co.

Hello,

I am newbie to the group so hi all! My name is Jim and I am a
codaholic. Ok so now that is out of the way.


I am new to using all things static and I am wondering if what I am
doing is going to get me in trouble.

I am using the component designer to build my Sql Server objects for
me. I then expose methods such as getDataSet as static methods. In
order to leverage the non-static properties that the designer creates
for me I instantiate a new object of the component designer and set its
properties accordingly (mainly the SqlServer connection on the command
objects so that each Sql server command has its own connection).

One thing of note:

1.) Private methods are exposed to the New Instance of the component
designer inside of the static method. (Makes me a bit concerned that
something funny is going on)

The code below gives an example.



using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data.SqlClient;

namespace GoDAT.Venue
{

public class Venue : System.ComponentModel.Component
{
// ~~~~
// ~~~~
#region CONSTRUCTOR
public Venue(System.ComponentModel.IContainer container)
{
container.Add(this);
InitializeComponent();
}
public Venue()
{
InitializeComponent();
}
#endregion
// ~~~~
// ~~~~

// ~~~~
// ~~
#region DISPOSAL
protected override void Dispose( bool disposing )
{
//blah, blah, blah
}
#endregion

private System.Data.SqlClient.SqlCommand sqlSelect_Venue;
private System.Data.SqlClient.SqlCommand sqlInsert_Venue;
private System.Data.SqlClient.SqlCommand sqlUpdate_Venue;
private System.Data.SqlClient.SqlCommand sqlDelete_Venue;
private System.Data.SqlClient.SqlDataAdapter sqlDA_Venue;
private GoDAT.Venue.dsVenue dsVenue1;
// ~~~~~
// ~~~~~

// ~~~~
// ~~~~
#region Component Designer generated code
// ALL THAT GREAT CODE GENERATED FOR FREE!!!!
// Or rather for the price of VS.NET and the
// hours of trial and error trying to figure
// out how to best leverage it.
#endregion
// ~~~~
// ~~~~

// ~~~~
public static GoDAT.Venue.dsVenue Get_dsVenue()
{

Venue tmpThis = new Venue();
GoDAT.Venue.dsVenue tmpDS = tmpThis.dsVenue1;
SqlDataAdapter adapter = tmpThis.sqlDA_Venue;

tmpThis.sqlSelect_Venue.Connection = SQLConnMgr.GetSqlConn();
tmpThis.sqlSelect_Venue.Connection.Open();
try
{
adapter.Fill(tmpDS);
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(("[Get_dsVenue] Exception " +
e.Message));
}
finally
{
adapter.Dispose();
tmpThis.sqlSelect_Venue.Connection.Close();
tmpThis.sqlSelect_Venue.Connection = null;
tmpThis = null;
}

return tmpDS;
}
// ~~~~~
}
}
 
T

tdavisjr

What you did seems to be ok, however I would create a private static field
to your class and create your Venue object there. E.g

private static Venue venue = new Venue();

Then this variable could be access by all other methods in this class and
you only have to create this object once. Also, if you want to prevent this
class from begin created by some client code you could also make your
constructor private.

HTH


Hello,

I am newbie to the group so hi all! My name is Jim and I am a
codaholic. Ok so now that is out of the way.


I am new to using all things static and I am wondering if what I am
doing is going to get me in trouble.

I am using the component designer to build my Sql Server objects for
me. I then expose methods such as getDataSet as static methods. In
order to leverage the non-static properties that the designer creates
for me I instantiate a new object of the component designer and set its
properties accordingly (mainly the SqlServer connection on the command
objects so that each Sql server command has its own connection).

One thing of note:

1.) Private methods are exposed to the New Instance of the component
designer inside of the static method. (Makes me a bit concerned that
something funny is going on)

The code below gives an example.



using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data.SqlClient;

namespace GoDAT.Venue
{

public class Venue : System.ComponentModel.Component
{
// ~~~~
// ~~~~
#region CONSTRUCTOR
public Venue(System.ComponentModel.IContainer container)
{
container.Add(this);
InitializeComponent();
}
public Venue()
{
InitializeComponent();
}
#endregion
// ~~~~
// ~~~~

// ~~~~
// ~~
#region DISPOSAL
protected override void Dispose( bool disposing )
{
//blah, blah, blah
}
#endregion

private System.Data.SqlClient.SqlCommand sqlSelect_Venue;
private System.Data.SqlClient.SqlCommand sqlInsert_Venue;
private System.Data.SqlClient.SqlCommand sqlUpdate_Venue;
private System.Data.SqlClient.SqlCommand sqlDelete_Venue;
private System.Data.SqlClient.SqlDataAdapter sqlDA_Venue;
private GoDAT.Venue.dsVenue dsVenue1;
// ~~~~~
// ~~~~~

// ~~~~
// ~~~~
#region Component Designer generated code
// ALL THAT GREAT CODE GENERATED FOR FREE!!!!
// Or rather for the price of VS.NET and the
// hours of trial and error trying to figure
// out how to best leverage it.
#endregion
// ~~~~
// ~~~~

// ~~~~
public static GoDAT.Venue.dsVenue Get_dsVenue()
{

Venue tmpThis = new Venue();
GoDAT.Venue.dsVenue tmpDS = tmpThis.dsVenue1;
SqlDataAdapter adapter = tmpThis.sqlDA_Venue;

tmpThis.sqlSelect_Venue.Connection = SQLConnMgr.GetSqlConn();
tmpThis.sqlSelect_Venue.Connection.Open();
try
{
adapter.Fill(tmpDS);
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(("[Get_dsVenue] Exception " +
e.Message));
}
finally
{
adapter.Dispose();
tmpThis.sqlSelect_Venue.Connection.Close();
tmpThis.sqlSelect_Venue.Connection = null;
tmpThis = null;
}

return tmpDS;
}
// ~~~~~
}
}
 
T

tdavisjr

You don't have to do what I suggested, you may been to weigh the pro's and
con's first before you do.


Hello,

I am newbie to the group so hi all! My name is Jim and I am a
codaholic. Ok so now that is out of the way.


I am new to using all things static and I am wondering if what I am
doing is going to get me in trouble.

I am using the component designer to build my Sql Server objects for
me. I then expose methods such as getDataSet as static methods. In
order to leverage the non-static properties that the designer creates
for me I instantiate a new object of the component designer and set its
properties accordingly (mainly the SqlServer connection on the command
objects so that each Sql server command has its own connection).

One thing of note:

1.) Private methods are exposed to the New Instance of the component
designer inside of the static method. (Makes me a bit concerned that
something funny is going on)

The code below gives an example.



using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data.SqlClient;

namespace GoDAT.Venue
{

public class Venue : System.ComponentModel.Component
{
// ~~~~
// ~~~~
#region CONSTRUCTOR
public Venue(System.ComponentModel.IContainer container)
{
container.Add(this);
InitializeComponent();
}
public Venue()
{
InitializeComponent();
}
#endregion
// ~~~~
// ~~~~

// ~~~~
// ~~
#region DISPOSAL
protected override void Dispose( bool disposing )
{
//blah, blah, blah
}
#endregion

private System.Data.SqlClient.SqlCommand sqlSelect_Venue;
private System.Data.SqlClient.SqlCommand sqlInsert_Venue;
private System.Data.SqlClient.SqlCommand sqlUpdate_Venue;
private System.Data.SqlClient.SqlCommand sqlDelete_Venue;
private System.Data.SqlClient.SqlDataAdapter sqlDA_Venue;
private GoDAT.Venue.dsVenue dsVenue1;
// ~~~~~
// ~~~~~

// ~~~~
// ~~~~
#region Component Designer generated code
// ALL THAT GREAT CODE GENERATED FOR FREE!!!!
// Or rather for the price of VS.NET and the
// hours of trial and error trying to figure
// out how to best leverage it.
#endregion
// ~~~~
// ~~~~

// ~~~~
public static GoDAT.Venue.dsVenue Get_dsVenue()
{

Venue tmpThis = new Venue();
GoDAT.Venue.dsVenue tmpDS = tmpThis.dsVenue1;
SqlDataAdapter adapter = tmpThis.sqlDA_Venue;

tmpThis.sqlSelect_Venue.Connection = SQLConnMgr.GetSqlConn();
tmpThis.sqlSelect_Venue.Connection.Open();
try
{
adapter.Fill(tmpDS);
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(("[Get_dsVenue] Exception " +
e.Message));
}
finally
{
adapter.Dispose();
tmpThis.sqlSelect_Venue.Connection.Close();
tmpThis.sqlSelect_Venue.Connection = null;
tmpThis = null;
}

return tmpDS;
}
// ~~~~~
}
}
 

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

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top