Structure in

How do i convert this code from vb 6.0 to

Type AutoRunDetails
    Name As String
    doControls As Boolean
    ReportTotal As Integer
    ReportType(1 To 20) As String
    ReportName(1 To 20) As String
End Type

Also how do add values in it

example vb 6.0
    RunDetails(BatchNo).ReportType(RptCount) = "RI"

but if i do the same in
Structure AutoRunDetails
            Dim Name As String
            Dim doControls As Boolean
            Dim ReportTotal As Short
            <VBFixedArray(20)> Dim ReportType() As String
            <VBFixedArray(20)> Dim ReportName() As String            
            Public Sub Initialize()
            ReDim ReportType(20)
            ReDim ReportName(20)
            End Sub
      End Structure
      RunDetails(BatchNo).ReportType(RptCount) = "RI"

i get error object not set to reference
Who is Participating?
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
Not having tried this before I assumed that it would be easier than it actually is. Why on earth it is more complicated in .Net than VB6 I do not know. Anyway here is a workable solution to the issue:

    Structure AutoRunDetails
        Public Name As String
        Public doControls As Boolean
        Public ReportTotal As Short
        Public ReportType() As String
        Public ReportName() As String
        Public Sub New(ByVal Length As Integer)
            Length = 20
            ReDim ReportType(Length)
            ReDim ReportName(Length)
        End Sub
    End Structure


        Dim RunDetails(20) As AutoRunDetails
        For I As Integer = 0 To RunDetails.GetUpperBound(0)
            RunDetails(I) = New AutoRunDetails(20)
        RunDetails(12).ReportType(10) = "RI"
        RunDetails(20).ReportType(10) = "RI"

For example, this initialises the arrays when each element of the RunDetails() array is scanned. It then works as you would expect.

Not a nice implementation perhaps but it seems to be the only way of doing it, all the obvious shortcuts e.g.,

Dim RunDetails() As AutoRunDetails = New AutoRunDetails(20)

Etc don't seem to work whereas this does.
TimCotteeHead of Software ServicesCommented:
I assume that you have declared RunDetails:

Dim RunDetails() As AutoRunDetails
ReDim RunDetails(20)

For example.
anusdesaiAuthor Commented:
Yes,i have done that
 ReDim RunDetails(BatchTotal)
        <VBFixedArray(20)> Dim ReportType() As String
            <VBFixedArray(20)> Dim ReportName() As String      
            Dim ReportType(20) As String
            Dim ReportName(20) As String      
if you want two fixed arrays with 20 elements.

You cannot change instance data from within a method in a struct.  You can only change static data.

If you want to do this sort of thing, why not just use  a class and the constructor?
anusdesaiAuthor Commented:
Cheers mate
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.