Namespaces and References...confused

P

Paolo

I've created a class Library called Utilities (utilities.dll) with namespace
'Utilities' and a class 'Utils' which contains, say, function01, function02.

I've created another class Library called ScreenManager . I've added a
'using Utilities' directive at the head of this file, and also added a
reference to 'utilities.dll' . I have 3 classes in the Screenmanager
file.When I try to use function01 and function02 in any of the 3 classes I'm
getting an error "The name 'function01' does not exist in the current
context", same for any use of function02.

If I qualify my use e.g. Utilities.function01, I get an error "The type or
namespace name 'function01' does not exist in the namespace 'Utilities'".

So I'm confused as I thought the 'using' directive and adding a reference
would make my utilities 'visible'.
 
A

Arne Vajhøj

Paolo said:
I've created a class Library called Utilities (utilities.dll) with namespace
'Utilities' and a class 'Utils' which contains, say, function01, function02.

I've created another class Library called ScreenManager . I've added a
'using Utilities' directive at the head of this file, and also added a
reference to 'utilities.dll' . I have 3 classes in the Screenmanager
file.When I try to use function01 and function02 in any of the 3 classes I'm
getting an error "The name 'function01' does not exist in the current
context", same for any use of function02.

If I qualify my use e.g. Utilities.function01, I get an error "The type or
namespace name 'function01' does not exist in the namespace 'Utilities'".

So I'm confused as I thought the 'using' directive and adding a reference
would make my utilities 'visible'.

using Utilities;

and then calling Utils.function01 is what you want.

Arne
 
P

Paolo

Arne: I have tried that i.e.
using Utilities;

and then calling Utils.function01 is what you want.

I get a different error namely:

"An object reference is required for the non-static method, field or
property 'Utilities.Utils.function01(params)"
 
P

Paolo

Peter: Ok, I've gone back to my 'Utilities.Utils' class and changed the
declaration of function01 from 'public void' to 'public static void'. This
gives the message "An object reference is required for the non-static method,
field or property 'Utilities.Utils.function01(params)"
 
A

Arne Vajhøj

Paolo said:
Peter: Ok, I've gone back to my 'Utilities.Utils' class and changed the
declaration of function01 from 'public void' to 'public static void'. This
gives the message "An object reference is required for the non-static method,
field or property 'Utilities.Utils.function01(params)"

Have you rebuild the DLL after making the method static ?

Arne
 
P

Paolo

Arne: I've made all of the methods in my Utilities.Utils class static and
that solved the problem I just referred to.

However when I go back to my ScreenManager and 'use' the Utilities methods,
say function01, I'm still getting the message "An object reference is
required for the non-static method, field or property
'Utilities.Utils.function01(params)" .
 
P

Paolo

Peter: your reply reminded me that having rebuilt the Utilities.dll I had
failed to copy it to a directory (myLibraries) where I intend to hold the
ones I produce myself. Therefore I was still referencing the 'old' - non
static - version and thus receiving the error messages. Having corrected
this, the error messages have, not surprisingly, disappeared.

A bit stupid really, but, in trying to learn C#, I'm guilty of forgetting
the basics. Thanks to you and Arne for your input.
 

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

Top