英文:
How do I add new elements to an array if I do not know the final size when I declare it? (Resolved)
问题
我正在尝试创建一个应用程序(WinForm),以使用SOAP将数据从我的服务器发送到外部服务器。
我创建了数组:
Friend EventArray() As GPSmodule.gps.com.module.gps.Event
Dim ReportCount as Integer=0;
然后,在任务内部,我有一个循环,读取来自数据库的每一行作为SQL请求的结果,并按要求格式化每个值。之后,我将数据添加到当前索引。
EventArray(ReportCount).plate = Patente
EventArray(ReportCount).Evtcode = Evento
EventArray(ReportCount).date = FechaHora
EventArray(ReportCount).latitude = Latitud
EventArray(ReportCount).longitude = Longitud
EventArray(ReportCount).speed = Velocidad
EventArray(ReportCount).temperature = Sensor1
ReportCount+=1
但是这不起作用,因为EventArray没有大小。我应该如何做?
英文:
I'm trying to create an app(WinForm) to send data from my server to an external server using SOAP.
I created the array
Friend EventArray() As GPSmodule.gps.com.module.gps.Event
Dim ReportCount as Integer=0;
Then inside the task I have a loop that read every line that comes from the database as result of an SQL request and format every value as requested.
After that I add the data to the current index.
EventArray(ReportCount).plate = Patente
EventArray(ReportCount).Evtcode = Evento
EventArray(ReportCount).date = FechaHora
EventArray(ReportCount).latitude = Latitud
EventArray(ReportCount).longitude = Longitud
EventArray(ReportCount).speed = Velocidad
EventArray(ReportCount).temperature = Sensor1
ReportCount+=1
But this doesn't work because EventArray has no size. How should I do it?
答案1
得分: 1
Redim Preserve EventArray(ReportCount) '<<< 添加这行
英文:
Resizing arrays is something VB.NET can do that C# can't:
Redim Preserve EventArray(ReportCount) '<<< add this line
EventArray(ReportCount).plate = Patente
EventArray(ReportCount).Evtcode = Evento
EventArray(ReportCount).date = FechaHora
EventArray(ReportCount).latitude = Latitud
EventArray(ReportCount).longitude = Longitud
EventArray(ReportCount).speed = Velocidad
EventArray(ReportCount).temperature = Sensor1
ReportCount+=1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论