質問

I'm doing this program for a class; I have a listbox with 4 choices that are counted each time they're selected,and i'm supposed to write out the results to an out.File so that they can then be retrieved and displayed when asked to.

Here's an image,so you can see what i mean.

Here's the code i got for the file part so far:

'declare a streamwriter variable
    Dim outFile As IO.StreamWriter

    'open file for output
    outFile = IO.File.CreateText("Commercials.txt")

    'write to file
    For intIndex As Integer = 0 To lstCommercial.Items.Count - 1
        outFile.WriteLine(lstCommercial.Items(intIndex))
    Next intIndex

    'close th efile
    outFile.Close()

So my problem is this,i can get everything to work except for it to write the totals to the file,with the result of not displaying. How do i go about doing this? What am i doing wrong in any case?

役に立ちましたか?

解決

It depends on what lstCommercial.Items is. If it is a Collection object, then you can get the index of an item using the following:

outFile.WriteLine(lstCommercial.Items.Item(intIndex))

If it is an array, then instead of using the Count property to find the length, you should rather use GetUpperBound(0) to find the last index:

For intIndex As Integer = 0 To lstCommercial.Items.GetUpperBound(0)

I have very little experience in Visual Basic so I am probably wrong, but I have just gathered this from reading Arrays in Visual Basic and Collection Object (Visual Basic).

Also, looking at the docs here, you may need to 'Flush' the buffer to the file before closing it:

outFile.Flush()
'close the file
outFile.Close()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top