how to do validations when composing object of a class in other class ?
- by haansi
Hi,
I have an IPAddress class which has one property named ip and in its setter I am validating data coming and if data is invalid it throws an error. (Its code is as the following):
private string ip;
public string IP
{
get { return ip; }
set { string PartsOfIP = value.Split('.');
if (PartsOfIP.Length == 4)
{
foreach (string part in PartsOfIP)
{ int a = 0; bool result = int.TryParse(part, out a);
if (result != true)
{
throw new Exception("Invalid IP");
}
else { ip = value; }
}
}
else { throw new Exception("Invalid IP");
}
}
In User Class I want to compose an object of IPAddress class.
I am doing validations for properties of User in User class and validations of Ip in IPAddress class.
My question is how I will compose IPAddress object in UserClass and what will be syntax for this ?
If I again mention get and set here with IPAddress object in User class will my earlier mentioned (in IPAddress class) getter and setter work ?
plz advice me in details thanks