我的vb程序有问题,因为我的结构数组不会填充使用split命令拆分的文本文件中的数据。困惑。任何建议都会很棒。代码如下。
Imports System.IO
Public Class Form1
Public Structure ScavRecord
Public latitude As String
Public longitude As String
Public message As String
Public encrypted As String
End Structure
Dim Scav(4) As ScavRecord
Dim nextScav As Integer
Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
Dim read As New StreamReader("F:\Higher Computing Science\Assignment\files for candidates\messages.txt")
nextScav = 0
Do
Dim temp As String = read.ReadLine()
Dim seperated As String() = temp.Split(",")
Scav(nextScav).latitude = seperated(0)
Scav(nextScav).longitude = seperated(1)
Scav(nextScav).message = seperated(2)
nextScav = nextScav + 1
ListBox.Items.Add(Scav(nextScav).latitude)
ListBox.Items.Add(Scav(nextScav).longitude)
ListBox.Items.Add(Scav(nextScav).message)
Loop Until read.EndOfStream()
End Sub
End Class
答案 0 :(得分:0)
结构是值类型,这意味着当您从某个地方获取结构类型的值时,您将获得一个副本。这包括访问值类型数组中的项目,就像您使用Scav(nextScav)
一样。所以这一行:
Scav(nextScav).latitude = seperated(0)
首先评估Scav(nextScav)
并在ScavRecord
处为您提供nextScav
的副本。然后为其latitude
指定一个值。然后将副本丢弃。
要解决此问题,您需要分配整个值:
Dim record As New ScavRecord()
record.latitude = separated(0)
record.longitude = separated(1)
record.message = separated(2)
Scav(nextScav) = record