200 likes | 371 Views
Data. Creating a Class, representing data, reading from files. Data. We will look at how we can represent data. Creating simple classes. Creating and using a SortedList . Reading Data from a Tab Delimited File. But First. Yesterday we looked at... User interaction Handling events
E N D
Data Creating a Class, representing data, reading from files.
Data • We will look at how we can represent data. • Creating simple classes. • Creating and using a SortedList. • Reading Data from a Tab Delimited File. Lecture 4: Data
But First • Yesterday we looked at... • User interaction • Handling events • Init, OnLoad, PreRender Lecture 4: Data
Class • A class may be used to model something in the real world. • We might represent a country by the five values: • Name • Region • Area (in sq km) • Population • GDP (Gross Domestic Product) Lecture 4: Data
Country Class public class Country { String name; String region; int area; long population; long gdp; public Country(String name, String region, int area, long population, long gdp) { this.name = name; this.region = region; this.area = area; this.population = population; this.gdp = gdp; } } Lecture 4: Data
Displaying Data • We might write a routine to display the class: public Table ShowCountry() { Table t = new Table(); TableRowtrName = new TableRow(); TableCell tdName1 = new TableCell(); tdName1.Text = "Name"; TableCell tdName2 = new TableCell(); tdName2.Text = this.name; trName.Cells.Add(tdName1); trName.Cells.Add(tdName2); t.Rows.Add(trName); TableRowtrRegion = new TableRow(); ... } Lecture 4: Data
Show The Country Details • The Country Class will now create a table. • We can display the table by adding it: Controls.Add(c.ShowDetails()); Lecture 4: Data
Questions • Consider the multiple choice question shown... How do you make the cell td span two columns. • td.RowSpan = 2; • td.ColumnSpan = 2; • td.ColumnSpan(2); • td.Column.Span = 2; • ColumnSpan(td,2); Lecture 4: Data
Complete the Class • We want to model such a question • What properties should the class Question have? Lecture 4: Data
SortedList • To deal with more than one country you can use the SortedList collection. • You need a using statement: using System.Collections.Generic; • To create a sorted list you specify the type of the key and the list items: SortedList<String, Country> world = new SortedList<String, Country>(); Lecture 4: Data
SortedList • To add to a SortedList you specify the key and the value: world.Add(china.name, china); world.Add("France", france); • To retrieve an item you use [] • Country x = world["China"]; Lecture 4: Data
TDF • A tab delimited file has one record of each line • Each field is separated by a tab character '\t' Afghanistan Asia 652090 28150000 9596000000 Albania Europe 28748 3170000 10768000000 Algeria Africa 2381741 34895000 134275000000 • You can read one line from a StreamReadersr using sr.ReadLine() • You can split a String s using s.Split('\t') Lecture 4: Data
Reading TDF • The file world.txt includes data in tab delimited format. StreamReadersr = new StreamReader(Server.MapPath("World.txt"); while (!sr.EndOfStream) { String[] fld = sr.ReadLine().Split('\t'); Country c = new Country(fld[0],fld[1], int.Parse(fld[2]),long.Parse(fld[3]),long.Parse(fld[4])); world.Add(c.name, c); } Lecture 4: Data
Questions • What kind of data is in fld[0]? • What kind of data is in fld[2]? • What data type does sr.EndOfFile return? Lecture 4: Data
Questions • What does int.Parse(fld[2]) do? • What does world.Add(c.name, c) do? Lecture 4: Data
DropDownList • A drop down list includes each country. • Clicking on “Show Details” brings up information on the selected country. Lecture 4: Data
Filling the Drop List • You can iterate over every country and add to the list: foreach (Country c in world) CList.Items.Add(c.name); Lecture 4: Data
Show Selected • You can access the selected item from CList: protected void Show_Click(object sender, EventArgs e) { Controls.Add(world[CList.Text].ShowCountry()); } Lecture 4: Data
Tomorrow • We will review the work covered so far. • If you have questions from the work you have seen you should prepare them. Lecture 4: Data
Summary • You can create a class to model a “real world” item • You can create a collection of objects using an Array or a more complicate collection like a SortedList • You can read simple data from a tab delimited file Lecture 4: Data