Simple question

P

pawelr74

Hello all,
I'm beginner in c#. In my web application I have class myTool with few
public methods. How can I attached this class to other classes and use
method from myTool class.
In VB I can use Import, but if I try use using in C# I have
Error Message
The type or namespace name 'type/namespace' could not be found (are
you missing a using directive or an assembly reference?)

code:
myTool class is in folder App_Code

public class myTool
{

public int GetIdUser(string UserName)
{
............
}
public void NewUser(string UserName, string Imie, string Nazwisko)
{
.....
}
}

I try use method in class on root application:


using .....
using myTool ; /// error message
public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Login1_LoggedIn(object sender, EventArgs e)
{
Session["idUser"] = GetIdUser(Login1.UserName);
}
}

How can I correct my code?

Thanks for yours help

BR.

Pawel
 
F

Family Tree Mike

In VB you didn't use import to help what you have here. You never created
an
instance of the myTool class in your code. Something like the following
would
be necessary:

myTool t = new myTool();
Session["idUser"] = t.GetIdUser(Login1.UserName);

There is probably more to it than this though as the instance of myTool may
need to be kept around. We don't know enough about the myTool class to
know for sure.
 
H

Hans Kesting

(e-mail address removed) expressed precisely :
Hello all,
I'm beginner in c#. In my web application I have class myTool with few
public methods. How can I attached this class to other classes and use
method from myTool class.
In VB I can use Import, but if I try use using in C# I have
Error Message
The type or namespace name 'type/namespace' could not be found (are
you missing a using directive or an assembly reference?)

code:
myTool class is in folder App_Code

public class myTool
{

public int GetIdUser(string UserName)
{
............
}
public void NewUser(string UserName, string Imie, string Nazwisko)
{
.....
}
}

I try use method in class on root application:


using .....
using myTool ; /// error message
public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Login1_LoggedIn(object sender, EventArgs e)
{
Session["idUser"] = GetIdUser(Login1.UserName);
}
}

How can I correct my code?

Thanks for yours help

BR.

Pawel

Does that class (myTool) live in a namespace? If not, add something
like this around your class:

namespace MyToolsNamespace
{
// your class definition
}


The "using directive" accepts only namespaces, not classnames.
So that would be "using MyToolsNamespace;", using the example above.

And as Mike said, you need an instance of this class to use it's
methods.

By the way, the regular naming conventions specify that a classname
should start with an uppercase letter (MyTool).

Hans Kesting
 
J

Jon Skeet [C# MVP]

I'm beginner in c#. In my web application I have class myTool with few
public methods. How can I attached this class to other classes and use
method from myTool class.
In VB I can use Import, but if I try use using in C# I have
Error Message
The type or namespace name 'type/namespace' could not be found (are
you missing a using directive or an assembly reference?)

The using directive is used to make a *namespace* available, not a
type. What namespace is "myTool" in?

Jon
 
F

Family Tree Mike

In a web application the IDE does not add a namespace for these helper
classes. A namespace is not really necessary in this case. You are correct
that a using statement would need a namespace though.

Hans Kesting said:
(e-mail address removed) expressed precisely :
Hello all,
I'm beginner in c#. In my web application I have class myTool with few
public methods. How can I attached this class to other classes and use
method from myTool class.
In VB I can use Import, but if I try use using in C# I have
Error Message
The type or namespace name 'type/namespace' could not be found (are
you missing a using directive or an assembly reference?)

code:
myTool class is in folder App_Code

public class myTool
{

public int GetIdUser(string UserName)
{
............
}
public void NewUser(string UserName, string Imie, string Nazwisko)
{
.....
}
}

I try use method in class on root application:


using .....
using myTool ; /// error message
public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Login1_LoggedIn(object sender, EventArgs e)
{
Session["idUser"] = GetIdUser(Login1.UserName);
}
}

How can I correct my code?

Thanks for yours help

BR.

Pawel

Does that class (myTool) live in a namespace? If not, add something
like this around your class:

namespace MyToolsNamespace
{
// your class definition
}


The "using directive" accepts only namespaces, not classnames.
So that would be "using MyToolsNamespace;", using the example above.

And as Mike said, you need an instance of this class to use it's
methods.

By the way, the regular naming conventions specify that a classname
should start with an uppercase letter (MyTool).

Hans Kesting
 

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

Postback Question 2
Use of event handling 6
Pass values b/w forms using properties 4
RDP / C# 1
RDP - C# 0
serialize sorteddictionary-does this seem ok 13
invoke methods with params 7
Class casting. 9

Top