1 / 20

Data

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

marv
Download Presentation

Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Creating a Class, representing data, reading from files.

  2. 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

  3. But First • Yesterday we looked at... • User interaction • Handling events • Init, OnLoad, PreRender Lecture 4: Data

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Complete the Class • We want to model such a question • What properties should the class Question have? Lecture 4: Data

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. Questions • What does int.Parse(fld[2]) do? • What does world.Add(c.name, c) do? Lecture 4: Data

  16. DropDownList • A drop down list includes each country. • Clicking on “Show Details” brings up information on the selected country. Lecture 4: Data

  17. 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

  18. 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

  19. 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

  20. 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

More Related