Join 136,905 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,755 people online right now. Registration is fast and FREE... Join Now!
Hello again everyone! I've been working on a program that requires me to write 3 variables to a text file, per entry. I got the writing down and thats working...I think, however when I read the file and assign the text to variables, for some reason I can't get it to add the NAME variable to a List View. I've made sure that the variables are being loaded and assigned, however, the blocks of code I have don't add the NAME to the List View. Heres my code for writing the file..
CODE
public void readFromFile() { int ao=0;//creates a counter for the while StreamReader mR=new StreamReader(@"C:\StudentData.txt"); string fileName,fileAverage,fileGrade; try { while((fileName=mR.ReadLine()) !=null)//Every third variable it finds it assigns to fileName untill it reads nothing { fileAverage=mR.ReadLine(); fileGrade=mR.ReadLine(); Form2Object.recieveStudentData(fileName,fileAverage,fileGrade,ao); ao++;//count up }//end while }//end try
After loading my variables, I send them over to Form2 to be read and loaded into the list box.
CODE
public void recieveStudentData(string Name, string Average, string Grade,int counter) { arrStudentData=new string[counter+1,3];//takes AO from the readMethod and assigns it to counter arrStudentData[counter,0]=Name; arrStudentData[counter,1]=Average; arrStudentData[counter,2]=Grade; lstStudentsInFile.Items.Add(Name);//NOW THIS SHOULD ADD THE ITEM, but it doesnt... }//end method
So when I send my four variables over to recieveStudentData, I know the values are getting there, as I've tested with a MessageBox showing me the private variables Name, Grade, Average, and Counter in recieveStudentData.
So basically, I want this to happen:
On form1, when you press Show All Data, the read from file method activates, reading the txt file, loads the data into 3 variables, then on each rotation of the while loop, sends the values over to recieve student data (which runs that method then I think..). After the recieve student data method gets the values, it assigns specific slots in the array the values contained in Name, Grade, and Average. then it's SUPPOSED to add the Name variable as a new item in the List view......
Sorry, I'm still very new to programming and my code is still very haywire and sloppy. And reccomendations would be highly appreciated!
Oh and I was so impressed with the help I got last time, I linked this forum on my myspace and website
Sorry we had to make you wait, been swamped with questions all over the place. Your receiveStudentData is having problems because of how you defined the array being a 2D array. But first we want to make sure that you have the listview setup correctly so that you can receive this information once we do get it.
The listview control should be set to have three column headers. So in the properties window (under the column header property) construct three headers.
Once you have that done, set the listview "view" property to "Details". In design mode you should now see your three column headers magically appear.
Now we are ready to give it some data.
The first part is to create a string array which will hold various string arrays. An array of arrays so to speak. This is going to be outside of any function and setup as a form level array variable. You can set it up like...
csharp
// Construct an array of 100 string arrays. String[][] arrStudentData = new String[100][];
We put it outside the functions because we want this array to persist and not be reset each time we call receiveStudentData. In that function we want to do a few things including construct an array of strings which will contain the name, average, and grade. Then once we do that, we will construct a listview item object which then we can give to the listview to process and populate the number of columns.
csharp
// Build the string array using name, average and grade String[] thestudent = new String[] { Name, Average, Grade };
// Save the student in the array of students (now form level) arrStudentData[counter] = thestudent;
// Construct the listview item using this student ListViewItem item = new ListViewItem(arrStudentData[counter]);
// Add the listview item to the listview lstStudentsInFile.Items.Add(item);
So now in our listview we should see the person's name in the first column, their average in the second, and grade in the third column.
Just be careful when you go to do any math on the students averages. Remember to convert them back to Double before adding or averaging them.
That should get your program moving.
"At DIC we be listview adding code ninjas... we add to the local neighborhoods gang problems too!"
Hello there and thank you for the response! No worries about making me wait hah.
Well I tried your method of declaring arrays. It seems the array is being loaded, however it still isn't adding to the list. Let me describe the process I'm using:
1)Form1 is where you enter new student data 2)When clicking on the Show/Search button on the main form, it hides the main form, then opens form2 (where I want the student data displayed). Then activates readFromFile method. 3)readFromFile pulls out all students and the corresponding average/grade and sends them to recieveStudentData. 4)RecieveStudentData loads the 3 variables into the multi-dimensional array and trys to add it to the listView.
(This is all supposed to happen when I switch forms so that the Names are displayed on a list view on the left, then you can click the names and another listView displays the student information.)
I think my problem is with the readFromFile method. Does that method look correct for reading the 3 variables I need?
Well you said that part was working right and that you also were sending the data to receiveStudentData just fine.
QUOTE
So when I send my four variables over to recieveStudentData, I know the values are getting there, as I've tested with a MessageBox showing me the private variables Name, Grade, Average, and Counter in recieveStudentData.
So you must be reading them properly and sending them to the function if the above statement is true right?
Yes, my mistake. The variables ARE in fact getting to recieveStudentData. For some reason unknown to me however, it doesn't matter what format I add the variables to the list in, it never adds anything if the command is in the recieveStudentData method. I could ever try to add a predetermined string using:
Ok so, new development. my variable assignment is working flawlessly with a multi-dimensional array now. However I'm still having difficulties AUTO-adding the Name to the list view. I think thats a key phrase I omitted in the first post. This is all done automatically upon entering Form2. When I add the Class2Object.readFromfile method into the Form2 constructor, I get an error and my application closes(and yes when trying that I do declare Class2)
I've tried using another method to add the students to the list, and calling that method inside recieveStudentData. however that fails to work as well. Wonder if I'm missing a critical line of code and this is the universe's way of telling me to pay more attention to detail.... O_o
For clarifications sake on my part, when a method is called (or when data is sent to a method) it runs that method, right? So something like:
CODE
Form1Object.recieveStudentData(name,average);
:should run the recieveStudentdata method, correct?
This post has been edited by Vaune: 1 Apr, 2008 - 12:06 PM
Show us where you declare Class2Object and the full constructor for form2. We need to see what your code looks like and what type of error message you are getting would be nice also.
Well...after taking what I had to class and showing my proff, even he didn't know why it wouldn't work.
I activated the readFromFile method on Form2_Load. The variables were read from the txt file, correctly assigned to the array, but no matter what we did the list box wouldn't add the names.
I think it won't add them due to Form2_Load not being treated as an action, and in my limited knowledge, listboxes may not add unless its being called from an action (such as clicking a button).
I tried adding the names via the array in the Form2_Load method, however I encountered an error: "Object is not set to an instance object" or something along those lines, even though I made sure I loaded the array BEFORE trying to add them. Thoughts?
You are missing a critical piece somewhere and not telling me everything I need to know.
First of all, Form2_load is just like all the other events. It should fire your readfromfile just fine and read in the data. It is how you are adding to the listview (and it is listview isn't it because you just said listbox... two different things here) or how you have the listview configured.
Worst comes to worst, you can zip up your solution and input text file and attach them to the thread and I can see first hand what you are doing.
If you are getting the data from file and over to receiveStudentData intact there is no reason why you shouldn't be adding it to the listview.
Ah! I'm so sorry for the long ass time since I replied. I lost my connection for a while, and recently got it back.
(and I tried both listboxes and views, but for the sake of continuity I used a listview the whole time I talked to you.)
Well, since I started this I moved on because I couldn't solve the problem, however I've run into it again, this time with textboxes. I have a loaded array (like last time) and I know its correctly loaded. When I go to assign the data cell to a TextBox..nothing.
CODE
textBox1.Text=array1[0]; (array1 is a string array, so I know thats not the problem.)
I also tried adding .ToString();
I tried to fill several textboxes like that, but alas they do nothing. I checked the protection level, read-only, and other such properties. Everything seems to be fine. When I use that code in a dataMethod (dataMethod is connected to a button this time, however it fires through a Class2 readFromFile Method) nothing happens. When I move the textbox assigning code to the button event itself, I get an error, the same error I got last time.
CODE
Object not set to an instance on an object.
or something like that. I think that means the array isnt being loaded. However, I declared the array globally so all methods can call it. In the dataMethod, after reading the data from the file, splitting it, and assigning the values to the different array cells, I called a messagebox to display the contents of the array. When I call the messagebox in the dataMethod, it correctly displays the correct data. However, it only displays that correct data in the dataMethod. Otherwise the array is not loaded, which it odd because I made sure the method path to load the array is being called before I try to access the array data itself.
I checked my code to see if I was somehow resetting the array before it could be displayed, but I found no such actions.
I'm using Visual Studio .NET (2003), maybe that has some relavence.
This is a brief overview of how I load the array. 1) Snag the data from the textboxes (user input). 2) Send said data to a StreamWriter, to be saved. 3) Read the file, assign all numbers as one string (not array). //Last time I sent each number as I read it// 4) Send the number string (containing all numbers from the saved file) to a different class. 5) Split the string and assign each split to a new cell in array1.
Like I said, the data is being written, read, and assigned correctly (assigned correctly if it's in the dataMethod only - even though it's a global array.)
~Vaune
This post has been edited by Vaune: 7 Apr, 2008 - 01:54 PM
I notice you have a very bad habit of explaining things we can't see. Rule number 1 for any questions you ask on here, provide the explanation as you are, but ALWAYS provide the code you are referring to. Without seeing what you are doing how are we suppose to know where you are going wrong?
Obviously you have a scope issue here but until we can see where you have defined your array, how you are accessing it, and where you are seeing the data and where you are not, we can do little to really help.
So please post all the code you can that refers to the definition of the array, where you are setting it with your strings and where you are attempting to get access to the array elements.
//Read the PREMADE text file.. public void readFromFile(string filePath, string pPlayRead) { //Declare the string I will load the numbers into. Mult is another number needed for my calculations string numbers,mult;
StreamReader mR=new StreamReader(filePath); try { if (pPlayRead=="true") { //All the numbers are in one line, with no newline character '[]' in between them. numbers=mR.ReadLine();
//Mult is seperated by a newline character '[]' mult=mR.ReadLine();
//Declare the PowerBall class so I can send my numbers.. PowerBallWinning sendToPB=new PowerBallWinning();
//sends the numbers sendToPB.recieveData(numbers,mult); }//end if...
else { //the reason for the if/else is I'm reading and writing from two seperate files using the same method. //The files I read and write from are denoted by 'filePath', which is declared in the corresponding classes. numbers=mR.ReadLine(); }//end else... }//end try... catch(FileNotFoundException) { MessageBox.Show("File not found. Please make sure file has been created and in the 'C:\' folder."); }//end catch... }//end method...
csharp
//Over in my PowerBall Class, I have recieveData //Declare array globally string [] arrTest=new string[6];
//start the recievedata method public void recieveData(string numbers, string mult) //numbers contains 6 numbers, seperated by spaces. { //Split numbers down. Splitter is defined as: char [] splitter={' '}; arrTest=numbers.Split(splitter);
//This is the messagebox that shows the CORRECT data MessageBox.Show(arrTest[0]+arrTest[1]+arrTest[2]);
//oldMult is the multiplier I'm writing over oldMult=int.Parse(mult);
//And here is where I'm trying to make the textboxes display the data txtPowerWin1.Text=arrTest[0]; txtPowerWin2.Text=arrTest[1]; txtPowerWin3.Text=arrTest[2]; txtPowerWin4.Text=arrTest[3]; txtPowerWin5.Text=arrTest[4]; txtBallWin.Text=arrTest[5]; txtPPlayMult.Text=oldMult.ToString(); }//end method...
csharp
//The button I use to start this god forsaken process... private void btnLoadPowerBall_Click(object sender, System.EventArgs e) { //Declare my StreamWriting/Reading class.. streamClass newStream=new streamClass();
//Initialzes the read file method using the filePath for the corresponding text file. newStream.readFromFile(filePath,"true"); }//end button method...
edited for readability.
If you need any additional code / explanations, just ask and i'll be happy to oblige
This post has been edited by Vaune: 7 Apr, 2008 - 03:00 PM