Enums that start with a number

Nov 1, 2010 at 12:51 PM

Hi, thanx for a nice control. I have a problem though.

I want the user to be able to choose a width from the list: 1, 2, 3, 4, Max, Standard.

So I make an Enum, WidthEnum, with the above values. But C# does not support Enums that starts with a number. So far I solved it by using _1, _2, _3 and so on, but I don't really like it.

Any suggestions?

Thanx

/Mike

Coordinator
Nov 1, 2010 at 12:59 PM

Unfortunately that is a limiting factor of C#.

But you could use the words… One, Two, Three, Max, Standard

Nov 4, 2010 at 5:47 PM
Edited Nov 4, 2010 at 5:48 PM

Thanx, using One, Two, etc is a good idea. But I would like it best if I could use the numerals.

May I suggest a small revision of the PropertyGrid:

In ComboBoxEditorBase.cs:


                 protected virtual void LoadItems(IEnumerable<object> items)
                 {
                          foreach (var item in items)
                               if (item.ToString()[0] != '_')
                                    this.cbo.Items.Add(item.ToString());
                               else
                                   this.cbo.Items.Add(item.ToString().Substring(1));
                 }

 

void ShowTextBox()
{
[…]
if (currentValue.ToString()[0] != '_')
                txt.Text = currentValue.ToString();
            else
                txt.Text = currentValue.ToString().Substring(1);

 

Or instead of ‘_’ one might use “Invisible_” or something.

I’m not sure if this is a good idea but I thought I should at least ask what you think.

Coordinator
Nov 5, 2010 at 12:18 PM
Edited Nov 5, 2010 at 12:53 PM

TFS is down for maintenance, so here is the code...

Sample Enum (could be anything)... you have to use Numeral_ in the name

public enum NameWithNumeral
{
	Numeral_1,
	Numeral_2,
	Numeral_3,
	Numeral_4,
	Standard,
	Max
}

Sample Object.... (standard class, could be anything)

public class CustomObject : INotifyPropertyChanged
{
	/// <summary>
	/// Gets or sets the CustomEnum
		value.
	/// </summary>
	[Category("Other")]
	public NameWithNumeral CustomEnum
	{
		get { return this._CustomEnum; }
		set
		{
			if (this._CustomEnum != value)
			{
				this._CustomEnum = value;
				this.OnPropertyChanged("CustomEnum");
			}
		}
	} NameWithNumeral _CustomEnum = NameWithNumeral.Numeral_1;


	[Category("Strings")]
	public string String
	{
		get { return _String; }
		set
		{
			if (_String == value) return;
			_String = value;
			OnPropertyChanged("String");
		}
	} string _String;

	#region INotifyPropertyChanged Members

	public event PropertyChangedEventHandler PropertyChanged;

	private void OnPropertyChanged(string propertyName)
	{
		if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");
		PropertyChangedEventHandler handler = PropertyChanged;
		if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
	}

	#endregion
}

The New LoadItems...

protected virtual void LoadItems(IEnumerable<object> items)
{
	showingNumeral = false;

	foreach (var item in items)
	{
		if (item.ToString().StartsWith(NumeralENum.Numeral))
		{
			this.cbo.Items.Add(new NumeralENum() { DisplayValue = item.ToString(), EnumValue = (Enum)item });
			showingNumeral = true;
		}
		else
			this.cbo.Items.Add(item.ToString());
	}
}

The ShowTextBox mod...

if (currentValue.ToString().StartsWith(NumeralENum.Numeral))
	txt.Text = NumeralENum.ConvertNameWithNumeralStringToString(currentValue.ToString());
else
	txt.Text = currentValue.ToString()

CBO Selection Changed mod...

var obj = e.AddedItems[0];
if (obj is NumeralENum)
	currentValue = ((NumeralENum)obj).EnumValue;
else
	currentValue = obj;

And the new NumeralENum object...

 

#region NumeralENum
public struct NumeralENum
{
	public const string Numeral = "Numeral_";


	public static string ConvertNameWithNumeralStringToString(string enumValueString)
	{
		return enumValueString.Replace(Numeral, "");
	}

	public string DisplayValue
	{
		get { return _displayValue; }
		set
		{
			_displayValue = value;
			if (_displayValue.StartsWith(Numeral))
				_displayValue = ConvertNameWithNumeralStringToString(value);
		}
	} string _displayValue;

	public Enum EnumValue
	{
		get { return _enumValue; }
		set
		{
			_enumValue = value;
			if (_enumValue.ToString().StartsWith(Numeral))
				_displayValue = _enumValue.ToString().Replace(Numeral, "");
		}
	} Enum _enumValue;

	public override bool Equals(object obj)
	{
		if (null == obj)
			return false;

		if (obj is NumeralENum)
		{
			var nwn = (NumeralENum)obj;
			return nwn.EnumValue == EnumValue;
		}

		try
		{
			string objStr = obj.ToString();
			if (objStr.StartsWith(Numeral))
				return this.EnumValue.ToString() == objStr;
			return false;
		}
		catch
		{
			return false;
		}
	}

	public override int GetHashCode()
	{
		return EnumValue.GetHashCode();
	}

	public override string ToString()
	{
		return DisplayValue;
	}
}
#endregion
Nov 10, 2010 at 3:07 PM

Cool :) Thanks alot. Sry for the late answer, I've been really busy.

Coordinator
Nov 10, 2010 at 11:06 PM

No problem, glad i could help.