C# : Example 9
Form1.cs
Using C# class system.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Program9_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Pet myPet = new Pet();
getPetData(myPet);
label4.Text = "Name: " + myPet.Name + "
" +
"Type: " + myPet.Type + "
" +
"Age: " + myPet.Age.ToString();
}
private void getPetData(Pet pet)
{
try
{
pet.Name = textBox1.Text;
pet.Type = textBox2.Text;
int age;
if (int.TryParse(textBox3.Text, out age))
{
pet.Age = age;
}
else
{
MessageBox.Show("Invalid age.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Pet.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program9_1
{
class Pet
{
private string _name, _type;
private int _age;
public Pet()
{
_name = "";
_type = "";
_age = 0;
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Type
{
get
{
return _type;
}
set
{
_type = value;
}
}
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
}
}