patterncsharpModerate
Can someone improve this C# code
Viewed 0 times
thiscansomeoneimprovecode
Problem
Its working but I don't think its the best way of doing it. I also changed variable names just to let you know,
I saved all resources in lower case, also if in case cN doesn't got any resource won't it will give an exception.
string [] columnN = AnotherString.Split(','); //Another String is something like this = "HEhehehehehE heeh, aSKdjhkaaksjd, asldkhja slkdlk, asdajsdlka, asdljkasd, asdkasjdasd, asdasdasdl, askdjasd"
AnotherString = "";
int i = 0;
foreach (string cN in columnN)
{
if (!string.IsNullOrEmpty(Res.ResourceManager.GetString(cN.ToLower().Trim())))
AnotherEmptyString += Res.ResourceManager.GetString(cN.ToLower().Trim());
else
AnotherString += cN;
i++;
if (i < columnN.Length)
AnotherString += ",";
}I saved all resources in lower case, also if in case cN doesn't got any resource won't it will give an exception.
Solution
Your code should work ok. However, I would have used a list to store the intermediate results, and then
string.Join to create the result. (Also perhaps Linq to filter the data, but then someone not familiar with Linq will have trouble maintaining the code.) Something likestring[] columnN = AnotherString.Split(',');
string[] stringList = new string[columnN.Length];
for (int i = 0; i < columnN.Length; i++)
{
string cN = columnN[i];
string resource = Res.ResourceManager.GetString(cN.ToLower().Trim()));
stringList[i] = resource ?? cN;
}
AnotherString = string.Join(",", stringList);Code Snippets
string[] columnN = AnotherString.Split(',');
string[] stringList = new string[columnN.Length];
for (int i = 0; i < columnN.Length; i++)
{
string cN = columnN[i];
string resource = Res.ResourceManager.GetString(cN.ToLower().Trim()));
stringList[i] = resource ?? cN;
}
AnotherString = string.Join(",", stringList);Context
StackExchange Code Review Q#17586, answer score: 10
Revisions (0)
No revisions yet.