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

Discussion in 'ASP .Net' started by Electric Co., Aug 9, 2006.

  1. Electric Co.

    Electric Co. Guest

    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;
    }
    // ~~~~~
    }
    }
     
    Electric Co., Aug 9, 2006
    #1
    1. Advertising

  2. Electric Co.

    tdavisjr Guest

    "Electric Co." <> wrote in message
    news:...

    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;
    > }
    > // ~~~~~
    > }
    > }
    >
     
    tdavisjr, Aug 9, 2006
    #2
    1. Advertising

  3. Electric Co.

    tdavisjr Guest

    "Electric Co." <> wrote in message
    news:...
    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;
    > }
    > // ~~~~~
    > }
    > }
    >
     
    tdavisjr, Aug 9, 2006
    #3
  4. Electric Co.

    Electric Co. Guest

    Hi,

    Thanx, I thought your suggestion was brilliant.

    Jim
     
    Electric Co., Aug 9, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G. Burton
    Replies:
    2
    Views:
    59,947
    Sudsy
    Feb 21, 2004
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,520
    Tony Morris
    Oct 19, 2004
  3. Nita Rai
    Replies:
    1
    Views:
    464
    mlimber
    May 9, 2006
  4. Replies:
    1
    Views:
    119
  5. rickman
    Replies:
    5
    Views:
    467
    rickman
    Mar 30, 2013
Loading...

Share This Page