Class as an array

  • Thread starter Glenn Lacampuenga
  • Start date
G

Glenn Lacampuenga

Still fairly new to vb.net and was wondering if it's possible to Dim a class
as an array.
And if so how do i store data to it. I have listed my code below.

Here's my code

Public Shared Function LookupEmployeeByLastName(ByVal EmployeeLastName As
String) As Employee()

Dim EmployeeDS As New DataSet

Dim EmpRecord() As Employee 'DIMENSION CLASS AS AN ARRAY

Dim EmployeeTable As DataTable

Const Strconn As String = "integrated security=SSPI;data
source=GLENNL;initial catalog=BSGData"

Dim Myconn As New SqlConnection(Strconn)

Dim MyAdapter As New SqlDataAdapter _

("Exec LookUpEmployeesByName @LastName", Myconn)

MyAdapter.SelectCommand.Parameters.Add( _

"@LastName", SqlDbType.VarChar).Value = EmployeeLastName

MyAdapter.Fill(EmployeeDS, "Employees")

EmployeeTable = EmployeeDS.Tables(0)

Dim anEmployee As DataRow

Dim iRow, nRow As Integer

nRow = EmployeeTable.Rows.Count

For iRow = 0 To nRow - 1

anEmployee = EmployeeTable.Rows(iRow)

EmpRecord(irow).FirstName = anEmployee.Item("FirstName") ' THIS IS WHERE I
RUN INTO PROBLEMS

Nexst

Return EmpRecord
 
A

_AnonCoward

: Still fairly new to vb.net and was wondering if it's possible to Dim a
: class as an array.
: And if so how do i store data to it. I have listed my code below.
:
: Here's my code
:
: Public Shared Function LookupEmployeeByLastName(ByVal EmployeeLastName As
: String) As Employee()
:
: Dim EmployeeDS As New DataSet
:
: Dim EmpRecord() As Employee 'DIMENSION CLASS AS AN ARRAY
:
: Dim EmployeeTable As DataTable
:
: Const Strconn As String = "integrated security=SSPI;data
: source=GLENNL;initial catalog=BSGData"
:
: Dim Myconn As New SqlConnection(Strconn)
:
: Dim MyAdapter As New SqlDataAdapter _
:
: ("Exec LookUpEmployeesByName @LastName", Myconn)
:
: MyAdapter.SelectCommand.Parameters.Add( _
:
: "@LastName", SqlDbType.VarChar).Value = EmployeeLastName
:
: MyAdapter.Fill(EmployeeDS, "Employees")
:
: EmployeeTable = EmployeeDS.Tables(0)
:
: Dim anEmployee As DataRow
:
: Dim iRow, nRow As Integer
:
: nRow = EmployeeTable.Rows.Count
:
: For iRow = 0 To nRow - 1
:
: anEmployee = EmployeeTable.Rows(iRow)
:
: EmpRecord(irow).FirstName = anEmployee.Item("FirstName") ' THIS IS WHERE
: I RUN INTO PROBLEMS
:
: Nexst
:
: Return EmpRecord


You have not dimensioned your EmpRecord array. You define it here:


Dim EmpRecord() As Employee 'DIMENSION CLASS AS AN ARRAY


but you never state it's size. An array has a fixed length and you must
specify it at some point. What you need is something along these lines:


(Caution: untested)

nRow = EmployeeTable.Rows.Count

ReDim EmpRecord(nRow - 1)

For iRow = 0 To nRow - 1


Ralf
 
Top