• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

How to remove values from an array ?

I want to remove a value from the center of an array
The array holds the value of a listbox
If an item is removed from the listbox then the same value to be removed from the array
The array has a lot of values for calculations
Dim roof() As Variant
ReDim Preserve roof(0 To 50, 0 To 7)
How to do this?
0
isnoend2001
Asked:
isnoend2001
  • 8
  • 3
  • 3
  • +1
4 Solutions
 
Martin LissRetired ProgrammerCommented:
It won't be easy but post your project and I'll see what I can do.
0
 
isnoend2001Author Commented:
Thanks Martin Liss, but the project is too huge with tons of purchased ocx's
I was thinking I could empty the array then loop the listbox with the removed value
0
 
Martin LissRetired ProgrammerCommented:
Sure. You just solved it!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
isnoend2001Author Commented:
Where I am having a problem is the value in the listbox is removed by clicking remove item.then the array needs updating less the removed item
After giving more thought. I think I only want to reset a few dimintions and leave others. I wrote the code yrs ago when i was reading books titled beginning vb5 and asking questions like "you can't type into a label, then what do you use ? I am attaching an image that shows the values the array holdsarray valuesThe values in the listbox like 10 x 10 = 100 are parsed into columns 5 & 6 in the array the other columns hold Hips, valleys etc
Maybe a better way would be to create another array and copy the values there, leaving out the column with the new values and then back
0
 
aikimarkCommented:
@isnoend2001

Is this a VB6, VBA, or VB.Net project?
0
 
isnoend2001Author Commented:
vb6
I think this is the only place for vb6
0
 
aikimarkCommented:
From your description, it looks like you need to coordinate the removal of an item from both a listbox control and the associated data structure.  Is that an accurate assessment of the problem?

Listbox controls have a Remove method that can remove a single item.

If you are building the contents of your listbox with code, you might find it easier to simply clear the contents of the listbox and then rebuild it.  Your data structure should be able to logically delete/hide items, which would be skipped by the rebuilding routine.
0
 
aikimarkCommented:
If you need to physically remove data, you should create a routine that loops from the point of 'deletion' to the 'end' of the dimension/array, copying all the items to the lower position.

Such maintenance overhead can be avoided by using collection or dictionary objects.
0
 
Martin LissRetired ProgrammerCommented:
Either way would probably work but it's hard to tell without the project.
0
 
isnoend2001Author Commented:
Most of the program values are referenced from the array the project has about 50 forms and 50 modules and 30 classes many reference the array
0
 
isnoend2001Author Commented:
example:job cost
0
 
isnoend2001Author Commented:
Yes items are removed from the listbox by selecting an item and clicking the remove item button. if no item is selected it removes the last item. This is where i am having a problem eg; updating the array
0
 
GrahamSkanCommented:
You will have to shuffle the items down before resizing the array:
Sub RemoveItem()
Dim strArray() As String
Dim i As Integer
Dim u As Integer

strArray = Split("one,two,three,six,four,five", ",")
u = UBound(strArray)
For i = 3 To u - 1
    strArray(i) = strArray(i + 1)
Next i
ReDim Preserve strArray(u - 1)
For i = 0 To u - 1
Debug.Print strArray(i)
Next i
End Sub

Open in new window

0
 
isnoend2001Author Commented:
Thanks i see what needs to be done
0
 
isnoend2001Author Commented:
This seems to work:
roof(0 To 50, 0 To 7)
50 rows, 8 columns

Empty Column
Sub RemoveFromArray(Col As Integer)
Dim i As Integer
For i = 0 To UBound(roof)
roof(i, Col) = vbNullString
Next

Remove selected item from listbox
If ListBoxdetail.ListCount > 0 And ListBoxdetail.ListIndex <> -1 Then
 ListBoxdetail.RemoveItem (ListBoxdetail.ListIndex) 'Remove selected item
 End If

Repopulate array
For i = 0 To ListBoxdetail.ListCount - 1
roof(i, Col) = ListBoxdetail.list(i)
Next
This seems to work, will this cause any problems ?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now