473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

static types cannot be used as return types

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

Nov 17 '05 #1
10 16213
> 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" <sa****@nospam.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #2
Sanjay Pais <sa****@nospam.com> wrote:
I have a class:
public static class HOW_GOOD
<snip>
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Adam Clauss <ca*****@tamu.edu> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
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" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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" <sa****@nospam.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
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


Nov 17 '05 #5
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.

--
Adam Clauss
Nov 17 '05 #6
Adam Clauss <ca*****@tamu.edu> wrote:
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?


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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.

--
Adam Clauss
Nov 17 '05 #8
Isn't that exactly what a Module is in VB? A static class in which
all members are static?

Nov 17 '05 #9
Yes.

"Chris Dunaway" <du******@gmail.com> schrieb im Newsbeitrag
news:11*********************@z14g2000cwz.googlegro ups.com...
Isn't that exactly what a Module is in VB? A static class in which
all members are static?

Nov 17 '05 #10
I think he's trying to simulate a enum that's not integer based, which is not
possible.

"Jon Skeet [C# MVP]" wrote:
Sanjay Pais <sa****@nospam.com> wrote:
I have a class:
public static class HOW_GOOD


<snip>
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Knight | last post by:
Hello, I'm new to Boost & Python and I'm diving straight in by trying to write an extension module to a third party library. Foolishness probably, but I don't have much choice! My question...
7
by: Alex Vinokur | last post by:
Hello, Here is some program with virtual constructors. Is there any difference between * clone1() vs. clone2() * create1() vs. create2() ? It seems that it should be.
14
by: Stefan Slapeta | last post by:
Hi, this code does not compile in C#: class base_class {} class derived_class : base_class {} class A { public virtual base_class f()
13
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public...
2
by: Mike | last post by:
I keep running into the scenario below over and over again. Currently I get around not having covariant return types by using an interface and explicit property definitions which works to some...
6
by: miked | last post by:
Why are there still no covariant return types? All searches reveal no workarounds accept for using an interface which is a real pain. I wind up missing this capability almost every time I...
8
by: Alex Vinokur | last post by:
Here is a code from http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 -------------------------------------- class Shape { public: virtual ~Shape() { } // A...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
3
by: Samuel R. Neff | last post by:
Is there any way to declare a static variable within a generic type definition and have that variable be shared across all constructed generic types? For example, how can I modify this code: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.