473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

"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_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.AdCreatedEventAr gs 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();

}

}

}

}
Nov 15 '05 #1
3 1463
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 solefunction in this class in another .cs file. Here's the code, filenamebannertrack.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"]; //Takeconnectionstring 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();

}

}

}

}
.

Nov 15 '05 #2
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.

"Charles Stanley" <ch***@wizardware.com> wrote in message
news:KK3fb.6243$La.5097@fed1read02...
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.AdCreatedEventAr gs 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();

}

}

}

}

Nov 15 '05 #3

Thanks everyone,

Problem solved :-)
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Justin | last post by:
Hi. I decided to tidy up some of my queries, but I came across something that stumpt me: mysql> SELECT -> jobs.jobId, -> jobs.active, -> jobs.title, -> jobs.listed, -> ...
13
by: Jalal | last post by:
I am trying to use numeric_limits<double>::min() in an MFC application in Microsoft Visual C++.NET 2003. I am having some difficulties here. The following are the parts of a simple program I wrote...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
5
by: charliewest | last post by:
I've implemented the USING statement to ensure that my newly created sql connection closes when my method is finished using it. The USING statement is wrapped in try/catch error handling statement....
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
6
by: Thomas H | last post by:
Hi everyone, I've got a question that's more of a "style" question... Do you guys reference "this" for every object that's inherited from System.Web.UI.Page? For example, when you use the...
12
by: Steve Pope | last post by:
Compiling the following works on my system: file main.cpp: #include <iostream> namespace space { int foo; }
5
by: veaux | last post by:
I'm thinking this is easy but can't get it. I have a table with following: Table1 Date 1/1/2007 Table2 Type 0107 (This is MMYY of above) So I'm having trouble using a query to turn the...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.