Structure in vb.net

Posted on 2007-10-05
Medium Priority
Last Modified: 2013-11-26
How do i convert this code from vb 6.0 to vb.net

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 vb.net
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
Question by:anusdesai
  • 2
  • 2
LVL 43

Expert Comment

ID: 20021030
I assume that you have declared RunDetails:

Dim RunDetails() As AutoRunDetails
ReDim RunDetails(20)

For example.

Author Comment

ID: 20021065
Yes,i have done that
 ReDim RunDetails(BatchTotal)
LVL 25

Expert Comment

ID: 20021261
        <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?
LVL 43

Accepted Solution

TimCottee earned 2000 total points
ID: 20021299
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.

Author Comment

ID: 20032680
Cheers mate

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question