Getting namespace errors on compile

T

tshad

I have a file that I converted from VB.Net to C# that works fine in VB.Net
when I compile but not in C# using the same libraries.

The error I am getting is:

PageInit.cs(9,7): error CS0138: A using namespace directive can only be
applied
to namespaces; 'System.Web.HttpCookie' is a class not a namespace

The code is:
**********************************
using System;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
using System.Web.HttpCookie;
using System.Web.HttpCookieCollection;
using System.Web.HttpResponse;
using System.Web.HttpRequest;
using System.Web.HttpContext;
using System.Web.HttpApplication;
using System.Web.HttpApplicationState;
using System.Collections;
using Microsoft.VisualBasic;

namespace MyFunctions
{

public class PageInit
{

public static void PageSetup(MyFunctions.Page thePage)
{
string url;
string stemp;
string urlNoParams;
Label UserLoggedOn = (Label)thePage.FindControl("UserLoggedOn");
Label UserLoggedOnLabel =
(Label)thePage.FindControl("UserLoggedOnLabel");
if ((UserLoggedOn != null)) {
if (HttpContext.Current.session("LoggedIn") != null) {
if (HttpContext.Current.session("firstName") != null) {
UserLoggedOn.Text = UserLoggedOn.Text +
HttpContext.Current.session("firstName");
if (HttpContext.Current.session("lastName") != null)
{
UserLoggedOn.Text = UserLoggedOn.Text + " " +
HttpContext.Current.session("lastName");
}
}
if ((UserLoggedOn != null)) {
UserLoggedOn.visible = true;
if ((UserLoggedOnLabel != null))
UserLoggedOnLabel.visible = true;
}
}
}
if ((!thePage.IsRefresh)) {
if (((HttpContext.Current.session("User") != null)))
HttpContext.Current.session("LastPageVisited") =
HttpContext.Current.Session("User").LastPageVisited;
url =
HttpContext.Current.Request.Url.ToString.Substring(HttpContext.Current.Request.Url.ToString.LastIndexOf("/")
+ 1);
urlNoParams = url;
if (url.LastIndexOf("?") > -1)
urlNoParams = Url.Substring(0, url.LastIndexOf("?"));
if (((HttpContext.Current.session("User") != null)))
HttpContext.Current.Session("User").LastPageVisited =
urlNoParams;
url = "";
urlNoParams = "";
if ((HttpContext.Current.Request.UrlReferrer != null)) {
url =
HttpContext.Current.Request.UrlReferrer.ToString.Substring(HttpContext.Current.Request.UrlReferrer.ToString.LastIndexOf("/")
+ 1);
if (url.LastIndexOf("?") > -1) {
urlNoParams = Url.Substring(0,
url.LastIndexOf("?"));
}
}
}

// Check to see if we are already at disabledAccount - if yes
don't test for /employer/ - we are already there
// if no - then check if we are a company account (/employer/)
if so & Company disabled - send to disabledAccount.aspx

HttpContext.Current.session("stemp") =
HttpContext.Current.Request.Url.ToString.IndexOf("/employer/");
if
(HttpContext.Current.Request.Url.ToString.IndexOf("disabledAccount.aspx")
== -1) {
if
(HttpContext.Current.Request.Url.ToString.IndexOf("/employer/") > -1) {
if ((HttpContext.Current.session("CompanyDisabled") !=
null)) {
HttpContext.Current.Response.Redirect("disabledAccount.aspx");
}
}
}
}
}
}
************************************

In the VB.Net code it is:
*******************************************8
Imports System
Imports System.Web
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.SessionState
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.HttpCookie
Imports System.Web.HttpCookieCollection
Imports System.Web.HttpResponse
Imports System.Web.HttpRequest
imports System.Web.HttpContext
Imports System.Web.HttpApplication
Imports System.Web.HttpApplicationState
Imports System.Collections
Imports Microsoft.VisualBasic
***************************************************

Where Imports is the same as Using. So why am I getting the error?

The errors are from HTTPCookie to HttpApplicationState.

My compiler line for c# that doesn't work is:

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc /t:library PageInit.cs
/r:system.web.dll /r:system.data.dll /r:system.dll
/r:Microsoft.VisualBasic.dll /r:refresh.dll

And the compiler line for vb.net that does work is:
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc /t:library PageInit.vb
/r:system.web.dll /r:system.data.dll /r:system.dll
/r:Microsoft.VisualBasic.dll /r:refresh.dll

As you can see virtually identical.

Thanks,

Tom
 
J

John B

tshad said:
I have a file that I converted from VB.Net to C# that works fine in VB.Net
when I compile but not in C# using the same libraries.

The error I am getting is:

PageInit.cs(9,7): error CS0138: A using namespace directive can only be
applied
to namespaces; 'System.Web.HttpCookie' is a class not a namespace

The code is:
**********************************
using System;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
using System.Web.HttpCookie;
^^^^
Have you tried removing the offending using directive?

Try and remove it and see if that fixes it.

<...>

JB
 
F

Francois Malgreve

Hi,

In C# you can "import" namespace ONLY with the using keyword. When you
import a certain namespace, all the classes under that namespace are
directly available without having to specify their namespace in the code.

So your code should compile if you replace your using section by:

using System;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Microsoft.VisualBasic;

The line "using System.Web;" will make that all classes such as HttpCookie,
HttpCookieCollection, HttpResponse available directly (which you were trying
to import separatly and which is illegal in C#).

Apparently in VB.NET you can "import" a class and not just the namespace.
Note that in your list of Imports, you were already importing the namespace
System.Web, so it was unecessary to import any of the classes under that
namespace separatly.
This means that your code in VB.NET should also compile with the following
imports statements:

Imports System
Imports System.Web
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.SessionState
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports Microsoft.VisualBasic

I hope that this further information helps.

Best regards,

Francois Malgreve
http://www.malgreve.net
 
T

tshad

Francois Malgreve said:
Hi,

In C# you can "import" namespace ONLY with the using keyword. When you
import a certain namespace, all the classes under that namespace are
directly available without having to specify their namespace in the code.

So your code should compile if you replace your using section by:

using System;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Microsoft.VisualBasic;

The line "using System.Web;" will make that all classes such as
HttpCookie, HttpCookieCollection, HttpResponse available directly (which
you were trying to import separatly and which is illegal in C#).

That did it.

Thanks,

Tom
 

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

Similar Threads


Top