problems with static members

C

codymanix

Hi I have following weird problem:

I Have a class with the Name CustomerListView which contains a static field.
Additionally I have a Property in another class which returns a
CustomerListView, and its name is also CustomerListView:

class CustomerListView
{
public const int A = 0;
}

class Test
{
CustomerListView CustomerListView;

Test()
{
int i = CustomerListView.A; // compile error here!!!
}
}

I know I could rename either the class or the field CustomerListView. But,
theoretically, what if I couldn't rename them because the field
CustomerListView is defined in the base class which resides in a thrid party
library?

Is it possible to access the static field A (without using reflection)???

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no ****ing ads]
 
J

Jon Skeet [C# MVP]

codymanix said:
I Have a class with the Name CustomerListView which contains a static field.
Additionally I have a Property in another class which returns a
CustomerListView, and its name is also CustomerListView:

class CustomerListView
{
public const int A = 0;
}

class Test
{
CustomerListView CustomerListView;

Test()
{
int i = CustomerListView.A; // compile error here!!!
}
}

What compile error are you getting? The only one I'm getting is that i
isn't used...
 
C

cody

Jon Skeet said:
codymanix said:
I Have a class with the Name CustomerListView which contains a static field.
Additionally I have a Property in another class which returns a
CustomerListView, and its name is also CustomerListView:
[..]

What compile error are you getting? The only one I'm getting is that i
isn't used...

Sorry the code I posted was not correct, that means It was correct but it
should not be compile correct you know :)
Here is the real code:

class CustomerListView
{
public const int A = 0;
}
class Test
{
ListView CustomerListView;
public Test()
{
int i = CustomerListView.A; // compile error here!!!
}
}

Iam getting the error that "A" woulöd not be defined in ListView, because
there seems to be no way to explicitly tell the compiler that a mean the
field CustomerListView, nmot the class CustomerListView. I consider this a
serious design error in the C# programming language.
 
J

Jon Skeet [C# MVP]

Iam getting the error that "A" woulöd not be defined in ListView, because
there seems to be no way to explicitly tell the compiler that a mean the
field CustomerListView, nmot the class CustomerListView. I consider this a
serious design error in the C# programming language.

I don't think I'd call it a *serious* design error, but it could cause
a problem - *unless* the class in question is in a namespace, in which
case you can just use the fully qualified name of the class instead.
And in my experience, *all* production classes should be in a
namespace.

Oh, and I've just found another way of doing it - with a using alias:

using CustomerListViewClass = CustomerListView;

....

int i = CustomerListViewClass.A;

Not ideal, but it works.
 
D

David Martin

Not sure if this ever came out, but your variable A is not static, it's
const. Two very different beasts from my experience.

if you had declared A as: public static int A =0;
then perhaps your problem would go away. I personally try to not name my
variables exactly how I name my classes so that it's clear for me. Usually
just change the case.

-david


Iam getting the error that "A" woulöd not be defined in ListView, because
there seems to be no way to explicitly tell the compiler that a mean the
field CustomerListView, nmot the class CustomerListView. I consider this a
serious design error in the C# programming language.

I don't think I'd call it a *serious* design error, but it could cause
a problem - *unless* the class in question is in a namespace, in which
case you can just use the fully qualified name of the class instead.
And in my experience, *all* production classes should be in a
namespace.

Oh, and I've just found another way of doing it - with a using alias:

using CustomerListViewClass = CustomerListView;

....

int i = CustomerListViewClass.A;

Not ideal, but it works.
 
C

cody

Not sure if this ever came out, but your variable A is not static, it's
const. Two very different beasts from my experience.

const is implicity static.
if you had declared A as: public static int A =0;
then perhaps your problem would go away. I personally try to not name my
variables exactly how I name my classes so that it's clear for me. Usually
just change the case.

I cannot without violating coding rules: classes should use exacty like
public members
(like my constant) the pascal case convention.
 

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