Add a Property
Now let's give our cyborg a name:
public class Cyborg : System.Object
{
public virtual void IntroduceYourself()
{
MessageBox.Show("Hi, my name is " + Name + ".");
}
public String Name;
}
The following exercises our new functionality:
Cyborg T1 = new Cyborg();
T1.Name = "Number 1";
T1.IntroduceYourself();
Add a Method
Now let's give our cyborg the ability to speak:
public class Cyborg
{
public virtual void IntroduceYourself
{
MessageBox.Show("Hi, I do not have a name yet.");
}
public virtual void Speak(string pMsg)
{
MessageBox.Show(pMsg);
}
}
Create object from class:
Cyborg T1 = new Cyborg();
T1.Speak("Humans are imperfect.");
Complete Listing
Here is the complete code listing (a form with a button on it). I renamed the namespace to Skynet, seemed appropriate.
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 Skynet
{
public class Cyborg : System.Object
{
//Properties:
public String Name;
//Methods:
public virtual void IntroduceYourself()
{
MessageBox.Show("Hi, my name is " + Name + ".");
}
public virtual void Speak(string pMsg)
{
MessageBox.Show(pMsg);
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Cyborg T1 = new Cyborg();
T1.Name = "Number 1";
T1.IntroduceYourself();
T1.Speak("Humans are imperfect.");
}
}
}