473,598 Members | 3,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_TO O_BAD;
}
}

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

Thanks in advance

Sanjay

Nov 17 '05 #1
10 16253
> 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******** ******@tk2msftn gp13.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_TO O_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_TO O_BAD;
}

but the type of HOW_GOOD.NOT_TO O_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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Adam Clauss <ca*****@tamu.e du> 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.co m>
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 HowAreYouFeelin g(string MyName)

{
return HOW_GOOD.NOT_TO O_BAD;
}
or

public string HowAreYouFeelin g(HOW_GOOD m_HowGood)

{

return "Not Too Bad";
}
"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:11******** *****@corp.supe rnews.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******** ******@tk2msftn gp13.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_TO O_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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.e du> 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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******** *************@z 14g2000cwz.goog legroups.com...
Isn't that exactly what a Module is in VB? A static class in which
all members are static?

Nov 17 '05 #10

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

Similar topics

2
3048
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 is how can I write a C++ method that will either return a boost wrapped C++ class or a Python object type depending on some internal value?
7
2144
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
1997
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
4250
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 abstract new System.Data.IDbConnection Connection }
2
1472
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 extent, but causes other problems that occur with explicit definitions and also leads to much more code that is also more difficult to maintain. The new keyword is not even an option, if used in the example below Offspring on a Dog when in the...
6
2544
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 inherit a class. The best workaround and one that I use is the new keyword and cast the base class reference, but in my opinion this is a bad practice leading to error prone code. Can someone from MS chime in on this and provide some feedback?
8
2172
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 virtual destructor virtual void draw() = 0; // A pure virtual function virtual void move() = 0; ...
3
4544
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 = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
3
1975
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: private class C<T> { public static int Counter = 0; }
0
7991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8395
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8398
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8265
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.