"using" question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles Stanley

    #1

    "using" question

    Hi all,
    I'm new to C#, and have two pretty simple questions:

    I have a class, in a .cs file by itself. I need to make use of the sole
    function in this class in another .cs file. Here's the code, filename
    bannertrack.cs

    First question - the function "LogImpr" users ServerVariables["REMOTE_ADD R"]
    + "')"; The compiler tells me that this is not included in the namespace.
    ??

    Second question - How do I call this function from another .cs file?

    namespace internetdating. bannertracking. bannertrack

    {

    using System;

    using System.Collecti ons;

    using System.Componen tModel;

    using System.Data;

    using System.Drawing;

    using System.Web;

    using System.Web.Sess ionState;

    using System.Web.UI;

    using System.Web.UI.W ebControls;

    using System.Web.UI.H tmlControls;

    using System.Data.Odb c;

    using System.Configur ation;

    /// <summary>

    /// STUFF HERE

    /// </summary>

    public class bannertrack: System.Web.UI.P age

    {


    public void LogImpr(System. Web.UI.WebContr ols.AdCreatedEv entArgs e)

    {

    String dsn = ConfigurationSe ttings.AppSetti ngs["idate"]; //Take
    connectionstrin g from Web.Config

    OdbcConnection myConnection = new OdbcConnection( dsn);

    string InsertStr = "insert into impressions (LinkURL, Timestamp, IP) Values
    ('" + e.NavigateUrl +

    "',NOW()" + ", '" + ServerVariables["REMOTE_ADD R"] + "')";

    OdbcCommand myCommand = new OdbcCommand(Ins ertStr, myConnection);

    myCommand.Conne ction.Open();

    try

    {

    myCommand.Execu teNonQuery();


    }

    finally

    {

    myCommand.Conne ction.Close();

    }

    }

    }

    }


  • Gary van der Merwe

    #2
    &quot;using&quo t; question

    Hi Charles

    You need to create an instance of the
    class "internetdating .bannertracking .bannertrack", then
    call the method.

    e.g.:

    using internetdating. bannertracking;
    ....
    bannertrack mybannertrack = new bannertrack()
    mybannertrack.L ogImpr(e)

    BUT...

    because the method is not specific to an instance of a
    class, you can make it static
    e.g.:
    public static void LogImpr (...)

    then you don't have to create an instance of the class to
    call the method
    e.g.:

    using internetdating. bannertracking;
    ....
    bannertrack .LogImpr(e)


    Hope this is a help.

    Are you not from a OOP language background? I not, try do
    some reading on OOP.

    Gary
    [color=blue]
    >-----Original Message-----
    >Hi all,
    >I'm new to C#, and have two pretty simple questions:
    >
    >I have a class, in a .cs file by itself. I need to make[/color]
    use of the sole[color=blue]
    >function in this class in another .cs file. Here's the[/color]
    code, filename[color=blue]
    >bannertrack. cs
    >
    >First question - the function "LogImpr" users[/color]
    ServerVariables["REMOTE_ADD R"][color=blue]
    >+ "')"; The compiler tells me that this is not included[/color]
    in the namespace.[color=blue]
    >??
    >
    >Second question - How do I call this function from[/color]
    another .cs file?[color=blue]
    >
    >namespace internetdating. bannertracking. bannertrack
    >
    >{
    >
    >using System;
    >
    >using System.Collecti ons;
    >
    >using System.Componen tModel;
    >
    >using System.Data;
    >
    >using System.Drawing;
    >
    >using System.Web;
    >
    >using System.Web.Sess ionState;
    >
    >using System.Web.UI;
    >
    >using System.Web.UI.W ebControls;
    >
    >using System.Web.UI.H tmlControls;
    >
    >using System.Data.Odb c;
    >
    >using System.Configur ation;
    >
    >/// <summary>
    >
    >/// STUFF HERE
    >
    >/// </summary>
    >
    >public class bannertrack: System.Web.UI.P age
    >
    >{
    >
    >
    >public void LogImpr[/color]
    (System.Web.UI. WebControls.AdC reatedEventArgs e)[color=blue]
    >
    >{
    >
    >String dsn = ConfigurationSe ttings.AppSetti ngs[/color]
    ["idate"]; //Take[color=blue]
    >connectionstri ng from Web.Config
    >
    >OdbcConnecti on myConnection = new OdbcConnection( dsn);
    >
    >string InsertStr = "insert into impressions (LinkURL,[/color]
    Timestamp, IP) Values[color=blue]
    >('" + e.NavigateUrl +
    >
    > "',NOW()" + ", '" + ServerVariables["REMOTE_ADD R"][/color]
    + "')";[color=blue]
    >
    >OdbcCommand myCommand = new OdbcCommand(Ins ertStr,[/color]
    myConnection);[color=blue]
    >
    >myCommand.Conn ection.Open();
    >
    >try
    >
    >{
    >
    >myCommand.Exec uteNonQuery();
    >
    >
    >}
    >
    >finally
    >
    >{
    >
    >myCommand.Conn ection.Close();
    >
    >}
    >
    >}
    >
    >}
    >
    >}
    >
    >
    >.
    >[/color]

    Comment

    • Peter Rilling

      #3
      Re: &quot;using&quo t; question

      ServerVariables cannot be called by itself. Remember that C# is a
      completely class based language. ServerVariable must be called on an
      instance of HttpRequest. You might be able to so something like
      "this.Request.S erverVariables[...]".

      For other classes to be able to call your method, they must either be given
      in instance of the class or you make the method static in which case all
      classes can have access to it.

      "Charles Stanley" <chuck@wizardwa re.com> wrote in message
      news:KK3fb.6243 $La.5097@fed1re ad02...[color=blue]
      > Hi all,
      > I'm new to C#, and have two pretty simple questions:
      >
      > I have a class, in a .cs file by itself. I need to make use of the sole
      > function in this class in another .cs file. Here's the code, filename
      > bannertrack.cs
      >
      > First question - the function "LogImpr" users[/color]
      ServerVariables["REMOTE_ADD R"][color=blue]
      > + "')"; The compiler tells me that this is not included in the namespace.
      > ??
      >
      > Second question - How do I call this function from another .cs file?
      >
      > namespace internetdating. bannertracking. bannertrack
      >
      > {
      >
      > using System;
      >
      > using System.Collecti ons;
      >
      > using System.Componen tModel;
      >
      > using System.Data;
      >
      > using System.Drawing;
      >
      > using System.Web;
      >
      > using System.Web.Sess ionState;
      >
      > using System.Web.UI;
      >
      > using System.Web.UI.W ebControls;
      >
      > using System.Web.UI.H tmlControls;
      >
      > using System.Data.Odb c;
      >
      > using System.Configur ation;
      >
      > /// <summary>
      >
      > /// STUFF HERE
      >
      > /// </summary>
      >
      > public class bannertrack: System.Web.UI.P age
      >
      > {
      >
      >
      > public void LogImpr(System. Web.UI.WebContr ols.AdCreatedEv entArgs e)
      >
      > {
      >
      > String dsn = ConfigurationSe ttings.AppSetti ngs["idate"]; //Take
      > connectionstrin g from Web.Config
      >
      > OdbcConnection myConnection = new OdbcConnection( dsn);
      >
      > string InsertStr = "insert into impressions (LinkURL, Timestamp, IP)[/color]
      Values[color=blue]
      > ('" + e.NavigateUrl +
      >
      > "',NOW()" + ", '" + ServerVariables["REMOTE_ADD R"] + "')";
      >
      > OdbcCommand myCommand = new OdbcCommand(Ins ertStr, myConnection);
      >
      > myCommand.Conne ction.Open();
      >
      > try
      >
      > {
      >
      > myCommand.Execu teNonQuery();
      >
      >
      > }
      >
      > finally
      >
      > {
      >
      > myCommand.Conne ction.Close();
      >
      > }
      >
      > }
      >
      > }
      >
      > }
      >
      >[/color]


      Comment

      • Charles Stanley

        #4
        Re: &quot;using&quo t; question


        Thanks everyone,

        Problem solved :-)


        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        Working...