Solved

# Reversin Values.

Posted on 2000-03-27
Medium Priority
224 Views
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
0
Question by:vbRetard
• 7
• 6
• 5
• +4

LVL 32

Expert Comment

ID: 2662655
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?

0

LVL 17

Expert Comment

ID: 2662660
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

0

Author Comment

ID: 2662688
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.

0

LVL 17

Expert Comment

ID: 2662734
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
0

LVL 28

Expert Comment

ID: 2662974
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
0

LVL 14

Expert Comment

ID: 2663037
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..

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

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

0

Expert Comment

ID: 2663128
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
0

LVL 10

Expert Comment

ID: 2663778
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

0

LVL 10

Expert Comment

ID: 2663788
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

0

Author Comment

ID: 2663796
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 ?
0

LVL 10

Expert Comment

ID: 2663834
"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.
0

LVL 10

Accepted Solution

caraf_g earned 40 total points
ID: 2663844
But... that might actually explain your error!

If your array AFull is a string array, it could contain the following values:

"1"
"23"
"5"
"56"
"987299"

The last value would cause an overflow as VB would try to cast it to an integer (because Temp() is declared as an array of integers) and fail.
0

LVL 10

Expert Comment

ID: 2663851
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?"

0

LVL 14

Expert Comment

ID: 2663901
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>

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

LVL 14

Expert Comment

ID: 2663931
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.
0

Expert Comment

ID: 2663997
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....

0

Expert Comment

ID: 2664010
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....

0

LVL 10

Expert Comment

ID: 2664041
pinshah, fine. But now you do know, so would you mind withdrawing your answer? One can do that these days.
0

Expert Comment

ID: 2664072
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....

0

Expert Comment

ID: 2664081
ok i will do
0

LVL 14

Expert Comment

ID: 2664101
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>.
0

Expert Comment

ID: 2664236
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.

0

LVL 14

Expert Comment

ID: 2664422
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>.

0

LVL 10

Expert Comment

ID: 2675924
Kewl!
<happy pills all around>
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.