|
-
Aug 16th, 2001, 10:42 PM
#1
Thread Starter
New Member
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
-
Aug 16th, 2001, 10:52 PM
#2
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.
-
Aug 17th, 2001, 12:01 AM
#3
Maybe something like this although objects are a bit funky:
VB Code:
Public Sub AddItem(vItem As Variant)
'Adding an item to array
If IsArray(vItem) = True Then
'add array
Dim newMax As Integer
Dim newMin As Integer
Dim n As Integer
newMax = UBound(vItem)
newMin = LBound(vItem)
For n = newMin To newMax
ReDim Preserve m_vArray(UBound(m_vArray) + 1)
If IsObject(vItem) = True Then
Set m_vArray(UBound(m_vArray)) = vItem
Else
m_vArray(UBound(m_vArray)) = vItem
End If
Next n
Else
'add single
ReDim Preserve m_vArray(UBound(m_vArray) + 1)
If IsObject(vItem) = True Then
Set m_vArray(UBound(m_vArray)) = vItem
Else
m_vArray(UBound(m_vArray)) = vItem
End If
End If
End Sub
I'll also attach the project. I am not sure this is what you meant but its a try.
-
Aug 17th, 2001, 11:56 AM
#4
Thread Starter
New Member
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...
-
Aug 19th, 2001, 08:16 PM
#5
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|