Link to home
Start Free TrialLog in
Avatar of vbRetard
vbRetard

asked on

Reversin Values.

Hi again. Here is what i am trying to do. I did this successfull with one array of values but i can't do it with another one. I don't understand why i might be missing something simple. This example works.
Array R() already has values in it , what i do is i retrieve the values in the reverse order. It works
For i = UBound(r()) To LBound(r()) Step -1
    s(i) = i
    Debug.Print s(i)
Next

But when i try to do the same with another array it gives me an OverFlow error. Anyone knows how come ? Here is the snippet. I welcome suggestions Thanks alot.

Dim temp() As Integer
For j = UBound(AFull()) To LBound(AFull()) Step -1
    ReDim Preserve temp(j)
    temp(j) = AFull(j)
    j = j + 1
Next
Avatar of Erick37
Erick37
Flag of United States of America image

Overflow generally means you are trying to assign a value to a variable that exceeds the maximum allowable value for that type of variable.

temp() should be declared the same as AFull(); you declared it as Integer.  Is AFull declared as Integer as well?

Hi VBRetard,

I suppose you want to build up the temp() array with the values from AFull both inversed. The Redim Preserve should be increased by 1, in your example it is decreased as you start from UBound(AFull()).

What's even worse, in each
For j = .. to .. step -1
....
Next j

j is set to one integer lower, but you also state at the end of the For..Next loop, that (i=i+1) which annihilizes the next i call, and creates bizarre behaviour.

I would propose this code:

