Hi,

Sir, How can I validate integer value which inputs from console in set method??????
plz help me...

namespace validation
{
    public class role
    {
        private string role_name;
        private int role_id;

        public int Role_id
        {
            get { return role_id; }
            set { role_id = value;}
        }

      
        public string Role_name
        {
            get { return role_name; }
            set { role_name = value; }
        }


    }
}

Recommended Answers

All 3 Replies

Like:

role r = new role();
if(int.TryParse(Console.ReadLine(), out r.Role_id))
{
     Console.WriteLine("Id inserted.");
}

In case if you dont want to show the message, you can do:

role r = new role();
int.TryParse(Console.ReadLine(), out r.Role_id);

but this way you would not know if value was inserted.

but, as I shown in my code how can I use your code in set method ???
I want to validate "role_id" as input comes in variable it must be an integer value.if not it should throw exception....

I see, you would like to show an error (exception) message if the value is not an integer, so you can use Parse method, and try, catch blocks:

int myInt;
try
{
     myInt = int.Parse(Console.ReadLine());
}
catch(Exception ex)
{
    Console.WriteLine(ex.Message);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.