Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > .NET Framework 1.x
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 2nd, 2009, 02:44 PM
Authorized User
Points: 386, Level: 6
Points: 386, Level: 6 Points: 386, Level: 6 Points: 386, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2007
Location: windsor, toronto, Canada.
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.
Reply With Quote
  #2 (permalink)  
Old April 2nd, 2009, 04:47 PM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,050
Thanks: 80
Thanked 1,581 Times in 1,558 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!
Reply With Quote
  #3 (permalink)  
Old April 2nd, 2009, 04:51 PM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,050
Thanks: 80
Thanked 1,581 Times in 1,558 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!
Reply With Quote
  #4 (permalink)  
Old April 2nd, 2009, 05:37 PM
Authorized User
Points: 386, Level: 6
Points: 386, Level: 6 Points: 386, Level: 6 Points: 386, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2007
Location: windsor, toronto, Canada.
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
Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 04:44 PM
Setting variable to user defined type mantle51 SQL Server 2000 3 August 10th, 2007 04:52 AM
User-defined type not defined (Icecream.mdb) dloren01 BOOK: Beginning Access VBA 0 June 22nd, 2005 11:36 PM
VB.net Database -> user defined object boneill VB How-To 4 May 2nd, 2005 06:23 PM
Table and User Defined Type with same Name djuricd SQL Server 2000 3 April 20th, 2004 01:33 AM



All times are GMT -4. The time now is 03:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.