如何在声明为枚举类型的字符串中给出空格 [英] How to give space in a string which is declared as enum type

查看:126
本文介绍了如何在声明为枚举类型的字符串中给出空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在enum 
中声明了一个字符串eg.mydata,我传递了一个参数,并且能够获取枚举中的字符串。
我想为eg.my数据提供一个空间。那个值,我需要在.aspx文件上显示< b > < / b >
请做好

解决方案

你不能在枚举值名称中使用空格:它们需要遵循C#变量的规则,这明确排除了变量名中空格的使用。



但是有多种方法可以将人类可读标签(可以包含任何你想要的字符)应用于枚举项 - 这里有一个:用于枚举元素的人类可读字符串 [ ^ ]



你究竟如何在你的应用程序中使用类似的东西我不能说 - 你没有提供足够的信息来提供数据源,显示项目等等。

有许多聪明的方法可以基本上拉伸枚举,这样它们就会看起来有一个字符串,其中包含与每个枚举值相关的空格。



除了OriginalGriff在他的回复中链接到的优秀文章之外,Sacha Barber还有一个有趣的技术,在这个CodeProject文章中描述:[ ^ ]。你可以在这里阅读许多其他技术:[ ^ ],[ ^ ],[ ^ ]。



我想在这里玩魔鬼的拥护者并试着说服你使用任何这些技术;这就是原因:



1.为什么要使用精心设计的技术来拉伸Enum Type的功能,当你想要做的事情很简单时,还有另外一种方法这样做是什么?



2.所有这些技术都需要一些或全部:使用Reflection,使用ComponentModel,RunTime.Serialization和其他.NET库。它们使你的应用程序变得更大,更复杂,同时,imho,增加了一些价值。



3.在每种情况下我都看到程序员想让Enum使用空格(或点)的值,只使用(或可以使用)最基本的Enum特征:例如,使用这些值可能会排除使用Enum的高级功能,如'Flags和逐位操作。 br />


我建议您可以用这种相对简单的方式实现相同的功能:

  public   struct  sEnum 
{
public const string v1 = this是一个值;
public const string v2 = 这是值二;
public const string v3 = 这是值三;
public const string v4 = 这是值四;
}

一旦定义了这样的结构,就可以在代码中立即使用它:

 Console.WriteLine(sEnum.v1); 

请记住,此处的sEnum在应用程序的全局NameSpace范围内定义为类,因此,它可以在您的应用程序中的任何其他类,表单或其他任何内容中使用,正如在同一个NameSpace范围内定义的枚举在全球范围内可用。


I declared one string eg.mydata in enum
and i passed one parameter and able to getting that string which is in enum.
I want to give a  space for eg.my data.and that value,I need to show on .aspx file<b></b>
please do needful

解决方案

You can't use spaces in enum value names: they need to follow the rules for C# variables, which explicitly precludes the use of spaces in variable names.

However there are various ways to apply a "human readable" tag (what can imnclude any characters you want) to an enum item - here is one: Human readable strings for enum elements[^]

Exactly how you would use something like that in your application I can't say - you do not give anywhere near enough information as to the data source, display items, etc. to allow that.


There are a number of clever methods to essentially "stretch" Enums so they can appear to have a string with spaces associated with each Enum Value.

In addition to OriginalGriff's excellent article linked-to in his response here, Sacha Barber has an interesting technique described in this CodeProject article: [^]. You can read about many other techniques here: [^], [^], [^].

I'd like to play "devil's advocate" here and try and convince you not to use any of these techniques; here's why:

1. why use elaborate techniques to "stretch" the functionality of the Enum Type, when what you are trying to do is simple, and there's another way of doing it ?

2. all these techniques require some, or all, of: use of Reflection, use of the ComponentModel, RunTime.Serialization, and other .NET libraries. They make your app bigger, and more complex, while, imho, adding little value.

3. in every case I've seen where a programmer wanted to have Enum values with spaces (or dots), only the most basic of Enum features were used (or could be used): use of such values, for example, might exclude using advanced features of Enum like 'Flags, and bit-wise operations.

I propose you can achieve the same functionality in this relatively simple way:

public struct sEnum
{
    public const string v1 = "this is value one";
    public const string v2 = "this is value two";
    public const string v3 = "this is value three";
    public const string v4 = "this is value four";
}

Once you've defined a struct like this, you can use it immediately in your code like this:

Console.WriteLine(sEnum.v1);

Keep in mind that 'sEnum here is defined, like a Class, in the global NameSpace scope of the application: so, it's available for use in any other Class, or Form, or whatever, in your application, just as an Enum defined in the same NameSpace scope is available globally.


这篇关于如何在声明为枚举类型的字符串中给出空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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