Reply
dynamic array for user-defined type in VB.NET
Old 04-02-2009, 02:51 PM dynamic array for user-defined type in VB.NET
Average Talker

Posts: 23
Name: remya
Trades: 0
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 ".PrinterName = myReader(0)" line, it crashes.
Quote:
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 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 04-02-2009, 05:41 PM Re: dynamic array for user-defined type in VB.NET
Average Talker

Posts: 23
Name: remya
Trades: 0
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
remya1000 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to dynamic array for user-defined type in VB.NET
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.16466 seconds with 13 queries