静态类型不能用作返回类型 [英] static types cannot be used as return types

查看:233
本文介绍了静态类型不能用作返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂课:

公共静态课HOW_GOOD

{

静态字符串[] mstrHowGood =

{

" A",

" G",

" NTB",

" ; T"

};

公共静态字符串真棒

{

get

{

返回mstrHowGood [0];

}

}

公共静态字符串GREAT

{

get

{

返回mstrHowGood [1];

}

}

公共静态字符串NOT_TOO_BAD

{

get

{

return mstrHowGood [2];

}

}

public static string TERRIBLE

{

get

{

返回mstrHowGood [2];

}

}


}

但是当我将它用作返回类型时出现错误

HOW_GOOD'':静态类型不能用作返回类型


我试图这样做

公共课MyClass

{

public HOW_GOOD HowAreYouDoing(string MyName)

{

返回HOW_GOOD.NOT_TOO_BAD;

}

}


有什么方法吗?这是imporper编程吗? ?


提前致谢


Sanjay

解决方案

>公共静态类HOW_GOOD

为什么你的类本身被声明为静态?

我不认为我曾见过类似的事情。

我甚至不确定这对班级意味着什么。


-

Adam Clauss

" Sanjay Pais" < SA **** @ nospam.com>在消息中写道

新闻:ez ************** @ tk2msftngp13.phx.gbl ...

我有一节课:
公共静态类HOW_GOOD
{
静态字符串[] mstrHowGood =
{
" A",
" G",
NTB,
T
};
公共静态字符串AWESOME
{
获取
{
返回mstrHowGood [0 ];
}
}
公共静态字符串GREAT
{
获取
返回mstrHowGood [1];
}
}
公共静态字符串NOT_TOO_BAD
{
获取
{
返回mstrHowGood [2];
}
} {
获得
返回mstrHowGood [2];
}
}
}

但是当我将它用作返回类型时出现错误
HOW_GOOD'':静态类型不能用作返回类型

我试图这样做
公共课MyClass
{公共HOW_GOOD HowAreYouDoing(字符串MyName)
{
返回HOW_GOOD。 NOT_TOO_BAD;
}
}

有什么方法吗?这是imporper编程吗? ?

提前致谢

Sanjay



Sanjay Pais< sa * ***@nospam.com>写道:

我有一个班级:
公共静态课HOW_GOOD


< snip>

有没有绕过这个?这是imporper编程吗? ?




是的。静态类的要点是没有它的实例 -

因此你永远不会有一个除null之外的任何

值的变量。 br />

在你的代码中,你有:


public HOW_GOOD HowAreYouDoing(string MyName)

{

返回HOW_GOOD.NOT_TOO_BAD;

}


但是HOW_GOOD.NOT_TOO_BAD的类型不是HOW_GOOD - 它是字符串!你应该声明方法返回一个字符串,所有这些都将是

罚款。


你究竟在尝试什么?完成?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


Adam Clauss< ca ***** @ tamu.edu>写道:

public static class HOW_GOOD


为什么你的类本身被声明为静态?
我不认为我我以前见过这样的事情。
我甚至不确定这对于一个班级来说意味着什么。




这是一个新的C#2.0的功能。这意味着没有构造函数(没有

在C#v1中是不可能的),类是最终的,并且

所有成员(除了继承的成员)必须是静态的。


我不确定(还没有安装VS 2005的测试版)

静态类是否可以派生自另一个类,虽然

的有用性即使你可以也是有问题的。


-

Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ 〜双向飞碟

如果回复小组,请不要给我发邮件


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

解决方案

> 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



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


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


这篇关于静态类型不能用作返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