Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 21st, 2008, 12:57 PM
Registered User
 
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default why it doesn't work

I simply copied the following code of the book Beginning Visual C# 2005 by Karli,Nagel , and ... from chapter 22, "classes for input and output" ...but it doesn't compile...

may i know where it went wrong ?




Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

private static List<Dictionary<string, string>> GetData(out List<string> columns)
{
string strLine;
string[] strArray;
char[] charArray = new char[] {','};
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
columns = new List<string>();

try
{
FileStream aFile = new FileStream("../../../SomeData.txt", FileMode.Open);
StreamReader sr = new StreamReader(aFile);

// Obtain the columns from the first line.

// Split row of data into string array
strLine = sr.ReadLine();
strArray = strLine.Split(charArray);

for (int x = 0; x <= strArray.GetUpperBound(0); x++)
{
columns.Add(strArray[x]);
}

strLine = sr.ReadLine();
while (strLine != null)
{
// Split row of data into string array
strArray = strLine.Split(charArray);
Dictionary<string, string> dataRow = new Dictionary<string, string>();

for (int x = 0; x <= strArray.GetUpperBound(0); x++)
{
dataRow.Add(columns[x], strArray[x]);
}

data.Add(dataRow);

strLine = sr.ReadLine();
}

sr.Close();
return data;
}
catch (IOException ex)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return data;
}
}

static void Main(string[] args)
{
List<string> columns;
List<Dictionary<string, string>> myData = GetData(out columns);

foreach (string column in columns)
{
Console.Write("{0,-20}", column);
}
Console.WriteLine();
foreach (Dictionary<string, string> row in myData)
{
foreach (string column in columns)
{
Console.Write("{0,-20}", row[column]);
}
Console.WriteLine();
}
Console.ReadKey();
}
Thank you

 
Old January 21st, 2008, 01:02 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It probably doesn't work because you typed something wrong.

Please try downloading the code for the book in question from the download link above.

If that still doesn't fix it then at least tell us what line the compile fails on and what errors you get from the compiler.

/- Sam Judson : Wrox Technical Editor -/
 
Old January 21st, 2008, 02:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

You've got a bunch of using directives and a couple of methods with no class in site. Wrap the whole thing in a class.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

// Add this
class YourClass {

     ...put your 2 methods here

}

HTH,

Bob






Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 1 Ctrl+F5 don't work, F5 does work? jimboak BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 7 June 29th, 2008 03:46 AM
Ok, this should work but it is not polorboy PHP How-To 0 January 25th, 2007 01:34 PM
Able to Get it work !! rsrika C# 2005 0 March 8th, 2006 09:16 AM
Why this example doesn't work DietCoke ASP.NET 1.0 and 1.1 Basics 3 November 29th, 2004 01:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.