如何在声明数组时不知道最终大小的情况下向数组添加新元素?(已解决)

huangapple go评论53阅读模式
英文:

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) &#39;&lt;&lt;&lt; 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

huangapple
  • 本文由 发表于 2023年6月16日 06:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定