将枚举作为参数传递 [英] Passing an enum as a parameter

查看:108
本文介绍了将枚举作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想我之前见过有人在代码中传递了一个emumeration。


有谁能告诉我这是可能的,也是我想要的原因。

非常感谢


最基本的问候


西蒙

Hi all,

I think I''ve seen someone passing an emumeration in code before.

Can anyone tell me if thats possible and why i would want to.

Many thanks

Kindest Regards

Simon

推荐答案

嗨西蒙,


你的意思是将enum作为参数传递给某些方法吗? br />

假设你有这个枚举:


public enum DocumentType

{

发票,

订单

}


以及将DocumentType枚举作为参数的一些方法:


public bool SaveDocument(字符串名称,DocumentType docType)

也许这不是一个非常好的例子,但确定可以传递一个枚举
$ b一个方法$ b。

-

问候,

Peter Jausovec

http://blog.jausovec。网

" Simon" < sh856531@microsofts_free_email_service.com> je napisalvsporoèilo

新闻:u6 ************** @ TK2MSFTNGP12.phx.gbl ...
Hi Simon,

Do you mean to pass enum as a parameter to some method ?

Let''s say you have this enum:

public enum DocumentType
{
invoice,
order
}

and some method that has DocumentType enum as a parameter:

public bool SaveDocument (string name, DocumentType docType)
Maybe this isn''t a very good example but sure it is possible to pass an enum
to a method.
--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Simon" <sh856531@microsofts_free_email_service.com> je napisal v sporoèilo
news:u6**************@TK2MSFTNGP12.phx.gbl ...
大家好,

我想我之前见过有人在代码中传递过数字。

任何人都可以告诉我这是否可能以及我为什么要这样做。

最诚挚的问候

Simon
Hi all,

I think I''ve seen someone passing an emumeration in code before.

Can anyone tell me if thats possible and why i would want to.

Many thanks

Kindest Regards

Simon



Peter Jausovec写道:
Peter Jausovec wrote:
嗨西蒙,

你的意思是将枚举作为参数传递给某种方法吗?

让我们说你有这个枚举:

