why doesnt my code work??
-
Sunday, January 23, 2011 3:42 PM
i have just finished writing code for my project but after i would have to put it into functions and procedures, but thats not the problem, my code just wouldn't write into a csv file? im using open office for the csv file and ive done it before so it should be ok and the code i just did is pretty much exactly the same as the one ive done before, but i cant figure out why this wont work write to the file, so here is the code
- Edited by cappuccino345 Sunday, January 23, 2011 6:15 PM
All Replies
-
Sunday, January 23, 2011 4:03 PM
Your code produces exceptions trying to convert strings to numeric types.
arrDetails(count).hoursworked = fileReader.ReadLine
The statement above throws the first exception.
You should correct the code to properly convert the read text to your defined types so the code will run to completion.
-
Sunday, January 23, 2011 4:09 PM
Your code produces exceptions trying to convert strings to numeric types.
arrDetails(count).hoursworked = fileReader.ReadLine
The statement above throws the first exception.
You should correct the code to properly convert the read text to your defined types so the code will run to completion.
how?
the hoursworked is a number and i nthe structure i put it as a integer?
-
Sunday, January 23, 2011 4:13 PM
Hi,
Your code looks like a kind of VB6 (1998) or VB7 (2002)
I've tried to change it in something more from todays, while I also did did the conversion Peter is talking about.
I've made it in a way it should run on VB 2008, VB2010 and VB2010 SP1
Option Strict On Imports System.IO Class Employee Public Name As String Public Surname As String Public Hoursworked As Integer Public Rateofpay As Decimal Public Pay As Decimal End Class Module Module1 Sub Main() Dim fileReader As New StreamReader("d:\employeepay.txt") Dim fileWriter As New StreamWriter("d:\employeepayCSV.csv") Dim employees As New List(Of Employee) For count = 0 To 4 Dim empl As New Employee With {.Name = Trim(StrConv(fileReader.ReadLine, VbStrConv.ProperCase)), .Surname = Trim(StrConv(fileReader.ReadLine, VbStrConv.ProperCase)), .Hoursworked = CInt(fileReader.ReadLine), .Rateofpay = CInt(fileReader.ReadLine)} empl.Pay = empl.Hoursworked * empl.Rateofpay employees.Add(empl) Next For Each empl In employees Console.WriteLine("Name: " & empl.Name) Console.WriteLine("Surname: " & empl.Surname) Console.WriteLine("Hours Worked: " & empl.Hoursworked) Console.WriteLine("Rate of Pay: £" & empl.Rateofpay & " per hour") Console.WriteLine("Pay Owed: £" & empl.Pay) Console.WriteLine() Next For Each empl In employees With empl fileWriter.WriteLine(.Name & "," & .Surname & "," & CStr(.Hoursworked) & "," & CStr(.Rateofpay) & "," & CStr(.Pay)) End With Next Console.ReadLine() Console.Clear() Console.WriteLine("Finished writing CSV file, press any key to close") Console.ReadLine() End Sub End ModuleI can of course not test it.
Success
Cor- Proposed As Answer by pvdg42MVP Sunday, January 23, 2011 4:21 PM
-
Sunday, January 23, 2011 4:21 PM
I agree with Cor. This code, while crude, tests good.
Option Explicit On Imports System.IO Module Module1 Structure Employees Dim name As String Dim surname As String Dim hoursworked As Integer Dim rateofpay As Single Dim pay As Single End Structure Sub Main() Dim fileReader As StreamReader Dim fileName As String fileName = "d:\employeepay.txt" fileReader = New StreamReader(fileName) Dim fileWriter As StreamWriter Dim fileName2 As String fileName2 = "d:\employeepayCSV.csv" fileWriter = New StreamWriter(fileName2) Dim arrDetails(4) As Employees Dim count As Integer For count = 0 To 4 arrDetails(count).name = Trim(StrConv(fileReader.ReadLine, VbStrConv.ProperCase)) arrDetails(count).surname = Trim(StrConv(fileReader.ReadLine, VbStrConv.ProperCase)) arrDetails(count).hoursworked = Convert.ToInt32(fileReader.ReadLine) arrDetails(count).rateofpay = Convert.ToSingle(fileReader.ReadLine) arrDetails(count).pay = arrDetails(count).hoursworked * arrDetails(count).rateofpay Console.Clear() Next count Console.Clear() count = 0 For count = 0 To 4 Console.WriteLine("Name: " & arrDetails(count).name) Console.WriteLine("Surname: " & arrDetails(count).surname) Console.WriteLine("Hours Worked: " & arrDetails(count).hoursworked) Console.WriteLine("Rate of Pay: £" & arrDetails(count).rateofpay & " per hour") Console.WriteLine("Pay Owed: £" & arrDetails(count).pay) Console.WriteLine() Next count count = 0 For count = 0 To 4 fileWriter.WriteLine(arrDetails(count).name & "," & arrDetails(count).surname & "," & arrDetails(count).hoursworked.ToString & "," & arrDetails(count).rateofpay.ToString & "," & arrDetails(count).pay.ToString) Next count fileWriter.Flush() fileWriter.Close() Console.ReadLine() Console.Clear() Console.WriteLine("Finished writing CSV file, press any key to close") Console.ReadLine() End Sub End Module -
Sunday, January 23, 2011 4:26 PM
The others have fixed your problem, but I don't think they specifically said the issue. The real issue is that you do not do the line:fileWriter.Close()It often is prefereable to use a "using" statement in connection to StreamWriter objects.
--
Mike- Marked As Answer by cappuccino345 Sunday, January 23, 2011 6:14 PM

