Define two classes (C#) : Class Definition « Language Basics « ASP.Net

ASP.Net
1. ADO.net Database
2. Ajax
3. Asp Control
4. Collections
5. Components
6. Data Binding
7. Development
8. File Directory
9. HTML Control
10. Language Basics
11. Login Security
12. Mobile Control
13. Network
14. Page
15. Request
16. Response
17. Server
18. Session Cookie
19. Sitemap
20. Theme Style
21. User Control and Master Page
22. Validation by Control
23. Validation by Function
24. WebPart
25. WPF
26. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.Net » Language Basics » Class Definition 
Define two classes (C#)

<%@ page language="C#" runat="server" Debug="true"%>

<script runat="server">


  public class Key
  {
    private int shape;
    public Key(int newshape)
    {
      shape = newshape;
    }

    public int Shape
    {
      get
      {
        return shape;
      }
    }
  }
    
    public class Car
  {
    private string color;
    private int gear;
    private int ignition;
    private bool engineRunning;
    private static int count = 0;
    public Car(int IgnitionShape)
    {  
      color = "Cold gray steel";
      ignition = IgnitionShape;
      count += 1;
    }

    public static int Count
    {
      get
      {
        return count;
      }

    }

    public string Color
    {
      get
      {
        return color;
      }
      set
      {
        color = value;
      }
    }

    public int Gear
    {
      get
      {
        return gear;
      }
    }

    public string IsRunning  
    {
      get
      {

        if (engineRunning)
        {
          return "The engine is running.";
        }
        else
        {
          return "The engine is not running.";
        }
      }
    }
    public void ChangeGear(int direction)
    {
      if (direction < 0gear -= 1;
      if (direction > 0gear += 1;
      if (gear > 5gear = 5;
      if (gear < -1gear = -1;
    }
    
    public void ChangeGear(string direction)
    {
      
      if (direction == "down")
      {
        ChangeGear(-1);
      }
      if (direction == "up")
      {
        ChangeGear(+1);
      }
      
    }
    public void Ignition(Key IgnitionKey)
    {
      if (IgnitionKey.Shape == ignitionengineRunning = true;
    }
    public void EngineOff()
    {
      engineRunning = false;
    }

  }
    
    public void Page_Load()
  {
    Key AlexKey = new Key(0987654321);
    Key RobKey = new Key(1861005040);
    Key MyKey = new Key(1234567890);

    Car MyCar = new Car(1234567890);
    Response.Write("<b>New object 'MyCar' created.</b>");


    Response.Write("<br/>Color: " + MyCar.Color);
    Response.Write("<br/>Gear: " + MyCar.Gear);

    MyCar.Color = "Black";
    MyCar.ChangeGear(+1);
    Response.Write("<br/><b>Properties updated.</b>");

    Response.Write("<br/>New color: " + MyCar.Color);
    Response.Write("<br/>New gear: " + MyCar.Gear);

    MyCar.ChangeGear("up");
    Response.Write("<br/><b>Shifted 'up' one gear.</b>");

    Response.Write("<br/>New gear: " + MyCar.Gear);

    Response.Write("<hr/>Attempting to start MyCar with AlexKey: ");
    MyCar.Ignition(AlexKey);
    Response.Write(MyCar.IsRunning);

    Response.Write("<hr/>Attempting to start MyCar with RobKey: ");
    MyCar.Ignition(RobKey);
    Response.Write(MyCar.IsRunning);

    Response.Write("<hr/>Attempting to start MyCar with MyKey: ");
    MyCar.Ignition(MyKey);
    Response.Write(MyCar.IsRunning);
    Response.Write("<br/>Attempting to stop MyCar: ");
    MyCar.EngineOff();
    Response.Write(MyCar.IsRunning);

  }
</script>

           
       
Related examples in the same category
1. Define and use class: property (C#)
2. Define a class and display its value (C#)
3. Constructor with parameter (C#)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.