Question dynamic array for user-defined type

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
i'm using VB.net 2003 application program. i'm trying to convert a VB6 program to VB.NET. The VB6 code i'm trying to convert is shown below.

declared g_Share() array in module and trying to add values to it inside form.

VB.NET:
[COLOR="Red"]VB6 (Code inside Module)[/COLOR]

'Global type array to hold printer info.
Public Type OShare
    PrinterName As String
    BackupName As String
    CurrId as Integer
End Type

'Declare dynamic array for printer info as user-defined type declared above.
Public g_Share() As OShare

[COLOR="Red"]VB6 (Code inside Form)[/COLOR]

Public Sub LoadPrinters()
     Dim dbPrinters As DAO.Database
     Dim rsPrinters As DAO.Recordset
     Dim intPosition As Integer
  
    Set rsPrinters = dbPrinters.OpenRecordset("SELECT * FROM Printer")
       
    Do Until rsPrinters.EOF
        'This variable holds the current position of the recordset
        intPosition = rsPrinters.AbsolutePosition

        'Load the array with the printer info.
        With g_Share(intPosition)
            If Not IsNull(rsPrinters!PrinterName) Then
                .PrinterName = Trim(rsPrinters!PrinterName)
            End If
            If Not IsNull(rsPrinters!BackupPath) Then
                .BackupName = Trim(rsPrinters!BackupPath)
            End If
        End With
           rsPrinters.MoveNext
    Loop
   
    rsPrinters.Close
    dbPrinters.Close
 End Sub

Public Sub Add_ComboBox(intPrinter As Integer)
	g_Share(intPrinter).CurrID = "120"
	cboPrinters.AddItem g_Share(intPrinter).PrinterName, intPrinter
End Sub


and i tried to convert the above code to vb.net as shown below.

VB.NET:
[COLOR="Red"]VB.NET (Code inside Module)[/COLOR]

'Declare dynamic array for printer info as user-defined type declared above.
Public g_Share() As OShare

'Global type array to hold printer info. 
Public Class OShare
    Public PrinterName As String
    Public BackupName As String
    Public CurrId as Integer
End Class


[COLOR="Red"]VB.NET (Code inside Form)[/COLOR]

Public Sub LoadPrinters()
            Dim intPosition As Integer = 0
          
            myConnection.Open()
            
            strSQL = "SELECT PrinterName, BackupPath FROM Printer"
            myCommand = New OleDbCommand(strSQL, myConnection)
            myReader = myCommand.ExecuteReader
            While myReader.Read
                'This variable holds the current position of the recordset 
                intPosition = intPosition

               'Load the array with the printer info.
                With g_Share(intPosition)
                    If Not IsDBNull(myReader(0)) Then .PrinterName = myReader(0)
                    If Not IsDBNull(myReader(1)) Then .BackupName = myReader(1)
                End With
               
                intPosition = intPosition + 1
            End While
            myReader.Close()
            myConnection.Close()
End Sub


Public Sub Add_ComboBox(intPrinter As Integer)
    g_Share(intPrinter).CurrID = "120"	
    cboPrinters.Items.Add(g_Share(intPrinter).PrinterName)
End Sub

when pgm runs and when it reach ".PrinterName = myReader(0)" line, it crashes.
Object reference not set to an instance of an object.
using immediate window i can see the myReader(0) value.

how can i create dynamic array for user-defined type in vb.net?

If you have any idea how to do this, please let me know and if you can provide an example, then it will be great help for me.

Thanks in advance.
 
VB.NET:
Public Structure OShare
    Public PrinterName As String
    Public BackupName As String
    Public CurrId as Integer
End Class

Public g_Share() as OShare




Private Sub XXX
    myPrinterValue = g_Share(myArrayIndex).PrinterName
End Sub
 
Thanks tom.... it start working... as you said i tried and it start working ...

VB.NET:
(Code Inside Module)

    Public g_Share() As OShare

    Public Class OShare
        Public PrinterName As String
        Public BackupName As String
       
        Public Sub New(pName As String, bName As String)
            PrinterName = pName
            BackupName = bName
        End Sub
   End Class


(Code Inside Form)

Dim nC as OShare

Do While myReader.Read
     Dim gPrinterName As String = ""
     Dim gBackupName As String = ""
                
     If Not IsDBNull(myReader(0)) Then gPrinterName = Trim(myReader(0))
     If Not IsDBNull(myReader(1)) Then gBackupName = Trim(myReader(1))

     nC = New OShare(gPrinterName , gBackupName)
     intPosition += 1
     Redim Preserve g_share(intPosition)
     g_Share(intPosition) = nc
 Loop
 
Back
Top