how to do validations when composing object of a class in other class ?

Posted by haansi on Stack Overflow See other posts from Stack Overflow or by haansi
Published on 2010-04-19T06:40:48Z Indexed on 2010/04/19 6:43 UTC
Read the original article Hit count: 205

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#2.0