Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > .NET Framework 1.x
|
.NET Framework 1.x For discussing versions 1.0 and 1.1 of the Microsoft .NET Framework.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 1.x section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2009, 01:44 PM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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 ".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.
 
Old April 2nd, 2009, 03:47 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Have you tried:

If myReader(0) IsNot Nothing

or

If myReader(0) IsNot DBNull.Value

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 2nd, 2009, 03:51 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Nevermind, I misunderstood the question. The problem is with this:

Public g_Share() As OShare

You;re declaring it, but never newing it up.

Do you have to use an array? If so, you need to instantiate it first.

Otherwise, you could use a Hashtable or ArrayList (although you loose type safety a bit).

And if this doesn't have to be .NET 1.x but later you could use Generics and do something like:

Dim g_Shares As New List(Of OShare)

And later:

g_Shares.Add (New OShare).

Hope this helps a bit.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 2nd, 2009, 04:37 PM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much Imar....

it start working... as you s aid 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
ADODB.Connection user-defined type not defined Wall st Guru Excel VBA 2 March 26th, 2014 03:44 PM
Setting variable to user defined type mantle51 SQL Server 2000 3 August 10th, 2007 03:52 AM
User-defined type not defined (Icecream.mdb) dloren01 BOOK: Beginning Access VBA 0 June 22nd, 2005 10:36 PM
VB.net Database -> user defined object boneill VB How-To 4 May 2nd, 2005 05:23 PM
Table and User Defined Type with same Name djuricd SQL Server 2000 3 April 20th, 2004 12:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.