C# Enums with string properties [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Let's say I have enum with some strings in it like this:
enum MyEnum
{
stringA = "String a",
stringB = "String b"
}
Then, I try to access a string property of a string in this enum:
MyEnum.stringA.Length
And it doesn't let me. I can't use any of the string properties with the strings in the enum. Is it possible to access these string properties? or am I doing something wrong.
Thanks in advance.

You can use the DescriptionAttribute from the System.ComponentModel namespace.
enum MyEnum
{
[Description("String a")]
stringA,
[Description("String b")]
stringB
}
And then use this method to get description:
public static string GetDescription(Enum Enumeration)
{
string Value = Enumeration.ToString();
Type EnumType = Enumeration.GetType();
var DescAttribute = (DescriptionAttribute[])EnumType
.GetField(Value)
.GetCustomAttributes(typeof(DescriptionAttribute), false);
return DescAttribute.Length > 0 ? DescAttribute[0].Description : Value;
}
And you can get the value:
var result = GetDescription(MyEnum.stringA).Length;

Related

Using ternary operations to make my code cleaner [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have been doing some C# practice problems for a while and I want to start playing with ternary operations to make my code cleaner.
Here is my code:
public static string Bomb(string txt)
{
txt.ToLower().Contains("bomb") == true ? "Duck!!!" : "There is no bomb, relax.";
}
So basically if Bomb("xxxxxx") contains the string "bomb" it will return "Duck!!!" if not it will return "There is no bomb, relax."
But for some reason, this doesn't work and I can't figure out why.
You just need to add return
public static string Bomb(string txt)
{
return txt.ToLower().Contains("bomb") == true ? "Duck!!!" : "There is no bomb, relax.";
}
Your ternary operator looks OK, but you're missing a return statement from the function. Also note that Contains returns a boolean, so the == true is redundant:
So I have been doing some c# practice problems for a while and I want to start playing with ternary operations to make my code cleaner.
Here is my code:
public static string Bomb(string txt)
{
return txt.ToLower().Contains("bomb") ? "Duck!!!" : "There is no bomb, relax.";
}
you missed the return part in the function
public static string Bomb(string txt)
{
return txt.ToLower().Contains("bomb") ? "Duck!!!" : "There is no bomb, relax.";
}

IComparer does not implement interface member - Error CS0738 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to sort two objects by one of their properties (.Transaction.topLeftX, an integer) using the following code to create a comparer to use in a Sort method:
public class RespComp : IComparer<Kairos.Net.RecognizeImage>
{
public Kairos.Net.RecognizeImage Compare(Kairos.Net.RecognizeImage x, Kairos.Net.RecognizeImage y)
{
if (x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX) <= 0) return x;
else return y;
}
}
However, I get the error message Error CS0738 'RecogniseFacesKairos.RespComp' does not implement interface member 'IComparer.Compare(RecognizeImage, RecognizeImage)'. 'RecogniseFacesKairos.RespComp.Compare(RecognizeImage, RecognizeImage)' cannot implement 'IComparer.Compare(RecognizeImage, RecognizeImage)' because it does not have the matching return type of 'int'.
Does the comparer used in the Sort method need to have return type int?
The IComparer<T> interface is supposed to implement a method that returns an int comparison. -1 for less than, 0 for equal and 1 for greater than.
Look at your code, if you're just comparing the top left, you can probably just do the following:
public int Compare(FooImage x, FooImage y) {
return x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX);
}
The desired outcome of sorting objects by one of their parameters was achieved by the following code:
...
Kairos.Net.KairosClient client = new Kairos.Net.KairosClient();
client.ApplicationID = appId;
client.ApplicationKey = appKey;
Kairos.Net.RecognizeResponse resp = client.Recognize(...);
RespComp SortImages = new RespComp();
resp.Images.Sort(SortImages);
...
public class RespComp : IComparer<Kairos.Net.RecognizeImage>
{
public int Compare(Kairos.Net.RecognizeImage x, Kairos.Net.RecognizeImage y)
{
return x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX);
}
}

using strings inside if statement throw error "Operator '||' cannot be applied to operands string and string" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi my c# is not what it used to be and have just come back after using java script for a while.
Essentially I am just trying to do a simple if statement using index of arrays but I receive the error message.
"Operator '||' cannot be applied to operands string and string"
How come this is not allowed as it essentially becoming a bool.
string[] userCustomAnswerArray = {"It needs to be reaplaced", "This could be improved", "I struggle to see this"};
int customResponseindex = rand.Next(0, 3);
string[] questionResponseArray = { "Yes", "No but not a problem", userCustomAnswerArray[customResponseindex] };
int questionResponseIndex = rand.Next(0, 3);
string userAnswer = questionResponseArray[questionResponseIndex];
if (userAnswer = questionResponseArray[0] || userAnswer = questionResponseArray[1])
{
}
Thanks for your help !!!!
userAnswer = questionResponseArray[0] is incorrect.
= is the assignment operator while == is the equality operator

Unity error CS0029: Cannot implicitly convert type `bool' to `int' but it's bool [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have error
CS0029: Cannot implicitly convert type bool to int.
My declaration:
public bool isBig = false;
If statement with error:
if (player.GetHP() < 6 && player.isBig == false)
I don't understand this. I have also change this bool to return and checked few solutions:
player.GetBig() == false/0 / (player.GetBig()) == false/0 / !(player.GetBig())
but nothing works...
// Edit
public int GetBig()
{ // isBig is bool
return this.isBig;
}
public int GetHP()
{ // HP is int
return this.HP;
}
Simple typo.
public int GetBig()
should be
public bool GetBig()

Convert string to generic type c# (Convert string to T) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a method that needs to convert a string to the generic type:
T GetValue<T>(string name)
{
string item = getstuff(name);
return item converted to T // ????????
}
T could be int or date.
you can use Convert.ChangeType
T GetValue<T>(string name)
{
string item = getstuff(name);
return (T)Convert.ChangeType(item, typeof(T));
}
if you need to limit input types only for int and DateTime, add condition like below
if (typeof(T) != typeof(int) && typeof(T) != typeof(DateTime))
{
// do something with other types
}

Categories