VB Function to return array to ASP

Discussion in 'ASP General' started by Raj, Mar 22, 2007.

  1. Raj

    Raj Guest

    I need a VB function to return array of collections like

    Private Type Employee
    empname as string
    address as string
    salary as integer
    deptno as integer
    End Type

    dim employees() as Employee

    Public Function getEmployees() as Employee()
    getEmployees=employees
    End Function

    I will call that function in ASP.

    This was not working any other method is welcomed.

    Regards,

    Raj.
     
    Raj, Mar 22, 2007
    #1
    1. Advertising

  2. Raj

    Raj Guest

    Thanx indeed for the valuable input, but I still have problems:

    Here's what I did:

    Have created a standard exe project with two classes:

    Employee:
    Public empname As String
    Public address As String
    Public salary As Integer
    Public deptno As Integer

    Company:
    Dim employess() As Employee
    Dim mlCount As Long

    Public Property Get Count() As Long
    Count = mlCount
    End Property

    I had a form with Command Button. On it's click event I wrote like:

    Private Sub Command1_Click()
    Dim cmp1 As New Company

    cmp1.Item(1) = New Employee
    cmp1.Item(1).deptno = "10"
    cmp1.Item(1).empname = "Raj"

    cmp1.Item(2) = New Employee
    cmp1.Item(2).deptno = "20"
    cmp1.Item(2).empname = "Ram"

    Print cmp1.Count
    End Sub

    Then I got a compile error at:
    Public Property Get Item(ByVal Index As Long) As Employee
    Set Item = employees(Index)
    End Property

    Error Message: Sub or Function not defined.

    It seems I am missing something.

    Please let me know.

    Thank you very much.

    Regards,

    Raj.


    ----- Original Message -----
    From: "Anthony Jones" <>
    Newsgroups: microsoft.public.inetserver.asp.general
    Sent: Friday, March 23, 2007 4:32 AM
    Subject: Re: VB Function to return array to ASP


    >
    > "Raj" <> wrote in message
    > news:...
    >> I need a VB function to return array of collections like
    >>
    >> Private Type Employee
    >> empname as string
    >> address as string
    >> salary as integer
    >> deptno as integer
    >> End Type
    >>
    >> dim employees() as Employee
    >>
    >> Public Function getEmployees() as Employee()
    >> getEmployees=employees
    >> End Function
    >>
    >> I will call that function in ASP.
    >>
    >> This was not working any other method is welcomed.
    >>
    >> Regards,
    >>
    >> Raj.
    >>

    >
    > First off the code above doesn't compile. You can't expose a private type
    > as a return value of a public member. You would need make the Employee
    > type
    > public to get it to compile. Unfortunately that still doesn't help a
    > great
    > deal with ASP.
    >
    > ASP only has one data type, the variant (or two if you count an array of
    > variants as a type). Now whilst it is possible to re-arrange the VB code
    > to
    > pass a reference to User-Defined type into a variant and thus into
    > VBScript
    > you can't access the members of the type. The VBScript parsing doesn't
    > allow for the 'type.member' notation to work both for objects and for UDTs
    > as it does in VB6.
    >
    > If the ASP merely needs to hold reference to a type that is passed to
    > another component then that is possible as long as appropriate measures
    > are
    > taken to ensure the interfaces are compatible with Script in this way
    > (I.E.
    > takes a variant ByRef).
    >
    > If you need the ASP code to be able to access the members of the type then
    > you will need to use a class instead:-
    >
    > 'Class Employee
    > Public empname as string
    > Public address as string
    > Public salary as integer
    > Public deptno as integer
    >
    >
    > 'Your class
    >
    > Dim employees() as Variant ' Actuall contains employess objects
    >
    > Public Function getEmployees() As Variant
    > getEmployees = employees
    > End Public
    >
    > However that can get expensive. It may be better to turn your class into
    > a
    > form of a collection. E.g.,
    >
    > Dim employess() as Employee
    > Dim mlCount as Long
    >
    > Public Property Get Item(ByVal Index As Long) As Employee
    > Set Item = employees(Index)
    > End Property
    >
    > Public Property Get Count() as Long
    > Count = mlCount
    > End Property
    >
    >
    >
    >

    "Anthony Jones" <> wrote in message
    news:Ot%...
    >
    > "Raj" <> wrote in message
    > news:...
    >> I need a VB function to return array of collections like
    >>
    >> Private Type Employee
    >> empname as string
    >> address as string
    >> salary as integer
    >> deptno as integer
    >> End Type
    >>
    >> dim employees() as Employee
    >>
    >> Public Function getEmployees() as Employee()
    >> getEmployees=employees
    >> End Function
    >>
    >> I will call that function in ASP.
    >>
    >> This was not working any other method is welcomed.
    >>
    >> Regards,
    >>
    >> Raj.
    >>

    >
    > First off the code above doesn't compile. You can't expose a private type
    > as a return value of a public member. You would need make the Employee
    > type
    > public to get it to compile. Unfortunately that still doesn't help a
    > great
    > deal with ASP.
    >
    > ASP only has one data type, the variant (or two if you count an array of
    > variants as a type). Now whilst it is possible to re-arrange the VB code
    > to
    > pass a reference to User-Defined type into a variant and thus into
    > VBScript
    > you can't access the members of the type. The VBScript parsing doesn't
    > allow for the 'type.member' notation to work both for objects and for UDTs
    > as it does in VB6.
    >
    > If the ASP merely needs to hold reference to a type that is passed to
    > another component then that is possible as long as appropriate measures
    > are
    > taken to ensure the interfaces are compatible with Script in this way
    > (I.E.
    > takes a variant ByRef).
    >
    > If you need the ASP code to be able to access the members of the type then
    > you will need to use a class instead:-
    >
    > 'Class Employee
    > Public empname as string
    > Public address as string
    > Public salary as integer
    > Public deptno as integer
    >
    >
    > 'Your class
    >
    > Dim employees() as Variant ' Actuall contains employess objects
    >
    > Public Function getEmployees() As Variant
    > getEmployees = employees
    > End Public
    >
    > However that can get expensive. It may be better to turn your class into
    > a
    > form of a collection. E.g.,
    >
    > Dim employess() as Employee
    > Dim mlCount as Long
    >
    > Public Property Get Item(ByVal Index As Long) As Employee
    > Set Item = employees(Index)
    > End Property
    >
    > Public Property Get Count() as Long
    > Count = mlCount
    > End Property
    >
    >
    >
    >
     
    Raj, Mar 22, 2007
    #2
    1. Advertising

  3. "Raj" <> wrote in message
    news:...
    > I need a VB function to return array of collections like
    >
    > Private Type Employee
    > empname as string
    > address as string
    > salary as integer
    > deptno as integer
    > End Type
    >
    > dim employees() as Employee
    >
    > Public Function getEmployees() as Employee()
    > getEmployees=employees
    > End Function
    >
    > I will call that function in ASP.
    >
    > This was not working any other method is welcomed.
    >
    > Regards,
    >
    > Raj.
    >


    First off the code above doesn't compile. You can't expose a private type
    as a return value of a public member. You would need make the Employee type
    public to get it to compile. Unfortunately that still doesn't help a great
    deal with ASP.

    ASP only has one data type, the variant (or two if you count an array of
    variants as a type). Now whilst it is possible to re-arrange the VB code to
    pass a reference to User-Defined type into a variant and thus into VBScript
    you can't access the members of the type. The VBScript parsing doesn't
    allow for the 'type.member' notation to work both for objects and for UDTs
    as it does in VB6.

    If the ASP merely needs to hold reference to a type that is passed to
    another component then that is possible as long as appropriate measures are
    taken to ensure the interfaces are compatible with Script in this way (I.E.
    takes a variant ByRef).

    If you need the ASP code to be able to access the members of the type then
    you will need to use a class instead:-

    'Class Employee
    Public empname as string
    Public address as string
    Public salary as integer
    Public deptno as integer


    'Your class

    Dim employees() as Variant ' Actuall contains employess objects

    Public Function getEmployees() As Variant
    getEmployees = employees
    End Public

    However that can get expensive. It may be better to turn your class into a
    form of a collection. E.g.,

    Dim employess() as Employee
    Dim mlCount as Long

    Public Property Get Item(ByVal Index As Long) As Employee
    Set Item = employees(Index)
    End Property

    Public Property Get Count() as Long
    Count = mlCount
    End Property
     
    Anthony Jones, Mar 23, 2007
    #3
  4. "Raj" <> wrote in message
    news:%...
    > Thanx indeed for the valuable input, but I still have problems:
    >
    > Here's what I did:
    >
    > Have created a standard exe project with two classes:
    >
    > Employee:
    > Public empname As String
    > Public address As String
    > Public salary As Integer
    > Public deptno As Integer
    >
    > Company:
    > Dim employess() As Employee
    > Dim mlCount As Long
    >
    > Public Property Get Count() As Long
    > Count = mlCount
    > End Property
    >
    > I had a form with Command Button. On it's click event I wrote like:
    >
    > Private Sub Command1_Click()
    > Dim cmp1 As New Company
    >
    > cmp1.Item(1) = New Employee
    > cmp1.Item(1).deptno = "10"
    > cmp1.Item(1).empname = "Raj"
    >
    > cmp1.Item(2) = New Employee
    > cmp1.Item(2).deptno = "20"
    > cmp1.Item(2).empname = "Ram"
    >
    > Print cmp1.Count
    > End Sub
    >
    > Then I got a compile error at:
    > Public Property Get Item(ByVal Index As Long) As Employee
    > Set Item = employees(Index)
    > End Property
    >
    > Error Message: Sub or Function not defined.
    >
    > It seems I am missing something.
    >
    > Please let me know.
    >
    > Thank you very much.
    >
    > Regards,
    >
    > Raj.
    >


    Yeah you spelt employees wrong in the Dim. However here is a more complete
    solution:-

    Company:
    Private maoEmployees() As Employee
    Private mlCount As Long

    Public Property Get Count() As Long
    Count = mlCount
    End Property

    Public Function AddEmployee() as Employee

    If mlCount = UBound(maoEmployees) Then
    ReDim Preserve maoEmployess(mlCount * 2) As Employee
    End If

    mlCount = mlCount + 1

    Set AddEmployee = New Employess

    Set maoEmployees(mlCount) = AddEmployee

    End Function

    Public Function Item(ByVal Index as Long) As Employee
    Set Item = maoEmployees(Index)
    End Public

    Private Sub Class_Initialise()

    ReDim maoEmployees(16) As Employee

    End Sub

    Now you populate with:-

    Dim cmp1 As New Company

    With cmp1.AddEmployee
    .deptno = "10"
    .empname = "Raj"
    End With

    With cmp1.AddEmployee
    .deptno = "20"
    .empname = "Ram"
    End With

    My question now is why VB6?
    Why not just use VBScripts own Classes etc?
     
    Anthony Jones, Mar 23, 2007
    #4
  5. Raj

    Raj Guest

    I am in the making of some component

    I appreciate very much your help.
    "Anthony Jones" <> wrote in message
    news:...
    >
    > "Raj" <> wrote in message
    > news:%...
    >> Thanx indeed for the valuable input, but I still have problems:
    >>
    >> Here's what I did:
    >>
    >> Have created a standard exe project with two classes:
    >>
    >> Employee:
    >> Public empname As String
    >> Public address As String
    >> Public salary As Integer
    >> Public deptno As Integer
    >>
    >> Company:
    >> Dim employess() As Employee
    >> Dim mlCount As Long
    >>
    >> Public Property Get Count() As Long
    >> Count = mlCount
    >> End Property
    >>
    >> I had a form with Command Button. On it's click event I wrote like:
    >>
    >> Private Sub Command1_Click()
    >> Dim cmp1 As New Company
    >>
    >> cmp1.Item(1) = New Employee
    >> cmp1.Item(1).deptno = "10"
    >> cmp1.Item(1).empname = "Raj"
    >>
    >> cmp1.Item(2) = New Employee
    >> cmp1.Item(2).deptno = "20"
    >> cmp1.Item(2).empname = "Ram"
    >>
    >> Print cmp1.Count
    >> End Sub
    >>
    >> Then I got a compile error at:
    >> Public Property Get Item(ByVal Index As Long) As Employee
    >> Set Item = employees(Index)
    >> End Property
    >>
    >> Error Message: Sub or Function not defined.
    >>
    >> It seems I am missing something.
    >>
    >> Please let me know.
    >>
    >> Thank you very much.
    >>
    >> Regards,
    >>
    >> Raj.
    >>

    >
    > Yeah you spelt employees wrong in the Dim. However here is a more
    > complete
    > solution:-
    >
    > Company:
    > Private maoEmployees() As Employee
    > Private mlCount As Long
    >
    > Public Property Get Count() As Long
    > Count = mlCount
    > End Property
    >
    > Public Function AddEmployee() as Employee
    >
    > If mlCount = UBound(maoEmployees) Then
    > ReDim Preserve maoEmployess(mlCount * 2) As Employee
    > End If
    >
    > mlCount = mlCount + 1
    >
    > Set AddEmployee = New Employess
    >
    > Set maoEmployees(mlCount) = AddEmployee
    >
    > End Function
    >
    > Public Function Item(ByVal Index as Long) As Employee
    > Set Item = maoEmployees(Index)
    > End Public
    >
    > Private Sub Class_Initialise()
    >
    > ReDim maoEmployees(16) As Employee
    >
    > End Sub
    >
    > Now you populate with:-
    >
    > Dim cmp1 As New Company
    >
    > With cmp1.AddEmployee
    > .deptno = "10"
    > .empname = "Raj"
    > End With
    >
    > With cmp1.AddEmployee
    > .deptno = "20"
    > .empname = "Ram"
    > End With
    >
    > My question now is why VB6?
    > Why not just use VBScripts own Classes etc?
    >
    >
    >
    >
    >
    >
     
    Raj, Mar 23, 2007
    #5
  6. Raj

    Raj Guest

    With cmp1.AddEmployee
    .deptno = "20"
    .empname = "Ram"
    End With

    Putting ? cmp1.Count return Subscript out of range error at line:

    If mlCount = UBound(maoEmployees) Then


    "Anthony Jones" <> wrote in message
    news:...
    >
    > "Raj" <> wrote in message
    > news:%...
    >> Thanx indeed for the valuable input, but I still have problems:
    >>
    >> Here's what I did:
    >>
    >> Have created a standard exe project with two classes:
    >>
    >> Employee:
    >> Public empname As String
    >> Public address As String
    >> Public salary As Integer
    >> Public deptno As Integer
    >>
    >> Company:
    >> Dim employess() As Employee
    >> Dim mlCount As Long
    >>
    >> Public Property Get Count() As Long
    >> Count = mlCount
    >> End Property
    >>
    >> I had a form with Command Button. On it's click event I wrote like:
    >>
    >> Private Sub Command1_Click()
    >> Dim cmp1 As New Company
    >>
    >> cmp1.Item(1) = New Employee
    >> cmp1.Item(1).deptno = "10"
    >> cmp1.Item(1).empname = "Raj"
    >>
    >> cmp1.Item(2) = New Employee
    >> cmp1.Item(2).deptno = "20"
    >> cmp1.Item(2).empname = "Ram"
    >>
    >> Print cmp1.Count
    >> End Sub
    >>
    >> Then I got a compile error at:
    >> Public Property Get Item(ByVal Index As Long) As Employee
    >> Set Item = employees(Index)
    >> End Property
    >>
    >> Error Message: Sub or Function not defined.
    >>
    >> It seems I am missing something.
    >>
    >> Please let me know.
    >>
    >> Thank you very much.
    >>
    >> Regards,
    >>
    >> Raj.
    >>

    >
    > Yeah you spelt employees wrong in the Dim. However here is a more
    > complete
    > solution:-
    >
    > Company:
    > Private maoEmployees() As Employee
    > Private mlCount As Long
    >
    > Public Property Get Count() As Long
    > Count = mlCount
    > End Property
    >
    > Public Function AddEmployee() as Employee
    >
    > If mlCount = UBound(maoEmployees) Then
    > ReDim Preserve maoEmployess(mlCount * 2) As Employee
    > End If
    >
    > mlCount = mlCount + 1
    >
    > Set AddEmployee = New Employess
    >
    > Set maoEmployees(mlCount) = AddEmployee
    >
    > End Function
    >
    > Public Function Item(ByVal Index as Long) As Employee
    > Set Item = maoEmployees(Index)
    > End Public
    >
    > Private Sub Class_Initialise()
    >
    > ReDim maoEmployees(16) As Employee
    >
    > End Sub
    >
    > Now you populate with:-
    >
    > Dim cmp1 As New Company
    >
    > With cmp1.AddEmployee
    > .deptno = "10"
    > .empname = "Raj"
    > End With
    >
    > With cmp1.AddEmployee
    > .deptno = "20"
    > .empname = "Ram"
    > End With
    >
    > My question now is why VB6?
    > Why not just use VBScripts own Classes etc?
    >
    >
    >
    >
    >
    >
     
    Raj, Mar 23, 2007
    #6
  7. "Raj" <> wrote in message
    news:%...
    > With cmp1.AddEmployee
    > .deptno = "20"
    > .empname = "Ram"
    > End With
    >
    > Putting ? cmp1.Count return Subscript out of range error at line:
    >
    > If mlCount = UBound(maoEmployees) Then
    >


    You need to fix my spelling mistake; this :-

    ReDim Preserve maoEmployess(mlCount * 2) As Employee

    Should be this:-

    ReDim Preserve maoEmployees(mlCount * 2) As Employee

    You should also ensure that all your modules start with the line:-

    Option Explicit








    >
    > "Anthony Jones" <> wrote in message
    > news:...
    > >
    > > "Raj" <> wrote in message
    > > news:%...
    > >> Thanx indeed for the valuable input, but I still have problems:
    > >>
    > >> Here's what I did:
    > >>
    > >> Have created a standard exe project with two classes:
    > >>
    > >> Employee:
    > >> Public empname As String
    > >> Public address As String
    > >> Public salary As Integer
    > >> Public deptno As Integer
    > >>
    > >> Company:
    > >> Dim employess() As Employee
    > >> Dim mlCount As Long
    > >>
    > >> Public Property Get Count() As Long
    > >> Count = mlCount
    > >> End Property
    > >>
    > >> I had a form with Command Button. On it's click event I wrote like:
    > >>
    > >> Private Sub Command1_Click()
    > >> Dim cmp1 As New Company
    > >>
    > >> cmp1.Item(1) = New Employee
    > >> cmp1.Item(1).deptno = "10"
    > >> cmp1.Item(1).empname = "Raj"
    > >>
    > >> cmp1.Item(2) = New Employee
    > >> cmp1.Item(2).deptno = "20"
    > >> cmp1.Item(2).empname = "Ram"
    > >>
    > >> Print cmp1.Count
    > >> End Sub
    > >>
    > >> Then I got a compile error at:
    > >> Public Property Get Item(ByVal Index As Long) As Employee
    > >> Set Item = employees(Index)
    > >> End Property
    > >>
    > >> Error Message: Sub or Function not defined.
    > >>
    > >> It seems I am missing something.
    > >>
    > >> Please let me know.
    > >>
    > >> Thank you very much.
    > >>
    > >> Regards,
    > >>
    > >> Raj.
    > >>

    > >
    > > Yeah you spelt employees wrong in the Dim. However here is a more
    > > complete
    > > solution:-
    > >
    > > Company:
    > > Private maoEmployees() As Employee
    > > Private mlCount As Long
    > >
    > > Public Property Get Count() As Long
    > > Count = mlCount
    > > End Property
    > >
    > > Public Function AddEmployee() as Employee
    > >
    > > If mlCount = UBound(maoEmployees) Then
    > > ReDim Preserve maoEmployess(mlCount * 2) As Employee
    > > End If
    > >
    > > mlCount = mlCount + 1
    > >
    > > Set AddEmployee = New Employess
    > >
    > > Set maoEmployees(mlCount) = AddEmployee
    > >
    > > End Function
    > >
    > > Public Function Item(ByVal Index as Long) As Employee
    > > Set Item = maoEmployees(Index)
    > > End Public
    > >
    > > Private Sub Class_Initialise()
    > >
    > > ReDim maoEmployees(16) As Employee
    > >
    > > End Sub
    > >
    > > Now you populate with:-
    > >
    > > Dim cmp1 As New Company
    > >
    > > With cmp1.AddEmployee
    > > .deptno = "10"
    > > .empname = "Raj"
    > > End With
    > >
    > > With cmp1.AddEmployee
    > > .deptno = "20"
    > > .empname = "Ram"
    > > End With
    > >
    > > My question now is why VB6?
    > > Why not just use VBScripts own Classes etc?
    > >
    > >
    > >
    > >
    > >
    > >

    >
    >
     
    Anthony Jones, Mar 25, 2007
    #7
  8. Raj

    Raj Guest

    I did that already
    "Anthony Jones" <> wrote in message
    news:...
    >
    > "Raj" <> wrote in message
    > news:%...
    >> With cmp1.AddEmployee
    >> .deptno = "20"
    >> .empname = "Ram"
    >> End With
    >>
    >> Putting ? cmp1.Count return Subscript out of range error at line:
    >>
    >> If mlCount = UBound(maoEmployees) Then
    >>

    >
    > You need to fix my spelling mistake; this :-
    >
    > ReDim Preserve maoEmployess(mlCount * 2) As Employee
    >
    > Should be this:-
    >
    > ReDim Preserve maoEmployees(mlCount * 2) As Employee
    >
    > You should also ensure that all your modules start with the line:-
    >
    > Option Explicit
    >
    >
    >
    >
    >
    >
    >
    >
    >>
    >> "Anthony Jones" <> wrote in message
    >> news:...
    >> >
    >> > "Raj" <> wrote in message
    >> > news:%...
    >> >> Thanx indeed for the valuable input, but I still have problems:
    >> >>
    >> >> Here's what I did:
    >> >>
    >> >> Have created a standard exe project with two classes:
    >> >>
    >> >> Employee:
    >> >> Public empname As String
    >> >> Public address As String
    >> >> Public salary As Integer
    >> >> Public deptno As Integer
    >> >>
    >> >> Company:
    >> >> Dim employess() As Employee
    >> >> Dim mlCount As Long
    >> >>
    >> >> Public Property Get Count() As Long
    >> >> Count = mlCount
    >> >> End Property
    >> >>
    >> >> I had a form with Command Button. On it's click event I wrote like:
    >> >>
    >> >> Private Sub Command1_Click()
    >> >> Dim cmp1 As New Company
    >> >>
    >> >> cmp1.Item(1) = New Employee
    >> >> cmp1.Item(1).deptno = "10"
    >> >> cmp1.Item(1).empname = "Raj"
    >> >>
    >> >> cmp1.Item(2) = New Employee
    >> >> cmp1.Item(2).deptno = "20"
    >> >> cmp1.Item(2).empname = "Ram"
    >> >>
    >> >> Print cmp1.Count
    >> >> End Sub
    >> >>
    >> >> Then I got a compile error at:
    >> >> Public Property Get Item(ByVal Index As Long) As Employee
    >> >> Set Item = employees(Index)
    >> >> End Property
    >> >>
    >> >> Error Message: Sub or Function not defined.
    >> >>
    >> >> It seems I am missing something.
    >> >>
    >> >> Please let me know.
    >> >>
    >> >> Thank you very much.
    >> >>
    >> >> Regards,
    >> >>
    >> >> Raj.
    >> >>
    >> >
    >> > Yeah you spelt employees wrong in the Dim. However here is a more
    >> > complete
    >> > solution:-
    >> >
    >> > Company:
    >> > Private maoEmployees() As Employee
    >> > Private mlCount As Long
    >> >
    >> > Public Property Get Count() As Long
    >> > Count = mlCount
    >> > End Property
    >> >
    >> > Public Function AddEmployee() as Employee
    >> >
    >> > If mlCount = UBound(maoEmployees) Then
    >> > ReDim Preserve maoEmployess(mlCount * 2) As Employee
    >> > End If
    >> >
    >> > mlCount = mlCount + 1
    >> >
    >> > Set AddEmployee = New Employess
    >> >
    >> > Set maoEmployees(mlCount) = AddEmployee
    >> >
    >> > End Function
    >> >
    >> > Public Function Item(ByVal Index as Long) As Employee
    >> > Set Item = maoEmployees(Index)
    >> > End Public
    >> >
    >> > Private Sub Class_Initialise()
    >> >
    >> > ReDim maoEmployees(16) As Employee
    >> >
    >> > End Sub
    >> >
    >> > Now you populate with:-
    >> >
    >> > Dim cmp1 As New Company
    >> >
    >> > With cmp1.AddEmployee
    >> > .deptno = "10"
    >> > .empname = "Raj"
    >> > End With
    >> >
    >> > With cmp1.AddEmployee
    >> > .deptno = "20"
    >> > .empname = "Ram"
    >> > End With
    >> >
    >> > My question now is why VB6?
    >> > Why not just use VBScripts own Classes etc?
    >> >
    >> >
    >> >
    >> >
    >> >
    >> >

    >>
    >>

    >
    >
     
    Raj, Mar 27, 2007
    #8
  9. Raj

    Raj Guest

    It seems the Class_Initialize event of the Company class is never triggered.

    "Anthony Jones" <> wrote in message
    news:...
    >
    > "Raj" <> wrote in message
    > news:%...
    >> With cmp1.AddEmployee
    >> .deptno = "20"
    >> .empname = "Ram"
    >> End With
    >>
    >> Putting ? cmp1.Count return Subscript out of range error at line:
    >>
    >> If mlCount = UBound(maoEmployees) Then
    >>

    >
    > You need to fix my spelling mistake; this :-
    >
    > ReDim Preserve maoEmployess(mlCount * 2) As Employee
    >
    > Should be this:-
    >
    > ReDim Preserve maoEmployees(mlCount * 2) As Employee
    >
    > You should also ensure that all your modules start with the line:-
    >
    > Option Explicit
    >
    >
    >
    >
    >
    >
    >
    >
    >>
    >> "Anthony Jones" <> wrote in message
    >> news:...
    >> >
    >> > "Raj" <> wrote in message
    >> > news:%...
    >> >> Thanx indeed for the valuable input, but I still have problems:
    >> >>
    >> >> Here's what I did:
    >> >>
    >> >> Have created a standard exe project with two classes:
    >> >>
    >> >> Employee:
    >> >> Public empname As String
    >> >> Public address As String
    >> >> Public salary As Integer
    >> >> Public deptno As Integer
    >> >>
    >> >> Company:
    >> >> Dim employess() As Employee
    >> >> Dim mlCount As Long
    >> >>
    >> >> Public Property Get Count() As Long
    >> >> Count = mlCount
    >> >> End Property
    >> >>
    >> >> I had a form with Command Button. On it's click event I wrote like:
    >> >>
    >> >> Private Sub Command1_Click()
    >> >> Dim cmp1 As New Company
    >> >>
    >> >> cmp1.Item(1) = New Employee
    >> >> cmp1.Item(1).deptno = "10"
    >> >> cmp1.Item(1).empname = "Raj"
    >> >>
    >> >> cmp1.Item(2) = New Employee
    >> >> cmp1.Item(2).deptno = "20"
    >> >> cmp1.Item(2).empname = "Ram"
    >> >>
    >> >> Print cmp1.Count
    >> >> End Sub
    >> >>
    >> >> Then I got a compile error at:
    >> >> Public Property Get Item(ByVal Index As Long) As Employee
    >> >> Set Item = employees(Index)
    >> >> End Property
    >> >>
    >> >> Error Message: Sub or Function not defined.
    >> >>
    >> >> It seems I am missing something.
    >> >>
    >> >> Please let me know.
    >> >>
    >> >> Thank you very much.
    >> >>
    >> >> Regards,
    >> >>
    >> >> Raj.
    >> >>
    >> >
    >> > Yeah you spelt employees wrong in the Dim. However here is a more
    >> > complete
    >> > solution:-
    >> >
    >> > Company:
    >> > Private maoEmployees() As Employee
    >> > Private mlCount As Long
    >> >
    >> > Public Property Get Count() As Long
    >> > Count = mlCount
    >> > End Property
    >> >
    >> > Public Function AddEmployee() as Employee
    >> >
    >> > If mlCount = UBound(maoEmployees) Then
    >> > ReDim Preserve maoEmployess(mlCount * 2) As Employee
    >> > End If
    >> >
    >> > mlCount = mlCount + 1
    >> >
    >> > Set AddEmployee = New Employess
    >> >
    >> > Set maoEmployees(mlCount) = AddEmployee
    >> >
    >> > End Function
    >> >
    >> > Public Function Item(ByVal Index as Long) As Employee
    >> > Set Item = maoEmployees(Index)
    >> > End Public
    >> >
    >> > Private Sub Class_Initialise()
    >> >
    >> > ReDim maoEmployees(16) As Employee
    >> >
    >> > End Sub
    >> >
    >> > Now you populate with:-
    >> >
    >> > Dim cmp1 As New Company
    >> >
    >> > With cmp1.AddEmployee
    >> > .deptno = "10"
    >> > .empname = "Raj"
    >> > End With
    >> >
    >> > With cmp1.AddEmployee
    >> > .deptno = "20"
    >> > .empname = "Ram"
    >> > End With
    >> >
    >> > My question now is why VB6?
    >> > Why not just use VBScripts own Classes etc?
    >> >
    >> >
    >> >
    >> >
    >> >
    >> >

    >>
    >>

    >
    >
     
    Raj, Mar 27, 2007
    #9
  10. "Raj" <> wrote in message
    news:...
    > It seems the Class_Initialize event of the Company class is never

    triggered.
    >


    Another spelling error I used the english Class_Initialise.

    Your spelling above is correct is that what your code is using?
     
    Anthony Jones, Mar 27, 2007
    #10
  11. Raj

    Raj Guest

    That's working fantastically.

    Thanx indeed.

    "Anthony Jones" <> wrote in message
    news:...
    >
    > "Raj" <> wrote in message
    > news:...
    >> It seems the Class_Initialize event of the Company class is never

    > triggered.
    >>

    >
    > Another spelling error I used the english Class_Initialise.
    >
    > Your spelling above is correct is that what your code is using?
    >
    >
     
    Raj, Mar 28, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Seong-Kook Shin
    Replies:
    1
    Views:
    512
    Richard Bos
    Jun 18, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    869
    Keith Thompson
    Mar 6, 2005
  3. Replies:
    3
    Views:
    938
    Frederick Gotham
    Nov 30, 2006
  4. Replies:
    4
    Views:
    561
    James Kanze
    Jul 22, 2008
  5. Davy
    Replies:
    9
    Views:
    1,595
Loading...

Share This Page