公共枚举DocumentType
{
发票,
命令

和一些将DocumentType枚举作为参数的方法:

公共bool SaveDocument(字符串名称,DocumentType docType)

也许这不是一个非常好的例子但是确定可以通过枚举
来一种方法。
Hi Simon,

Do you mean to pass enum as a parameter to some method ?

Let''s say you have this enum:

public enum DocumentType
{
invoice,
order
}

and some method that has DocumentType enum as a parameter:

public bool SaveDocument (string name, DocumentType docType)
Maybe this isn''t a very good example but sure it is possible to pass an enum
to a method.




不幸的是,它无法通过这样一种方法的枚举。

因为它是从
System.Enum派生的特殊(用户定义)值类型,所以需要将基础类型传递给功能。


例如,要使用enum'的成员,你必须通过

他们分别指定他们的基础类型:


私有枚举MyEnum:int {ONE = 1,TWO = 2,THREE = 3}


private void DoIt(int i)

{

开关(i)

{

案例1:

Console.WriteLine( MyEnum:{0},i);

休息;

案例2:

Console.WriteLine(" MyEnum: {0}",i);

休息;

案例3:

Console.WriteLine(" MyEnum:{0}" ;,i);

休息;

默认:

休息;

}

}


this.DoIt((int)MyEnum.ONE);

this.DoIt((int)MyEnum.TWO);

this.DoIt((int)MyEnum.THREE);


这个窝打印:


MyEnum:1

MyEnum:2

MyEnum:3


对我来说似乎不太实用。枚举根本不是用于传递给函数本身而不是使用他们的

底层类型的成员。


也许有人MVP能够为枚举提供一些启示。


问候


凯瑟琳



Unfortunately it is not possible to pass an enum to a method this way.
Since it is a special (user-defined) value type derived from
System.Enum, you need to pass the underlying type to the function.

For example, to make use of the enum''s members you will have to pass
them seperately specifying their underlying type:

private enum MyEnum : int { ONE = 1, TWO = 2, THREE = 3 }

private void DoIt(int i)
{
switch (i)
{
case 1:
Console.WriteLine("MyEnum: {0}", i);
break;
case 2:
Console.WriteLine("MyEnum: {0}", i);
break;
case 3:
Console.WriteLine("MyEnum: {0}", i);
break;
default:
break;
}
}

this.DoIt((int)MyEnum.ONE);
this.DoIt((int)MyEnum.TWO);
this.DoIt((int)MyEnum.THREE);

This would print:

MyEnum: 1
MyEnum: 2
MyEnum: 3

Seems not very practical to me. Enums are simply not intended to be
passed to functions itself rather than their members using their
underlying type.

Maybe someone of the MVPs is able to shed some light on enums.

Regards

Catherine





我认为底层类型默认为整数


-

问候,

Peter Jausovec

http:// blog.jausovec.net

" Catherine Lowery" < CL ***** @ imap.cc> je napisal v sporocilo

news:32 ************* @ individual.net ...
Hi,

I think that the underlying type is integer by default

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Catherine Lowery" <cl*****@imap.cc> je napisal v sporocilo
news:32*************@individual.net ...
Peter Jausovec写道:
Peter Jausovec wrote:
嗨西蒙,

你的意思是将枚举作为参数传递给某些方法吗?

让我们说你有这个枚举:

公开枚举DocumentType
{
发票,
命令

和一些将DocumentType枚举为一个方法参数:

公共bool SaveDocument(字符串名称,DocumentType docType)

也许这不是一个非常好的例子但确定可以传递一个
枚举一个方法。
Hi Simon,

Do you mean to pass enum as a parameter to some method ?

Let''s say you have this enum:

public enum DocumentType
{
invoice,
order
}

and some method that has DocumentType enum as a parameter:

public bool SaveDocument (string name, DocumentType docType)
Maybe this isn''t a very good example but sure it is possible to pass an
enum to a method.



不幸的是,不可能以这种方式将枚举传递给方法。
因为它是从System派生的特殊(用户定义的)值类型.Enum,
你需要将基础类型传递给函数。

例如,要使用枚举的成员,你必须分别传递它们
指定他们的联合国derlying类型:

私有枚举MyEnum:int {ONE = 1,TWO = 2,THREE = 3}

私有无效DoIt(int i)
{
开关(i)
案例1:
Console.WriteLine(" MyEnum:{0}",i);
休息;
案例2:
Console.WriteLine(" MyEnum:{0}",i);
break;
案例3:
Console.WriteLine(" MyEnum:{ 0}",i);
休息;
默认:
休息;
}


this.DoIt((int )MyEnum.ONE);
this.DoIt((int)MyEnum.TWO);
this.DoIt((int)MyEnum.THREE);

这将打印:

MyEnum:1
MyEnum:2
MyEnum:3

对我来说似乎不太实用。 Enums根本不打算传递给函数本身而不是传递给它们使用其基础类型的成员。

也许MVP中的某个人能够对枚举有所了解。

问候

凯瑟琳



Unfortunately it is not possible to pass an enum to a method this way.
Since it is a special (user-defined) value type derived from System.Enum,
you need to pass the underlying type to the function.

For example, to make use of the enum''s members you will have to pass them
seperately specifying their underlying type:

private enum MyEnum : int { ONE = 1, TWO = 2, THREE = 3 }

private void DoIt(int i)
{
switch (i)
{
case 1:
Console.WriteLine("MyEnum: {0}", i);
break;
case 2:
Console.WriteLine("MyEnum: {0}", i);
break;
case 3:
Console.WriteLine("MyEnum: {0}", i);
break;
default:
break;
}
}

this.DoIt((int)MyEnum.ONE);
this.DoIt((int)MyEnum.TWO);
this.DoIt((int)MyEnum.THREE);

This would print:

MyEnum: 1
MyEnum: 2
MyEnum: 3

Seems not very practical to me. Enums are simply not intended to be passed
to functions itself rather than their members using their underlying type.

Maybe someone of the MVPs is able to shed some light on enums.

Regards

Catherine



这篇关于将枚举作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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