Dim temp() As Integer
For j = UBound(AFull()) To LBound(AFull()) Step -1
    ReDim Preserve temp(UBound(AFull()-j)
    temp(UBound(AFull()-j) = AFull(j)
Next

I don't use VB but VBA (Mostly Excel) and in there I would even use:

Dim temp() As Integer
For j = UBound(AFull) To LBound(AFull) Step -1
    ReDim Preserve temp(UBound(AFull)-j)
    temp(UBound(AFull)-j) = AFull(j)
Next


Good Luck

Calacuccia

Avatar of vbRetard
vbRetard

ASKER

Hi, Thanks for the input, I still get the error though, here is what i do so far. Both arrays are Declared like this
Dim Afull() as string
Dim Temp() as string

Afull() array has values as i said before. Now i tried to put this values into a temp array in reverse order. I get the error on this Line.

For j = UBound(AFull) To LBound(AFull)

What causes that ? I tried your solution Calacuccia it gave me an error too on that very same line. I am sure your solution is valid i just don't know why i get the error. Also can you please explain me line by line your code meaning ? I would appriciate that.
I didn't understood why you did this with an array =(AFull)-j) what does that do ? Thanks a lot.
   
Hi vbRetard,

First of two proposed code samples should have been:

Dim temp() As Integer
For j = UBound(AFull()) To LBound(AFull()) Step -1
    ReDim Preserve temp(UBound(AFull())-j)
    temp(UBound(AFull())-j) = AFull(j)
Next

Some explanation:

For j = UBound(AFull()) to LBound(AFull()) Step -1
Well, that's your part in fact, j will have values from the maximal index of your AFull() array, and go down in steps of 1 to the minimal index of AFull(). Dont forget the step -1 at the end of this line, it will not work without.

ReDim Preserve temp(UBound(AFull())-j)
this line redeclares the array as having UBound(AFull())-j records.
UBound(AFull()) will be for example 8 at the start of your application (providing the array has a maximal index of 8) and i will be 8 also. So the new dimension for temp will be 0, in the next step (after Next command) i will go down one step to 7, and as UBound(AFull()) still is 8, temp will have a new dimension of 1, so including index 0 and 1. And so on, until i = 0 and temp will have 8-0 = 8 records.

The last line, temp(UBound(AFull())-j) = AFull(j) is just to equal temp(0) to AFull(8) (in first step), then temp(1) to AFull(7) and so on..

Is this clear ?

Calacucccia
Avatar of Ark
Hi
First time you get overflow because of this:
j=j+1 - you arrange non-stop loop and when j became >32K (max integer value), you receive your error.
Next, you don't need redim your array every time - one time is enough
Here is all your code (pls, check brackets, I didn't check them)
Dim Temp() as string
Dim i as Long ' if your array.count >32K
Redim Temp (LBound(AFull()) to UBound(AFull()))
For i = LBound(AFull()) To UBound(AFull())  
    temp(UBound(AFull())+LBound(AFull())-i) = AFull(i)
Next
Cheers
Ark:
<laughing>.. vbretard has this posted in another question.. i and j are defined as BYTE... LOL. Additionally, he is under the false impression that the table is in reverse order.. (He is For Each-ing through a Controls collection and I guess he assumes that the controls are in some kinda order, which as you know they are not).

In this question.. as in the other.. what he is doing and why he feels the array is out of order.. is still very unclear. Watch yourself with this one.. vbRetard has a habit of taking comments and then DELETEing his questions.. (5 out of 9 questions asked.. sheesh).

Anyhow.. here is the link to his other question..

https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10318313 

Vbretard:
Please be more specific in what you are trying to do.. <smile>

 

j = j + 1 before the next statement :)
and as we know the next statement and step  -1 decrements it back

for eg

For i = 10 to 1 step -1
i = i + 1 'i will always remain 10
Next

this code will go in to infinite loop and will never come out. This causes the over flow
this code will go in to infinite loop and will never come out. This causes the over flow

Nonsense. It will just cause an infinite loop and it will never get out.

Nah...

Simply change
Dim temp() As Integer
For j = UBound(AFull()) To LBound(AFull()) Step -1
    ReDim Preserve temp(j)
    temp(j) = AFull(j)
    j = j + 1
Next

To
Dim temp() As Integer
Dim k As Integer
ReDim temp(LBound(AFull()) To UBound(AFull()))
k = LBound(AFull())
For j = UBound(AFull()) To LBound(AFull()) Step -1
    temp(j) = AFull(k)
    k = k + 1
Next



PS - take care... if your array AFull can be empty, you must cater for that as well:

Dim temp() As Integer
Dim k As Integer
On Error Resume Next
ReDim temp(LBound(AFull()) To UBound(AFull()))
If Err.Number = 9 Then
    'AFull is empty. Do something appropriate, e.g. exit sub
End If
On Error Goto 0 'or your previous error handler.
k = LBound(AFull())
For j = UBound(AFull()) To LBound(AFull()) Step -1
    temp(j) = AFull(k)
    k = k + 1
Next





Wsh2, why do you say that i delete my posts ? I have never done that. And please stop making fun of this, i understood your answer to my previouse question of how controls loading. I just didn't want to use Dictionary.
Calacucccia thanks for your explanation. I just want to know something. Why do you guys declare Temp() to be an Integer array ? when afull() holds strings. Shouldn't temp() be declared as a string ?
"Why do you guys declare Temp() to be an Integer array ?"

You started it ;-) I've just been copying your code from your question. 'pologies.

Apart from that, it should work though.
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PS - Erick37 actually brought this up in the very first response to this question:

"temp() should be declared the same as AFull(); you declared it as Integer.  Is AFull declared as Integer as well?"

vbretard:
Using a dictionary was but one of several approachs you could take. Maybe you can explain to all of us what you are trying to accomplish, because what I have seen so far makes little sense as to purpose, and we really do want to see you go down a productive road.. <smile>.

For everyone else:
To fully understand this question please take a look at vbRetards other question. In it you will find his variable declarations (his indexes i and j are byte fields.. the Empty and Full arrays are strings.. etc.. etc) and for some reason he is upset that a For Each iteration through the Controls Collection is not coming out in order. I suppose his intentions are good.. but I don't have a clue as to what he is trying to do. Anyhow.. here is the link.. check it out to get the full picture.. <smile>

https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10318313 
Oh.. one other thing.. vbRetard is merely trying to put the array in reverse order.. something he failed to mention in his question that would have saved everyone here a lot of time and effort.. <smile>.

-------------------------------------
Pinshah:
At least half a dozen other comments BEFORE yours said the same thing your comment said, and in greater detail!!!  And now you want to take credit for the answer?.. <tsk> <tsk>.. shame on you.
i m not interested in talking any credit or answer Mr Wsh2... I just read the question and due to lack of time i didnt read others comment...

I dont have any time to be on this site like you wasting my companies money....

 
i m not interested in talking any credit or answer Mr Wsh2... I just read the question and due to lack of time i didnt read others comment...

I dont have any time to be on this site like you wasting my companies money....

 
pinshah, fine. But now you do know, so would you mind withdrawing your answer? One can do that these days.
i m not interested in talking any credit or answer Mr Wsh2... I just read the question and due to lack of time i didnt read others comment...

I dont have any time to be on this site like you wasting my companies money....

 
ok i will do
Good point pinshah.. your company needs your poorly researched answers (I bet they pay you extra for that, huh?). You must be very busy correcting things there.. so I won't waste YOUR obviously valuable time.. as you have ours.. <smile>.
You are behaving in a manner that the fellow was satisfied with your answer. Make efforts to find a solution instead of picking on someone.

 
pinshah:

I have no quarrel with anyone getting the points.. unlike you earlier.. I just like to see that vbRetard gets the respect his/her question deserves.

Additionally, pinshah writes: "I dont have any time to be on this site like you wasting my companies money". A very professional comment by you. Er.. uh.. shouldn't you be working now?.. <lol>.

Nevertheless, despite these misgivings.. I want to say Thank you for changing your answer to a comment.. Afterall, it was the right thing to do.. and you did that too.. <smile>.


Kewl!
<happy pills all around>