Click to See Complete Forum and Search --> : Arrays
srinika
September 28th, 2001, 04:10 AM
There is an array having dimensioned to 100 elements
Data is filled up to the 4th element
Eg.
Dim s(100) as string
s(0) = "aa"
s(1) = "bb"
s(2) = "cc"
s(3) = "dd"
Is there a direct method to find the number of data filled elements (in this case, 4) without using a loop to find the element number having the "" element.
Also I don't want to use Collections.
Srinika
Cimperiali
September 28th, 2001, 04:20 AM
The only way i can think of is:
While writing data to array, store in a variable the index of last element written.
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
Clearcode
September 28th, 2001, 05:48 AM
First a little aside:
By default, Visual basic arrays are zero based, which means that:
Dim s(100) as string
actually has 101 elements.
To explicitly set the lower bound use:
Dim s(1 to 100) as string
OK - so to find the first empty element...
Dim lArrayBound as Long
for lArrayBound = LBound(s) to UBound(s)
If s(lArrayBound) = "" then
Exit for
End If
next lArrayBound
'next free element is held in lArrayBound now...
HTH,
D.
-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
srinika
September 28th, 2001, 05:52 AM
In my Question I told that --No Loops--
So that way will not work out
Clearcode
September 28th, 2001, 05:58 AM
In that case you are going to have to increase/decrease the size of your array as needed.
Dim s(1 to 1) as string
private Sub AddElement(sNewElement as string)
If s(1) = "" then
s(1) = sNewElement
else
Redim Preserve s(1 to (UBound(s)+1)) as string
s(UBound(s)) = sNewElement
End If
End Sub
HTH,
D.
-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
Cimperiali
September 28th, 2001, 09:05 AM
This is how, normally. But for some strange way I do think the one who quested has its 101 array dimensioned and do not want it to be less...
;-)
Stay with us, you Guru.
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.