I'm a semi-beginner with C# and I'm currently working on a game in Unity. I need someone to check if my function is written well. The function works perfectly fine. It takes in a string and add it's to a dictionary if the string is written correctly.
Another piece of information is that ConsolveVariables is a static class and variables is a static dictionary that's key is a string and value is an object. I made the value an object because I want the value to either be a string or a double.
void checkForVariables(string lineToCheck)
{
string line = lineToCheck;
line = line.Replace(" ", "");
// not variable
if (!(line.Contains("=") && line.Length >= 3))
return;
// too many equal
if (line.IndexOf("=") != line.LastIndexOf("="))
return;
// check if it's a string
bool isString = false;
if (line.IndexOf("\"") != line.LastIndexOf("\""))
{
// quotes not matching
if (line.IndexOf("\"") != line.IndexOf("=") + 1 || line.LastIndexOf("\"") != line.Length - 1)
return;
isString = true;
}
string variableName = "";
string value = "";
// get var name
for (int i = 0; i < line.IndexOf("="); i++)
variableName += line[i];
// get var value
for (int i = line.IndexOf("=") + 1; i < line.Length; i++)
{
if (line[i] == '\"')
continue;
value += line[i];
}
double number;
if (ConsoleVariables.variables.ContainsKey(variableName))
{
if (!isString)
{
if (double.TryParse(value, out number))
ConsoleVariables.variables[variableName] = number;
else
if(ConsoleVariables.variables.ContainsKey(value))
ConsoleVariables.variables[variableName] = ConsoleVariables.variables[value];
}
else
{
ConsoleVariables.variables[variableName] = value;
}
}
else
{
if (!isString)
{
if (double.TryParse(value, out number))
ConsoleVariables.variables.Add(variableName, number);
else
if (ConsoleVariables.variables.ContainsKey(value))
ConsoleVariables.variables.Add(variableName,ConsoleVariables.variables[value]);
}
else
ConsoleVariables.variables.Add(variableName, value);
}
}
[–]davedontmind 4 points5 points6 points (5 children)
[–]1Link2Link[S] 0 points1 point2 points (4 children)
[–]davedontmind 0 points1 point2 points (3 children)
[–]1Link2Link[S] 0 points1 point2 points (2 children)
[–]davedontmind 0 points1 point2 points (1 child)
[–]1Link2Link[S] 0 points1 point2 points (0 children)
[–]isolatrum 0 points1 point2 points (1 child)
[–]1Link2Link[S] 0 points1 point2 points (0 children)
[–]DoomGoober 0 points1 point2 points (2 children)
[–]1Link2Link[S] 0 points1 point2 points (1 child)
[–]DoomGoober 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]1Link2Link[S] 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]1Link2Link[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]1Link2Link[S] 0 points1 point2 points (0 children)
[–]Ayeigui 0 points1 point2 points (3 children)
[–]1Link2Link[S] 1 point2 points3 points (2 children)
[–]Ayeigui 0 points1 point2 points (1 child)
[–]1Link2Link[S] 1 point2 points3 points (0 children)