declared g_Share() array in module and trying to add values to it inside form.
Expand|Select|Wrap|Line Numbers
- 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.
Expand|Select|Wrap|Line Numbers
- 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
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.