I put together this to parse out a string and then return 3 values in a SQL stored procedure (I have another C# method that formats the 3 values based on the output format you choose, but there is no issue with that code so I did not post it). When my boss saw my code, he said this:
"How do you have a non-static class factory method? You need to create an object to parse a string to create an object to use?
Why move the parse into the class instead of leaving it where it was and just passing back the new class to hold the data?"
I had made a new class, but I can easily move it into the other one. The issue is I don't know what he means by non-static factory method, and also I don't know how to assign Value, Fraction and Direction without creating a new instance of TwpRng like I did: TwpRng result = new TwpRng();
This is my first crack at c# BTW.
public class TwpRng
{
public string Value;
public string Fraction;
public string Direction;
public TwpRng GetValues(string input)
{
TwpRng result = new TwpRng();
result.Value = "";
result.Fraction = "";
result.Direction = "";
Regex pattern_1 = new Regex(@"(?i)^\s*(?<val>\d{1,3})(?<frac>[01235AU])(?<dir>[NEWS])\s*$"); // Example: 0255N
Match match_1 = pattern_1.Match(input);
Regex pattern_2 = new Regex(@"(?i)^\s*(?<val>\d{1,3})(?<dir>[NEWS])\s*$"); // Example: 25N
Match match_2 = pattern_1.Match(input);
Regex pattern_3 = new Regex(@"(?i)^\s*(?<val>\d{1,3})(?<frac>[01235AU])\s*$"); // Example: 25A
Match match_3 = pattern_1.Match(input);
if (match_1.Success)
{
result.Value = match_1.Groups["val"].Value;
result.Fraction = match_1.Groups["frac"].Value;
result.Direction = match_1.Groups["dir"].Value.ToUpper();
}
else if (match_2.Success)
{
result.Value = match_2.Groups["val"].Value;
result.Direction = match_2.Groups["dir"].Value.ToUpper();
}
else if (match_3.Success)
{
result.Value = match_3.Groups["val"].Value;
result.Fraction = match_1.Groups["frac"].Value;
}
else
{
result = null;
}
return result;
}
}