471,325 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Namespaces and References...confused

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'.
Aug 18 '08 #1
9 1136
Paolo wrote:
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
Aug 18 '08 #2
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)"

"Arne Vajhøj" wrote:
Paolo wrote:
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
Aug 18 '08 #3
On Sun, 17 Aug 2008 18:26:03 -0700, Paolo
<Pa***@discussions.microsoft.comwrote:
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)"
Then you either need to create an instance of the "Utils" class, or (more
likely, given the nature of utility classes) you simply need to make the
methods static.

Pete
Aug 18 '08 #4
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)"

"Peter Duniho" wrote:
On Sun, 17 Aug 2008 18:26:03 -0700, Paolo
<Pa***@discussions.microsoft.comwrote:
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)"

Then you either need to create an instance of the "Utils" class, or (more
likely, given the nature of utility classes) you simply need to make the
methods static.

Pete
Aug 18 '08 #5
Paolo wrote:
"Peter Duniho" wrote:
>On Sun, 17 Aug 2008 18:26:03 -0700, Paolo
<Pa***@discussions.microsoft.comwrote:
>>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)"
Then you either need to create an instance of the "Utils" class, or (more
likely, given the nature of utility classes) you simply need to make the
methods static.
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
Aug 18 '08 #6
Arne: yes, I did a rebuild.

"Arne Vajhøj" wrote:
Paolo wrote:
"Peter Duniho" wrote:
On Sun, 17 Aug 2008 18:26:03 -0700, Paolo
<Pa***@discussions.microsoft.comwrote:
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)"
Then you either need to create an instance of the "Utils" class, or (more
likely, given the nature of utility classes) you simply need to make the
methods static.
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
Aug 18 '08 #7
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)" .

"Arne Vajhøj" wrote:
Paolo wrote:
"Peter Duniho" wrote:
On Sun, 17 Aug 2008 18:26:03 -0700, Paolo
<Pa***@discussions.microsoft.comwrote:
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)"
Then you either need to create an instance of the "Utils" class, or (more
likely, given the nature of utility classes) you simply need to make the
methods static.
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
Aug 18 '08 #8
On Sun, 17 Aug 2008 18:48:15 -0700, Paolo
<Pa***@discussions.microsoft.comwrote:
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)"
That error message only happens when accessing non-static members by the
type name instead of with an object reference.

So, you must not have fixed the "non-static" problem. Adding the "static"
keyword would do it, so either you put the keyword on the wrong member, or
the DLL itself hasn't been updated. The latter can happen for a variety
of reasons, including simply not building it, building the wrong build
type (e.g. building a "Debug" version, but testing your change with a
"Release" build), or failing to copy the DLL to some specific location
where it's supposed to be.

There's not really any practical way for anyone here to tell you exactly
what you've done wrong. For that matter, you have not even bothered to
post a concise-but-complete code sample that demonstrates the problem
(such a sample would have both the referencing module, and the declaring
module). I think you're just going to have to track down the problem on
your own. Just "cross your t's and dot your i's"; that is, check and
double-check everything and make sure that you're actually doing what you
think you're doing.

Pete
Aug 18 '08 #9
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.

"Peter Duniho" wrote:
On Sun, 17 Aug 2008 18:48:15 -0700, Paolo
<Pa***@discussions.microsoft.comwrote:
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)"

That error message only happens when accessing non-static members by the
type name instead of with an object reference.

So, you must not have fixed the "non-static" problem. Adding the "static"
keyword would do it, so either you put the keyword on the wrong member, or
the DLL itself hasn't been updated. The latter can happen for a variety
of reasons, including simply not building it, building the wrong build
type (e.g. building a "Debug" version, but testing your change with a
"Release" build), or failing to copy the DLL to some specific location
where it's supposed to be.

There's not really any practical way for anyone here to tell you exactly
what you've done wrong. For that matter, you have not even bothered to
post a concise-but-complete code sample that demonstrates the problem
(such a sample would have both the referencing module, and the declaring
module). I think you're just going to have to track down the problem on
your own. Just "cross your t's and dot your i's"; that is, check and
double-check everything and make sure that you're actually doing what you
think you're doing.

Pete
Aug 18 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jim Heavey | last post: by
9 posts views Thread by Patty O'Dors | last post: by
11 posts views Thread by Random | last post: by
8 posts views Thread by Doogie | last post: by
5 posts views Thread by Simon | last post: by

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.