How do you apply inheritnce in C# what is the method and procedure?
Inheritance in C sharp?
// Thestudent class
using System;
using System.Collections.Generic;
using System.Text;
namespace uribanabreak
{
// this class can compare itself
class Student: IComparable %26lt;Student%26gt;
{
// atribute are private
private int aid;
private string aname;
// create the set get for access to the atribute
public int id
{
get
{
return aid;
}
set
{
aid = value;
}
}
public string name
{
get
{
return aname;
}
set
{
aname = value;
}
}
public Student() { } //Declare of method
// next the body of method
public Student(int id, string name)
{
// set the values of id and name when
// the class is insted
this.id = id;
this.name = name;
}
// now implement the icomparable methos for
// now if the object are the same
#region IComparable%26lt;Student%26gt; Members
public int CompareTo(Student other)
{
// change id for other atribute if you lie
return other.id.CompareTo(this.id);
}
#endregion
}
}
// and the form1 class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace uribanabreak
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// the first is create a instance of the student class
List%26lt;Student%26gt; est = new List%26lt;Student%26gt;();
private void Form1_Load(object sender, EventArgs e)
{
// clear the listbox
l1.Items.Clear();
l2.Items.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
// here add element for the first list
if ((t1.Text != "") %26amp;%26amp; (t2.Text != ""))
{
l1.Items.Add((object)(t1.Text + " - " + t2.Text));
// now add the data form the list of student
est.Add(new Student(System.Convert.ToInt32(t1.Text) ,
t2.Text.ToString()));
}
else
{
System.Windows.Forms.MessageBox.Show("Es... ambos valores");
}
}
int numeros(char p)
{
int es = 0;
if (p.ToString().Equals("0"))
{
es = 1;
}
else if (p.ToString().Equals("1"))
{
es = 1;
}
else if (p.ToString().Equals("2"))
{
es = 1;
}
else if (p.ToString().Equals("3"))
{
es = 1;
}
else if (p.ToString().Equals("4"))
{
es = 1;
}
else if (p.ToString().Equals("5"))
{
es = 1;
}
else if (p.ToString().Equals("6"))
{
es = 1;
}
else if (p.ToString().Equals("7"))
{
es = 1;
}
else if (p.ToString().Equals("8"))
{
es = 1;
}
else if (p.ToString().Equals("9"))
{
es = 1;
}
return es;
}
private void t1_KeyUp(object sender, KeyEventArgs e)
{
char[] a;
int i;
a = t1.Text.ToCharArray();
t1.Text = "";
for (i = 0; i %26lt; a.Length; i++)
{
if (numeros(a[i]) == 1)
{
a[i] = Char.ToUpper(a[i]);
t1.Text = t1.Text + a[i].ToString();
t1.SelectionStart = t1.Text.Length;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
// here sort the list
est.Sort();
// and show into the list2
foreach (Student es in est)
{
l2.Items.Add((object)es.id.ToString() + " - " + es.name);
}
}
}
}
Reply:public class Class1
{
}
public class Class2 : Class1
{
}
Reply:Here's a couple of examples:
http://www.aspfree.com/c/a/C-Sharp/Inher...
http://msdn2.microsoft.com/en-us/library...
http://www.csharp-station.com/Tutorials/...
http://www.eli.sdsu.edu/courses/spring03...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment