Set the Enum display value C# [duplicate] - c#

This question already has answers here:
WPF Binding a ListBox to an enum, displaying the Description Attribute
(6 answers)
Closed 5 years ago.
I want to get the Enum values into a DropDown list (WPF Binding), but the values should include characters like Spaces and Forward-Slashes which is not allowed in Enum Values, I guess there is a way to get a modified value binding or set an Attribute to each value,
This is my Enum:
public enum CurrencyPair
{
JPY_EUR = 1,
USD_NZD = 2,
EUR_AUD = 3,
}
It's definitely different than the duplicate-marked question, because yes I found the answer there, but my question was in a position that I don't know how to search for it - since I didn't know what Attribute is needed for this purpose...

You'll want to use the Description attribute from System.ComponentModel.DataAnnotations
The answers to this question have the code you need:
Enum ToString with user friendly strings

Related

Naming an enum while changing the type of a field [duplicate]

This question already has answers here:
What does the # symbol before a variable name mean in C#? [duplicate]
(4 answers)
What's the use/meaning of the # character in variable names in C#?
(9 answers)
Closed 3 years ago.
So i got several case, i got my own enum for Boolean values:
public enum BooleanValue
{
None,
Yes = 'Y',
No = 'N'
}
When i changed type of a field in class by renaming (right mouse click-> rename), which was BooleanValue, to bool?, it changed the enum name from BooleanValue to #bool.
So it looks like this now:
public enum #bool
{
None,
Yes = 'Y',
No = 'N'
}
Question is, what does it give ?
Why mechanism decided to do something like this?
I just wanted to change type of a field in class, but in fact it changed the entire enum (and its my fault, i shouldnt rename a type).
But im curious about:
Does # changes anything to that enum ?
Its a naming convention or something ?
I met with variable names starting with #, but i renamed a type of a field, which got type of enum, and it suprised me.
Is any difference between using a # in enum/class names and naming a variable with # ?

Constructing an enum instance [duplicate]

This question already has answers here:
Can you add to an enum type in run-time
(7 answers)
Closed 8 years ago.
I have this enum function with some elements:
public enum TrackingTypeEnum
{
None,
Start,
PageView,
Foreground,
Background,
Push,
}
And I want to add some elements there, but not manually in the function, i want to use "new" command but dosn't work.
I've tried this:
TrackingTypeEnum Custom = new TrackingTypeEnum;
Any solution?
Thanks!
According to official documentation, you can't use enum keywork in such a way. In sort, an enum is a list of constants you can assign values to.
Read: http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
That's not what enums are for- you should only use them to store every possible state of an object.
If you really do want to use enums, you can store those additional values in a dictionary, like it's covered in this SO answer:
C#: can you add to an enum type in run-time

Get property info of all properties which values currently equal 'default' [duplicate]

This question already has answers here:
Programmatic equivalent of default(Type)
(14 answers)
Closed 8 years ago.
So, I need to retrieve all properties of an instance which currently have a value that matches the default value of their respective type. Something along the lines of
GetType().GetProperties().Where(x =>
x.GetValue(this).Equals(default(x.PropertyType)));
This obviously doesn't work because it seems 'x' cannot be resolved anymore at this point. What could I do?
The problem is slightly different. You cannot pass a runtime instance of Type to default. Your problem can be simplified to this:
var type = typeof (string);
var defaultValue = default(type); // doesn't work
That doesn't work. Instead, you want to get the default value at run time, which has been answered by this question.

Significance of text inside square brackets [duplicate]

This question already has answers here:
What does square bracket [] mean in the below code?
(2 answers)
Closed 8 years ago.
Hello, can you please explain me what is the significance of [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("activityid")] in the following code?
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("activityid")]
public Microsoft.Xrm.Sdk.EntityReference ActivityId
{
get
{
return this.GetAttributeValue<Microsoft.Xrm.Sdk.EntityReference>("activityid");
}
set
{
this.OnPropertyChanging("ActivityId");
this.SetAttributeValue("activityid", value);
this.OnPropertyChanged("ActivityId");
}
}
I searched for this thing and I got many posts which gave me answer as the ones in square brackets are Attributes in C#. But, then attributes are related with methods. Over here, ActivityId doesn't seem to be a method. So, how can [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("activityid")] act as an attribute?
Is it related to C# or it has got something to do with CRM?
The confusion comes from your statement about attributes only being valid on methods. Attributes can be valid on items specified in the AttributeTargets enum:
http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx
This then puts you back to the answer being "they are attributes". That attribute has simply been applied to a property.

How to loop through the ChartColorPalette properties and add to list? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I enumerate an enum?
I am using the Microsoft Chart Controls for .NET 3.5 (C#) and have a chart in a winform.
My hope is to allow the user to change the color palette based on their preference.
How do I iterate through the color properties of the ChartColorPalette and add them to a combobox list?
I know it should be something like:
for each(something in ChartColorPalette)
{
combobox.items.add(something.ToString);
}
You can enumerate the names in your enum via the GetNames class method...
foreach(string s in Enum.GetNames(typeof(ChartColorPalette))
{
}
then later if you need the enum for the name you can parse the name value...
var val = (ChartColorPalette)Enum.Parse(typeof(ChartColorPalette),"theValue");
See how to enumerate an enum.

Categories