Results 1 to 5 of 5

Thread: object Oriented Programming Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Afghanistan
    Posts
    9

    object Oriented Programming Problem

    Hi friends.

    Recently i am working on a program which requires me to do some conversion from Java to Visual Basic. The area where i am facing problem is the object oriented programming part of visual basic. the problem is here:

    I have a class named IntegerArray. It has the following methods:

    1. Class_Int(size as long) 'Initialization
    2 AddItem(item as Variant) 'Adding an item to array
    3. SetItem(index as Long, item as Variant) ' Setting an item
    4. LastItem() 'gives the last item
    5. FirstItem() 'gives the first item
    6. noItem() 'returns the number of items

    The idea of IntegerArray class is like the Vector class in Java. It is actually an array to hold any type of data types and increase its size as the array is filled to its upper bound.

    The problem arrises here:

    Dim MainArray as IntegerArray
    MainArray.Class_Int 10 'initializing an array of size 10

    'Here i declare another array of type IntegerArray
    Dim Array1 as IntegerArray
    Dim Array2 as IntegerArray
    Array1.Class_Int(10)
    Array2.Class_Int(10)

    'Here is where i get error when i want to store Array1 and Array2 in MainArray.

    MainArray.AddItem Array1
    MainArray.AddItem Array2

    Here it gives me error and says that i cannot store Array1 and Array2 in the MainArray...

    Hope you understood my problem. Please help me as i need to rectify this bug soon... mail me at [email protected] if you need the source code for the class.

    Waiting for your responses...

    Thanks...

    Farshid

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I know nothing about Java but...
    what is the code for the Additem method?

    I think you would have to loop thru the incoming array and add each member of that array to the existing array, inside the the Additem code.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Maybe something like this although objects are a bit funky:
    VB Code:
    1. Public Sub AddItem(vItem As Variant)
    2.     'Adding an item to array
    3.     If IsArray(vItem) = True Then
    4.         'add array
    5.         Dim newMax As Integer
    6.         Dim newMin As Integer
    7.         Dim n As Integer
    8.        
    9.         newMax = UBound(vItem)
    10.         newMin = LBound(vItem)
    11.         For n = newMin To newMax
    12.             ReDim Preserve m_vArray(UBound(m_vArray) + 1)
    13.             If IsObject(vItem) = True Then
    14.                 Set m_vArray(UBound(m_vArray)) = vItem
    15.             Else
    16.                 m_vArray(UBound(m_vArray)) = vItem
    17.             End If
    18.         Next n
    19.     Else
    20.         'add single
    21.         ReDim Preserve m_vArray(UBound(m_vArray) + 1)
    22.         If IsObject(vItem) = True Then
    23.             Set m_vArray(UBound(m_vArray)) = vItem
    24.         Else
    25.             m_vArray(UBound(m_vArray)) = vItem
    26.         End If
    27.     End If
    28. End Sub

    I'll also attach the project. I am not sure this is what you meant but its a try.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Afghanistan
    Posts
    9

    here is my code

    Here is my code for IntegerArray class


    Option Explicit
    Private data() As Variant
    Private no_items As Long
    Private size_increment As Long

    Public Sub Init_Class(initial_size As Long)
    ReDim Preserve data(0 To initial_size - 1) As Variant
    no_items = 0
    size_increment = initial_size
    End Sub

    Public Function noElement() As Long
    noElement = no_items
    End Function

    Public Function firstElement() As Variant
    If IsObject(data()) Then
    Set firstElement = data(0)
    Else
    firstElement = data(0)
    End If
    End Function

    Public Function lastElement() As Variant
    If IsObject(data()) Then
    Set lastElement = data(no_items - 1)
    Else
    lastElement = data(no_items - 1)
    End If
    End Function

    Public Function elementAt(ByVal Index As Long) As Variant
    If IsObject(data()) Then
    Set elementAt = data(Index)
    Else
    elementAt = data(Index)
    End If
    End Function

    Public Function indexOf(ByVal Item As Variant) As Long
    Dim i As Long
    While i < no_items And elementAt(i) <> Item
    i = i + 1
    Wend
    If i >= no_items Then
    indexOf = -1
    Else
    indexOf = i
    End If
    End Function

    Public Sub setElement(ByVal i As Long, ByVal Item As Variant)
    If i < no_items And elementAt(i) <> Item Then
    If IsObject(data()) Then
    Set data(i) = Item
    Else
    data(i) = Item
    End If
    End If
    End Sub

    Public Sub addElement(Item As Variant)
    If no_items = UBound(data()) Then
    Dim new_size As Long
    new_size = no_items + size_increment
    ReDim Preserve data(0 To new_size - 1) As Variant
    End If
    no_items = no_items + 1
    If IsObject(data()) Then
    Set data(no_items - 1) = Item
    Else
    data(no_items - 1) = Item
    End If
    End Sub



    Please try to create a data type of IntegerArray and try to save another IntegerArray data type in the first one. Does it give you any error???

    waiting for ur reply...



    p/s: the IntegerArray.cls is attached here...

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Afghanistan
    Posts
    9

    Solved!!

    First of all thanks everybody here for helping me... i think i figured out what was wrong with my program.

    the add method which is as follows:

    Public Sub addElement(Item As Variant)
    If no_items = UBound(data()) Then
    Dim new_size As Long
    new_size = no_items + size_increment
    ReDim Preserve data(0 To new_size - 1) As Variant
    End If
    no_items = no_items + 1
    If IsObject(data()) Then ///Bug here
    Set data(no_items - 1) = Item
    Else
    data(no_items - 1) = Item
    End If
    End Sub

    the mistake i was doing was that instead of checking whether <Item> variable is an object or not i was checking data() array.. that is why when data(no_items -1) ran, it gave me error as Item was an object and i had to use SET to store that in data() array..

    thanks anyway for the help.. and also try to change the code <If IsObject(data()) then> to <If IsObject(Item) then>.. i am sure it will work as it works with me..

    --farshid

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width