By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

dynamic array for user-defined type in VB.NET

100+
P: 115
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.

Expand|Select|Wrap|Line Numbers
  1. VB6 (Code inside Module)
  2.  
  3. 'Global type array to hold printer info.
  4. Public Type OShare
  5.     PrinterName As String
  6.     BackupName As String
  7.     CurrId as Integer
  8. End Type
  9.  
  10. 'Declare dynamic array for printer info as user-defined type declared above.
  11. Public g_Share() As OShare
  12.  
  13. VB6 (Code inside Form)
  14.  
  15. Public Sub LoadPrinters()
  16.      Dim dbPrinters As DAO.Database
  17.      Dim rsPrinters As DAO.Recordset
  18.      Dim intPosition As Integer
  19.  
  20.     Set rsPrinters = dbPrinters.OpenRecordset("SELECT * FROM Printer")
  21.  
  22.     Do Until rsPrinters.EOF
  23.         'This variable holds the current position of the recordset
  24.         intPosition = rsPrinters.AbsolutePosition
  25.  
  26.         'Load the array with the printer info.
  27.         With g_Share(intPosition)
  28.             If Not IsNull(rsPrinters!PrinterName) Then
  29.                 .PrinterName = Trim(rsPrinters!PrinterName)
  30.             End If
  31.             If Not IsNull(rsPrinters!BackupPath) Then
  32.                 .BackupName = Trim(rsPrinters!BackupPath)
  33.             End If
  34.         End With
  35.            rsPrinters.MoveNext
  36.     Loop
  37.  
  38.     rsPrinters.Close
  39.     dbPrinters.Close
  40.  End Sub
  41.  
  42. Public Sub Add_ComboBox(intPrinter As Integer)
  43.     g_Share(intPrinter).CurrID = "120"
  44.     cboPrinters.AddItem g_Share(intPrinter).PrinterName, intPrinter
  45. End Sub
  46.  

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

Expand|Select|Wrap|Line Numbers
  1. VB.NET (Code inside Module)
  2.  
  3. 'Declare dynamic array for printer info as user-defined type declared above.
  4. Public g_Share() As OShare
  5.  
  6. 'Global type array to hold printer info. 
  7. Public Class OShare
  8.     Public PrinterName As String
  9.     Public BackupName As String
  10.     Public CurrId as Integer
  11. End Class
  12.  
  13.  
  14. VB.NET (Code inside Form)
  15.  
  16. Public Sub LoadPrinters()
  17.             Dim intPosition As Integer = 0
  18.  
  19.             myConnection.Open()
  20.  
  21.             strSQL = "SELECT PrinterName, BackupPath FROM Printer"
  22.             myCommand = New OleDbCommand(strSQL, myConnection)
  23.             myReader = myCommand.ExecuteReader
  24.             While myReader.Read
  25.                 'This variable holds the current position of the recordset 
  26.                 intPosition = intPosition
  27.  
  28.                'Load the array with the printer info.
  29.                 With g_Share(intPosition)
  30.                     If Not IsDBNull(myReader(0)) Then .PrinterName = myReader(0)
  31.                     If Not IsDBNull(myReader(1)) Then .BackupName = myReader(1)
  32.                 End With
  33.  
  34.                 intPosition = intPosition + 1
  35.             End While
  36.             myReader.Close()
  37.             myConnection.Close()
  38. End Sub
  39.  
  40.  
  41. Public Sub Add_ComboBox(intPrinter As Integer)
  42.     g_Share(intPrinter).CurrID = "120"    
  43.     cboPrinters.Items.Add(g_Share(intPrinter).PrinterName)
  44. End Sub
  45.  
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.
Apr 2 '09 #1
Share this Question
Share on Google+
1 Reply


100+
P: 115
it start working... i tried this code...

Expand|Select|Wrap|Line Numbers
  1. (Code Inside Module)
  2.  
  3.     Public g_Share() As OShare
  4.  
  5.     Public Class OShare
  6.         Public PrinterName As String
  7.         Public BackupName As String
  8.         Public CurrID As Long
  9.  
  10.         Public Sub New(pName As String, bName As String)
  11.             PrinterName = pName
  12.             BackupName = bName
  13.         End Sub
  14.    End Class
  15.  
  16.  
  17. (Code Inside Form)
  18.  
  19. Dim nC as OShare
  20.  
  21. Do While myReader.Read
  22.      Dim gPrinterName As String = ""
  23.      Dim gBackupName As String = ""
  24.  
  25.      If Not IsDBNull(myReader(0)) Then gPrinterName = Trim(myReader(0))
  26.      If Not IsDBNull(myReader(1)) Then gBackupName = Trim(myReader(1))
  27.  
  28.      nC = New OShare(gPrinterName , gBackupName)
  29.      intPosition += 1
  30.      Redim Preserve g_share(intPosition)
  31.      g_Share(intPosition) = nc
  32.  Loop
  33.  
Apr 2 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.