473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

dynamic array for user-defined type in VB.NET

115 100+
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
1 4475
remya1000
115 100+
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

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

Similar topics

5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
3
by: Nikesh | last post by:
project is abt encrypting a txt file with an image.... in that i will to accept a txt file from the user ..this file a need to be stored in an array...thus file size will keep on changing....and...
2
by: Ghada Al-Mashaqbeh via DotNetMonster.com | last post by:
Hi all, I am facing a problem in dynamic code generation at run time, the problem occurs when the dynmaic code use global data exist within the original application. Lets say that my...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
9
by: dennis.sam | last post by:
Hi, Is there away to define a multi-dimensional array with respect to the number of dimensions the array has? For example, given a user spec of "a b c d", I want to create a 4 dimensional array...
0
by: Eniac | last post by:
Hi, I've been working on a custom user control that needs to be modified and the validation is causing me headaches. The control used to generate a table of 4 rows x 7 columns to display all...
1
by: Tinku | last post by:
Hi friends I know Static Hashing and i know about Dynamic Hashing, still i have problem to make program with Dynamic Hashing I am new in "C" world, please help me, my problem is: i have to...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
2
by: englishman69 | last post by:
Hello, I have been banging my head against this one for a while... Searches online have revealed many different proposals for correcting my issue but none that I can follow! My basic situation...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.