Arrays

G

Guest

I am writing an application and I want to be able to setup an array when the
form loads with some data I will use throughout the program.

Public Shared ptrArray() As String

Public Sub Initilize_Variables()
'Load Default Directory Information
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open("C:\Program Files\Requisition
Print\Requisition Print Default Directories.xls")
oSheet = oBook.Worksheets("Printers")
Dim i As Integer
Dim CellA As String
Dim CellB As String
For i = 1 To 10
CellA = "$A$" & i
CellB = "$B$" & i
ptrArray(oSheet.Range(CellA).Text) = oSheet.Range(CellB).Text
Next
oBook.Close(False)
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
End SubT
This code gets an error "Conversion from string "F3075" to type 'Integer' is
not valid" in this line of code:
ptrArray(oSheet.Range(CellA).Text) = oSheet.Range(CellB).Text

"F3075" should be the Array subscript

What I want to end up with is an array like this:

ptrArray("F3075") = "Epson Printer"
ptrArray("Form2") = "HP Printer"
etc.
Both the subscript and the data are picked up from a predefined file.

Any help would be appreciated.
 
S

shawn.bower

You need to use a hashtable instead of an array, for example:

Imports System
Imports System.Collections

Public Class MainClass

Public Shared Sub Main()

Dim table As Hashtable = New Hashtable()

Dim employee As New Employee("First Name", "Last Name")

Try
table.Add("Last Name", employee)
Console.WriteLine("Put: " & employee.ToString())

Catch argumentException As ArgumentException
Console.WriteLine(argumentException.ToString())
End Try

Dim result As Object = table("Last Name")

If Not result Is Nothing Then
Console.WriteLine("Get: " & result.ToString())
Else
Console.WriteLine("Get: " & "Last Name" & " not in table")
End If


table.Remove("Last Name")

Console.WriteLine("table count: " & table.Count)

Console.WriteLine("Contains key: " & table.ContainsKey("Last
Name"))
table.Clear()

Dim enumerator As IDictionaryEnumerator = table.GetEnumerator()

While enumerator.MoveNext()
Console.WriteLine(Convert.ToString(enumerator.Value))
End While


enumerator = table.GetEnumerator()

While enumerator.MoveNext()
Console.WriteLine(enumerator.Key )
End While

End Sub

End Class



Public Class Employee
Private firstName, lastName As String

Public Sub New(ByVal first As String, ByVal last As String)
firstName = first
lastName = last
End Sub

Public Overrides Function ToString() As String
Return firstName & " " & lastName
End Function

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top