vb.net - Pass a global array to routine for altering using vb.net
问题描述
I have various classes, and am trying to make routines that can edit global arrays for the respective classes. Let me explain..
Let's say one class is a person. Contained in this class is NAME, SSN, DOB, ADDRESS(as an object), etc.
Another class would be address. Contained in this class is NUMBER, STREET, CITY, STATE, ZIPCODE.
I have global arrays which hold each object for those classes. So I have an array of people and an array of addresses.
Public Shared peopleArr() as peopleClass
public shared addressArr() as addressClass
I am writing a method to resize the arrays as objects are added. So if a person is added the routine will check to see whether or not the array is empty and will subsequently add an empty spot creating a new object.
redim preserve peopleArr(0 to 1)
peopleArr(1) = new personClass
I would like one routine to complete this task no matter what type of array is being altered, but I keep getting an error "Option Strict On disallows narrowing from type 'Object()' to type 'PersonClass()' in copying the value 'ByRef' parameter 'objectArray' back to the matching argument.
If I turn off option strict then I get an error at runtime:
System.InvalidCastException: 'Unable to cast object of type 'System.Object[]' to type 'SMv3._0.personClass[]'.'
Basically, whether personArr or addressArr is passed, I'd like the routine to resize it and create a new item of the respective class, and actually make that change to the global array.
Here is the main call: CreateRemoveObjectArraySpot(globalVariables.personArr(), "")
and here the the routine:
Public Sub CreateRemoveObjectArraySpot(ByRef objectArray As Object(), deleteString As String)
Try
If deleteString = "" Then 'if deletestring is "" then an array spot will be created
If objectArray Is Nothing Then
ReDim objectArray(0 To 0)
objectArray(0) = New Object
Else
End If
Else 'if deletestring is not blank then the array spot containing the string will be deleted
End If
Catch ex As Exception
ErrorDisplay(ex)
End Try
End Sub
解决方案
推荐阅读
- .net-core - Dotnetcore:如何从服务引用中获取 httpClient?
- javascript - 将不间断连字符从 MS-Word 复制并粘贴到 HTML-Textarea
- swift - 在 Swift 测试驱动开发中对 @ObservableObject 进行单元测试
- php - Most cost effective package combination (shipping)
- android - 从线程完成后活动没有停止
- python - 如何使用 SQLAlchemy 遍历关系表?
- wkwebview - javascript中的本机图像对象代理
- vue.js - 对 v-data-table 进行排序检查复选框
- keras - 我从 keras.model.predict 得到不同的结果
- c# - asp.net DetailsView 允许 ID / PK 条目