Reputation: 23
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;
}
}
Upvotes: 0
Views: 541
Reputation: 3563
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?
What he means by that is that currently in order to parse input, you'd need to do this:
var twpRng = new TwpRng(); // Doesn't really make sense to instantiate an empty object
twpRng = twpRng.GetValues(input); // just to create another one.
If you made your factory method GetValues
static:
public static TwpRng GetValues(string input)
You could parse more easily:
var twpRng = TwpRng.GetValues(input);
Upvotes: 1
Reputation: 4146
If you change:
public TwpRng GetValues(string input)
to:
public static TwpRng GetValues(string input)
you have changed from a non-static to a static factory pattern. The calling sequence for the two would be as follows:
TwpRng myRng = new TwpRng();
TwpRng createdRng = myRng.GetValues(input);
as opposed to:
TwpRng createdRng = TwpRng.GetValues(input);
The rest of your code can be the same.
Expanded Explanation
What your boss is asking about is the use of the static modifier. Static methods can be called without instantiating (creating an instance of) the class first, and are often used to parse data and return a fully hydrated instance of the class. Static methods can also be used for utility classes and methods (where an instantiated object may overkill) and you just want to group a bunch of functionality.
Upvotes: 0