Skip to main content
gamedev.net
🔒 Locked

[.net] debug...problem

Started by nandinicbit Feb 9, 2009 at 11:15 PM 1 replies 1.2k views
Original Post
nandinicbit
nandinicbit
Please go through the program below.... In this prog i tried to extract data from a text file and then copy it into subscriber instances problem with this code is once the webmethod is called in Main, the control is not goin to the called method, its directly showing the output as null!!! help me..to...understand this problem class Class1 { public static void Main(string[] args) { subscriber[] subscr; subscr = new subscriber[4]; for(int i=0;i<4;i++) { subscr = new subscriber(); } solution sol; sol = new solution(); subscr = sol.webmethod(@"D:\Documents and Settings\parimi.nandini.devi\Desktop\nandu\TestData.txt"); for(int i=0;i<4;i++) { Console.WriteLine(subscr.name); Console.WriteLine(subscr.number); Console.WriteLine(subscr.street); Console.WriteLine(subscr.city); Console.WriteLine(subscr.zipcode); } } } public class subscriber { public string name; public int number; public string street; public string city; public int zipcode; } class solution { public subscriber[] webmethod(string filepath) { subscriber[] sub; sub = new subscriber[4]; if (filepath.EndsWith(".txt")) { StreamReader sr = new StreamReader(filepath); string str = sr.ReadLine(); for (int i = 0; i < 4; i++) { sub = new subscriber(); while (str != null) { int y = str.Length; int s = str.IndexOf(" "); sub.name = str.Remove(s,(y-s-1)); int s1 = str.IndexOf(" ", s + 1); string temp = str.Remove(s1,(y-s1-1)); sub.number = Int32.Parse(temp.Substring(s + 1)); int s2 = str.IndexOf(" ", s1 + 1); string temp1 = str.Remove(s2,(y-s2-1)); sub.street = temp1.Substring(s1 + 1); int s3 = str.IndexOf(" ", s2 + 1); string temp2 = str.Remove(s3,(y-s3-1)); sub.city= temp2.Substring(s2 + 1); int s4 = str.IndexOf(" ", s3 + 1); string h = str.Substring(s3+1); sub.zipcode= Int32.Parse(h); } str = sr.ReadLine(); } return sub; } else { Console.WriteLine("input is not in a correct format"); return sub; } } }
Silicon Munky
Silicon Munky
Code without beginning of line white space is hard to read. You'll be more likely to get help if you post your code inside the following tags

Edit: doh.. the tags of course get translated for me. So just go to the link to see how to use the source tags.

More info on posting

[Edited by - Silicon Munky on February 9, 2009 11:36:16 PM]
shane1985
shane1985
It looks like your problem is where you're parsing the data fields from the input line. Instead of keeping track of the length, and the indexes of the spaces, you can use String.Split(). For example,

while (str != null){    string[] subscriberData = str.Split(' ');    sub.name = str[0];    sub.number = Convert.ToInt32(str[1]);    sub.street = str[2];    sub.city = str[3];    sub.zipcode = Convert.ToInt32(str[3]);}

Hopefully that makes sense!
-Shane

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.