I have a class that represents an rtf document ClsRtfDoc
.
Instantiating an ClsRtfDoc
object reads an rtf file and it first parses the rtf file to obtain various rtf file metadata.
For example, I have a field int paperHeight
that I use to store the rtf file paper height parsed from the string that holds the rtf file contents.
Since the rtf file content is in a string variable, the PaperHeight
property data type is a string and the PaperHeight
property setter
parses the paper height from the string blob (e.g., look for \paperw12240\paperh15840
and grab 15840 from the blob), converts it to an integer, and sets the paperHeight
field to the integer value. See code below.
I constructed the code this way because I want to isolate the parse + set operation. Am I abusing the concept of a property settor by having the property datatype not match the data type of the corresponding field? Is there a better practice (design pattern?) I should be using in constructing this code?
CODE
public partial class ClsRtfDoc
{
int paperHeight;
public string PaperHeight
{
set
{
MatchCollection objPaperHeight = Regex.Matches(value, "(\\\\paperw\\d+)(\\\\paperh\\d*)");
if (objPaperHeight.Count >= 1 && objPaperHeight[0].Groups.Count == 3)
{
if (!(Int32.TryParse((objPaperHeight[0].Groups[2].Value).Replace("\\paperh", ""), out int paperHeightValue)))
{
throw new FormatException("Can't find paper height");
}
else
{
paperHeight = paperHeightValue;
}
}
else
{
throw new FormatException("Can't find paper height");
}
}
}
// CTOR
public ClsRtfDoc(string fqFRtfFileName)
{
string rtfTextFromFile = GetRtfFromFile(fqFRtfFileName);
PaperHeight = rtfTextFromFile;
}
}
Aucun commentaire:
Enregistrer un commentaire