"using" question

  • Thread starter Thread starter Charles Stanley
  • Start date Start date
C

Charles Stanley

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_ADDR"]
+ "')"; 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.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.Odbc;

using System.Configuration;

/// <summary>

/// STUFF HERE

/// </summary>

public class bannertrack: System.Web.UI.Page

{


public void LogImpr(System.Web.UI.WebControls.AdCreatedEventArgs e)

{

String dsn = ConfigurationSettings.AppSettings["idate"]; //Take
connectionstring from Web.Config

OdbcConnection myConnection = new OdbcConnection(dsn);

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

"',NOW()" + ", '" + ServerVariables["REMOTE_ADDR"] + "')";

OdbcCommand myCommand = new OdbcCommand(InsertStr, myConnection);

myCommand.Connection.Open();

try

{

myCommand.ExecuteNonQuery();


}

finally

{

myCommand.Connection.Close();

}

}

}

}
 
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.LogImpr(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
-----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 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_ADDR"]
+ "')"; 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.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.Odbc;

using System.Configuration;

/// <summary>

/// STUFF HERE

/// </summary>

public class bannertrack: System.Web.UI.Page

{


public void LogImpr
(System.Web.UI.WebControls.AdCreatedEventArgs e)
{

String dsn = ConfigurationSettings.AppSettings ["idate"]; //Take
connectionstring from Web.Config

OdbcConnection myConnection = new OdbcConnection(dsn);

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

"',NOW()" + ", '" + ServerVariables["REMOTE_ADDR"] + "')";

OdbcCommand myCommand = new OdbcCommand(InsertStr, myConnection);

myCommand.Connection.Open();

try

{

myCommand.ExecuteNonQuery();


}

finally

{

myCommand.Connection.Close();

}

}

}

}


.
 
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.ServerVariables[...]".

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.
 
Back
Top