static types cannot be used as return types

S

Sanjay Pais

I have a class:
public static class HOW_GOOD
{
static string[] mstrHowGood =
{
"A",
"G",
"NTB",
"T"
};
public static string AWESOME
{
get
{
return mstrHowGood[0];
}
}
public static string GREAT
{
get
{
return mstrHowGood[1];
}
}
public static string NOT_TOO_BAD
{
get
{
return mstrHowGood[2];
}
}
public static string TERRIBLE
{
get
{
return mstrHowGood[2];
}
}

}


But I get an error when I use it as a return type
HOW_GOOD': static types cannot be used as return types

I tried to do this
Public class MyClass
{
public HOW_GOOD HowAreYouDoing(string MyName)
{
return HOW_GOOD.NOT_TOO_BAD;
}
}

Is there any way around this? Is this the "imporper programming" ?

Thanks in advance

Sanjay
 
A

Adam Clauss

public static class HOW_GOOD
Why is your class itself declared as static?
I don't think I've ever seen something like that done before.
I'm not even sure what that means for a class.
 
J

Jon Skeet [C# MVP]

Sanjay Pais said:
I have a class:
public static class HOW_GOOD

Is there any way around this? Is this the "imporper programming" ?

Yes. The point of a static class is that there are no instances of it -
you therefore could never have a variable of that type which had any
value other than null.

In your code, you have:

public HOW_GOOD HowAreYouDoing(string MyName)
{
return HOW_GOOD.NOT_TOO_BAD;
}

but the type of HOW_GOOD.NOT_TOO_BAD is not HOW_GOOD - it's string! You
should therefore declare the method to return a string, and all will be
fine.

What are you actually trying to accomplish?
 
J

Jon Skeet [C# MVP]

Adam Clauss said:
Why is your class itself declared as static?
I don't think I've ever seen something like that done before.
I'm not even sure what that means for a class.

It's a new feature of C# 2.0. It means there are no constructors (none
whatsoever - something impossible in C# v1), the class is final, and
all members (other than those inherited) must be static.

I'm not sure (having not installed the beta of VS 2005 yet) whether a
static class can derive from another class or not, although the
usefulness of it would be questionable even if you could.
 
S

Sanjay Pais

it started off by me wanting to create a string enum
public enum HOW_GOOD

{

AWESOME = "A",

GREAT= "G",

NOT_TOO_BAD = "NTB",

TERRIBLE="T"

}

i wanted to use this as a parameter/ return type for methods

Eg
public HOW_GOOD HowAreYouFeeling(string MyName)

{
return HOW_GOOD.NOT_TOO_BAD;
}
or

public string HowAreYouFeeling(HOW_GOOD m_HowGood)

{

return "Not Too Bad";
}


Adam Clauss said:
public static class HOW_GOOD
Why is your class itself declared as static?
I don't think I've ever seen something like that done before.
I'm not even sure what that means for a class.

--
Adam Clauss

Sanjay Pais said:
I have a class:
public static class HOW_GOOD
{
static string[] mstrHowGood =
{
"A",
"G",
"NTB",
"T"
};
public static string AWESOME
{
get
{
return mstrHowGood[0];
}
}
public static string GREAT
{
get
{
return mstrHowGood[1];
}
}
public static string NOT_TOO_BAD
{
get
{
return mstrHowGood[2];
}
}
public static string TERRIBLE
{
get
{
return mstrHowGood[2];
}
}

}


But I get an error when I use it as a return type
HOW_GOOD': static types cannot be used as return types

I tried to do this
Public class MyClass
{
public HOW_GOOD HowAreYouDoing(string MyName)
{
return HOW_GOOD.NOT_TOO_BAD;
}
}

Is there any way around this? Is this the "imporper programming" ?

Thanks in advance

Sanjay
 
A

Adam Clauss

Jon Skeet said:
It's a new feature of C# 2.0. It means there are no constructors (none
whatsoever - something impossible in C# v1), the class is final, and
all members (other than those inherited) must be static.

I'm not sure (having not installed the beta of VS 2005 yet) whether a
static class can derive from another class or not, although the
usefulness of it would be questionable even if you could.

So...
It is basically:

public class SomeClass
{
private SomeClass() { }

<members here>
}

With the execption that all members must be static?

Why did they even bother? I don't mean to come off rude... but I just don't
see where it would be particularly useful.
 
J

Jon Skeet [C# MVP]

Adam Clauss said:
So...
It is basically:

public class SomeClass
{
private SomeClass() { }

<members here>
}

With the execption that all members must be static?

And without even a private constructor.
Why did they even bother? I don't mean to come off rude... but I just don't
see where it would be particularly useful.

It's putting a common pattern directly into the language. It makes it
easier to make sure that you don't forget to put in the private
construtor, or make some members non-static accidentally.

As one who's got a few unit tests for classes where the unit tests
solely exist to make sure that the above is actually true, it would be
a nicer not to have to worry about it :)
 
A

Adam Clauss

Jon Skeet said:
It's putting a common pattern directly into the language. It makes it
easier to make sure that you don't forget to put in the private
construtor, or make some members non-static accidentally.

As one who's got a few unit tests for classes where the unit tests
solely exist to make sure that the above is actually true, it would be
a nicer not to have to worry about it :)

Interesting, thanks for explaining that.
 
C

Chris Dunaway

Isn't that exactly what a Module is in VB? A static class in which
all members are static?
 

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