I am trying to write code for a class that has a couple of attributes that will can only be known upon creation. Because I think accessor functions (e.g. getSomething()) are outdate, I declared my attributes as "public readonly" in order to protect the attributes from getting modified yet still accessible by the parent class. I am not convinced that this is the best practice for this. Implementing properties seem like a bit of a waste of time to me. (refer to this article to see what I mean)
Another issue of mine is how trapping exceptions during class creation. I know having a blank catch statement is bad, but I do not want my class to thrown an exception if something went wrong. I have no problems with this because the default values of the properties will already tell me that the class was not properly created, but again I do not think this is the best practice.
To explain further, please see my code below:
Help!
Another issue of mine is how trapping exceptions during class creation. I know having a blank catch statement is bad, but I do not want my class to thrown an exception if something went wrong. I have no problems with this because the default values of the properties will already tell me that the class was not properly created, but again I do not think this is the best practice.
To explain further, please see my code below:
Code:
class MessageObject
{
public readonly int Length = 0;
public readonly int Sequence = 0;
public readonly bool Duplicate = false;
public readonly string Data = "";
public readonly string Message = "";
public MessageObject(string message)
{
try
{
Message = message;
Length = int.Parse(message.Substring(0, 3));
Sequence = int.Parse(message.Substring(3, 6));
Duplicate = message.Substring(9,1).ToLower() == "y" ? true : false;
Data = message.Substring(10, Length);
}
catch
{
// Do something
}
}
Comment