如何初始化在VB.NET中的结构内创建的对象数组?

时间:2015-09-11 19:49:21

标签: vb.net

无论我做什么,我最终都没有在运行时将Object引用设置为对象的实例。问题是RptParamList()。

结构是......

Partial Class WebReports_RptGeneric
    Inherits Page
    Protected ObjUtils As New Utilities
    Protected ObjDtn As New DataTable

    Structure SchedParms
        Shared ReportName As String
        Shared RptParamList() As ParameterValue
    End Structure

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim objSecurit As New STISecurity
        Dim intRc As Integer
        Dim objAudit As New Audits
        ...

    Public Sub XqtSaveSched(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Get Report Name and Report Parameters
        paramList.Clear()
        dt.Reset()
        dt = ObjUtils.GetDataTableForQuery("sp_GetPosFieldNames", paramList)
        ReDim SchedParms.RptParamList(13)
        SchedParms.RptParamList(0).Name = "rpFY" (BLOWS UP HERE!!)
        SchedParms.RptParamList(0).Value = CStr(Session("FY"))
        SchedParms.RptParamList(1).Name = "rpUserID"
        SchedParms.RptParamList(1).Value = "1000000"
        SchedParms.RptParamList(2).Name = "rpShowLinks"
        SchedParms.RptParamList(2).Value = CStr(False)

我尝试将ReDim语句添加为初始化程序,但这不起作用。我也尝试过SchedParms.RptParamList(0)= new ParameterValue(),但这也不起作用。

1 个答案:

答案 0 :(得分:0)

您需要使用New关键字创建数组,否则RptParamList将为Nothing

Shared RptParamList As ParameterValue() = New ParameterValue(13) {}

如果阵列可以增长,请使用List(Of ParameterValue),而不是重新编写阵列。列表会自动执行此操作。

Shared RptParamList As New List(Of ParameterValue)()

然后使用Add方法

添加项目
SchedParms.RptParamList.Add(New ParameterValue())