Define a class and display its value (C#) : Class Definition « Language Basics « ASP.Net






Define a class and display its value (C#)

<%@ Page Language="C#" %>

<script runat="server">

  public class Person
  {
    private string  _Name;
    private int _Age;
    private string  _EyeColor;

    public Person() {
    }

    public string  Name {
      get {
        return _Name;
      }
      set {
        _Name = value;
      }
    }

    public int Age {
      get {
        return _Age;
      }
      set {
        _Age = value;
      }
    }

    public string EyeColor {
      get {
        return _EyeColor;
      }
      set {
        _EyeColor = value;
      }
    }
  }


  void Page_Load(object Sender, EventArgs E) {

    Person p = new Person();
    p.Name = "Joe";
    p.Age = 25;
    p.EyeColor = "Blue";

    Name.Text = p.Name;
    Age.Text = p.Age.ToString();
    EyeColor.Text = p.EyeColor;
  }
</script>

<html>
<head>
</head>
<body>
    <form runat="server">
      Name: <asp:Label runat="server" id="Name" /><br />
      Age:  <asp:Label runat="server" id="Age" /><br />
      Eye Color: <asp:Label runat="server" id="EyeColor" />
    </form>
</body>
</html>

           
       








Related examples in the same category

1.Define and use class: property (C#)
2.Constructor with parameter (C#)
3.Define two classes (C#)