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

爱泼斯坦

无论我做什么,最终都会导致对象引用在运行时未设置为对象的实例。问题是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(),但这也不起作用。

奥利维尔·雅各·德斯科姆斯(Olivier Jacot-Descombes)

您需要使用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())

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章