Albert

Albert

  • NA
  • 10
  • 0

How to use labels in windows aplications to show class data

Jan 21 2006 9:39 PM
Hi, I have created the following program first as a console application. Then I created a windows application and copied the class data there. It is shown below. In the console application, I have used the printData() method to output the data of an instance of a Person or Athlete class. I want to do the same, but now that output should be in the label.

Any help would be appreciated.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace windowsPracticingWithClasses
{
///
/// Summary description for Form1.
///
public class Person : System.Windows.Forms.Form
{
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;

//My instance variables
private string firstName, lastName;
private int age;
private double height, weight;
private string bankName;
private double bankAccount;
private double balance = 0;
private string squareColor;

# region Properties
//Properties
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public double Height1
{
get { return height; }
set { height = value; }
}
public double Weight
{
get { return weight; }
set { weight = value; }
}
public string BankName
{
get { return bankName; }
set { bankName = value; }
}
public double BankAccount
{
get { return bankAccount; }
set { bankAccount = value; }
}
public double Balance
{
get { return balance ; }
set { balance = value; }
}
public string SquareColor
{
get { return squareColor ; }
set { squareColor = value; }
}
# endregion
//Custom constructor
public Person(int age, double height, double weight, string bankName, double bankAccount, double balance)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.age = age;
this.height = height;
this.weight = weight;
this.bankName = bankName;
this.bankAccount = bankAccount;
this.balance = balance;
}
//Default constructor
public Person()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

}
#region My Methods
//My Methods
public void makeDeposit(int amount)
{
balance+= amount;
}
public void makeWithdrawal(int amount)
{
balance-= amount;
}
public virtual void printData()
{
Console.WriteLine("\nProfile: \n\nAge: " + this.age);
Console.WriteLine("Height: " + this.height);
Console.WriteLine("Weight: " + this.weight);
Console.WriteLine("Bank Name: " + this.bankName);
Console.WriteLine("Bank Account: " + this.bankAccount);
Console.WriteLine("Balance: " + this.balance);
}
#endregion
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
//
// Person
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Person";
this.Text = "Form1";
}
#endregion


}
class Athlete : Person
{

public string sport;

public Athlete(string sport) : base(23,5.6,120,"Chase",5671234,1000)
{
this.sport = sport;
}
public override void printData()
{
base.printData();
Console.WriteLine("Sport Practicing: " + this.sport);
}
};
class TestClasses
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{

Application.Run(new Person());
Person[] person = new Person[5];

person[0] = new Person(22,5.6,120,"Chase",5671234,2000);
person[0].printData();

Person[] athlete = new Athlete[3];

athlete[0] = new Athlete("Figure Skating");
athlete[0].FirstName = "Sarah";
athlete[0].LastName = "Hughes";
athlete[0].Age = 20;
athlete[0].Height1 = 5.5;
athlete[0].Weight = 120;
athlete[0].BankName = "Chase";
athlete[0].BankAccount = 434324;
athlete[0].Balance = 200;

athlete[1] = new Athlete("Martial Arts");
athlete[1].FirstName = "Kim";
athlete[1].LastName = "Chung";
athlete[1].Age = 21;
athlete[1].Height1 = 5.7;
athlete[1].Weight = 130;
athlete[1].BankName = "Citi";
athlete[1].BankAccount = 212312;
athlete[1].Balance = 300;

athlete[2] = new Athlete("Soccer");
athlete[2].FirstName = "Joe";
athlete[2].LastName = "Tribiani";
athlete[2].Age = 22;
athlete[2].Height1 = 5.6;
athlete[2].Weight = 140;
athlete[2].BankName = "Apple";
athlete[2].BankAccount = 123124;
athlete[2].Balance = 400;

for(int i = 0; i < athlete.Length ; i++)
{
athlete[i].printData();
}

Console.ReadLine();
}
}
}

Answers (2)