Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've created a list with a lot of options, let's say for every character a different color.
string A = "#FFB97FC9";
string B = "#FF9BCC50";
// etc.
string answer1 = Options.Answer1;
answerRectangle.Fill = GetColorFromHexa(answer1);
now let's say that Options.Answer1 = A.
I want answerRectangle to have the color from the code #FFB97FC9
How can I achieve this?
NOTE: I want to have answer1 = #FFB97FC9
Use a dictionary instead of variables:
var colors = new Dictionary<string, string>();
colors["A"] = "#FFB97FC9";
colors["B"] = "#FF9BCC50";
// etc.
string answer1 = Options.Answer1;
answerRectangle.Fill = GetColorFromHexa(colors[answer1]);
Why not create a dictionary with options:
Dcitionary<string, string> colors = new Dictionary<string, string>();
colors.Add("A", "#FFB97FC9");
colors.Add("B", "#FF9BCC50");
and then:
answerRectangle.Fill = GetColorFromHexa(colors[answer1]);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a string array as follows:
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
In essence, it is broken down by id|active where for example id is say 1122 and active is true or false.
Say my id was 1123, how would I search this array to get the value of true in this case? I understand Substring needs to be used with IndexOf but not sure how to tie it together.
Little bit of LINQ and String.Split should do the trick.
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
int id = 1123;
var itemWithGivenId = stringArray
.SingleOrDefault(s => int.Parse(s.Split('|')[0]) == id);
Console.WriteLine(bool.Parse(itemWithGivenId.Split('|')[1]));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is it possible to look up if a string in contained in an Array, with a switch case?
string text = "blalbac";
string arr[] = {"a","b","c"};
switch (text)
{
case arr.Contains(filename):
//do..
break;
}
I’m not sure what you’re trying to do. Do you want something like
foreach(string item in arr)
{
if(text.Contains(item))
{
...
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am working on a window app in which a json string like
{
"0":{"UID":"3182713a-60de-4576-ad82-6804d2acf2b7","Rating":-1},
"1":{"UID":"eb41478e-e9bc-428e-8203-482f6a329666","Rating":-13},
"2":{"UID":"a0922817-e706-4478-8995-d258bada488d","Rating":6.0033992049},
"3":{"UID":"e0716752-56f4-49af-a811-ae55c69baa3d","Rating":-4.9836395671},
"4":{"UID":"ec6bf376-17b5-4a9e-92be-990557551cd0","Rating":-7.754526396}
}
I want to parse and sort it by rating. Please suggest how i can achieve it.
You will need to deseralise the JSON into a Dictionary<int, YourClass>
Example:
var json = #"{
""0"":{""UID"":""3182713a-60de-4576-ad82-6804d2acf2b7"",""Rating"":-1},
""1"":{""UID"":""eb41478e-e9bc-428e-8203-482f6a329666"",""Rating"":-13},
""2"":{""UID"":""a0922817-e706-4478-8995-d258bada488d"",""Rating"":6.0033992049},
""3"":{""UID"":""e0716752-56f4-49af-a811-ae55c69baa3d"",""Rating"":-4.9836395671},
""4"":{""UID"":""ec6bf376-17b5-4a9e-92be-990557551cd0"",""Rating"":-7.754526396}
}";
var result = JsonConvert.DeserializeObject<Dictionary<int,MyCustomClass>>(json);
MyCustomClass
public class MyCustomClass
{
public Guid UID {get;set;}
public decimal Rating {get;set;}
}
In the Dictionary<int, MyCustomClass> the int is the numeric key in your Json.
Note: I am using Newtonsoft Json here.
Once you have done the above you can sort it like the following using LINQ:
var sorted = result.OrderBy(x => x.Value.Rating);
// OR
var sorted = result.OrderByDescending(x => x.Value.Rating);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Title is like tl;dr version, here is what I mean:
I made currently just a string (to be a file with text), and I am splitting this string into separate words. I would like to make a method that allows me to mark words based on string/file. For example:
string nameOfString = "John likes pancakes";
Categorize(string nameOfString, class nameOfCategory)
this method would make John, likes and pancakes into a category (like Stupid, bestTexts) I passed to nameOfCategory.
I would like to count then the words into all of the categorys, so probably should use some kind of array top do this. Can someone help me with this? The big problem is I really have no idea how to pass the category (as a seperate class or just a string, maybe string[]?) and still be able to count it.
static void Main(string[] args)
{
var inputList = new List<string>
{
"John likes pancakes",
"John hates watching TV",
"I like my TV",
};
var dic = new Dictionary<string, int>();
inputList.ForEach(str => AddToDictionary(dic, str));
foreach (var entry in dic)
Console.WriteLine(entry.Key + ": " + entry.Value);
}
static void AddToDictionary(Dictionary<string, int> dictionary, string input)
{
input.Split(' ').ToList().ForEach(n =>
{
if (dictionary.ContainsKey(n))
dictionary[n]++;
else
dictionary.Add(n, 1);
});
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wanted to try some examples in C# using collections and generics but I am stuck at this example.
This is my code in c#,
public List<string> CurrentCount(int week, int year)
{
List<string> lst = new List<string>();
lst.Add("current:");
lst.Add("10");
lst.Add("target:");
lst.Add("15");
return lst;
}
It gives me result like this :
["current:","10","target:","15"]
But I want it like this :
["current": 10,"target": 15] or
["current": "10","target": "15"]
Any ideas will be helpful.
Thanks.
You want a Dictionary, not a List.
var dict = new Dictionary<string, int>();
dict.Add("current", 10);
dict.Add("target", 15);