I am working on an app that will interact with assembly (app) versions. I created a struct for that, along with an exception for parsing error.
Here's the code:
[Serializable]
public class VersionParseException : Exception
{
public VersionParseException()
{ }
public VersionParseException(string message)
: base(message)
{ }
public VersionParseException(string message, Exception innerException)
: base(message, innerException)
{ }
protected VersionParseException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
}
public struct AppVersion
{
int _majorVersion;
public int MajorVersion
{
get { return _majorVersion; }
set
{
_majorVersion = value;
}
}
int _minorVersion;
public int MinorVersion
{
get { return _minorVersion; }
set
{
_minorVersion = value;
}
}
int _buildNumber;
public int BuildNumber
{
get { return _buildNumber; }
set
{
_buildNumber = value;
}
}
int _revision;
public int Revision
{
get { return _revision; }
set
{
_revision = value;
}
}
public string Version
{
get { return MajorVersion.ToString() + "." + MinorVersion.ToString() + "." + BuildNumber.ToString() + "." + Revision.ToString(); }
set
{
try
{
MajorVersion = int.Parse(value.Split('.')[0]);
MinorVersion = int.Parse(value.Split('.')[1]);
BuildNumber = int.Parse(value.Split('.')[2]);
Revision = int.Parse(value.Split('.')[3]);
}
catch (Exception exception)
{
throw new VersionParseException("Invalid App Version", exception);
}
}
}
}
Is there any better way to handle AppVersions and exceptions occurring in the process of using it?
And here's an example of using the struct:
AppVersion version1;
version1.Version = "0.500.965.201";
Console.WriteLine(version1.MinorVersion); // outputs "500"
version1.BuildNumber = 753;
Console.WriteLine(version1.Version); // outputs "0.500.753.201"
Another example on throwing the exception:
AppVersion version2;
version2.Version = "0.blabla.987.test"; //Throws VersionParseException