dynamic array for user-defined type in VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • remya1000
    New Member
    • Apr 2007
    • 115

    dynamic array for user-defined type in VB.NET

    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.

    Code:
    VB6 (Code inside Module)
    
    '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
    
    VB6 (Code inside Form)
    
    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.

    Code:
    VB.NET (Code inside Module)
    
    '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
    
    
    VB.NET (Code inside Form)
    
    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 ".PrinterNa me = 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.
  • remya1000
    New Member
    • Apr 2007
    • 115

    #2
    it start working... i tried this code...

    Code:
    (Code Inside Module)
    
        Public g_Share() As OShare
    
        Public Class OShare
            Public PrinterName As String
            Public BackupName As String
            Public CurrID As Long
           
            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

    Comment

    Working...