Array saving question (lbound)

Hello!

I am saving an array to a file and then load it again.


     Open sPath For Binary As #iFile1

     If uLoad Then
         Get #iFile1, , uArray
     Else
         Put #iFile1, , uArray
     End If

My array looks like this:

Redim myArr(1 to 3) as long
myArr(1) = 100
myArr(2) = 500
myArr(3) = 10000

When I load it again, I say:

Redim MyNewArr(1 to 3) and call the above sub.

For some reason, MyNewArr now looks like this:

MyNewArr(0) = 100
MyNewArr(1) = 500
MyNewArr(2) = 3

When I try to access MyNewArr(3) an out of bounds error is thrown.

Can somebody please tell me how to save my array correctly so that it 
will start at 1 again, just like I declared it when redimming it?

Thank you!


0
Boris
9/11/2010 5:30:38 PM
vb.general.discussion 1016 articles. 0 followers. Follow

22 Replies
2088 Views

Similar Articles

[PageSpeed] 45

on 9/11/2010, Boris P. supposed :
> Hello!
>
> I am saving an array to a file and then load it again.
>
>
>      Open sPath For Binary As #iFile1
>
>      If uLoad Then
>          Get #iFile1, , uArray
>      Else
>          Put #iFile1, , uArray
>      End If
>
> My array looks like this:
>
> Redim myArr(1 to 3) as long
> myArr(1) = 100
> myArr(2) = 500
> myArr(3) = 10000
>
> When I load it again, I say:
>
> Redim MyNewArr(1 to 3) and call the above sub.
>
> For some reason, MyNewArr now looks like this:
>
> MyNewArr(0) = 100
> MyNewArr(1) = 500
> MyNewArr(2) = 3
>
> When I try to access MyNewArr(3) an out of bounds error is thrown.
>
> Can somebody please tell me how to save my array correctly so that it will 
> start at 1 again, just like I declared it when redimming it?
>
> Thank you!

By default arrays are zero based. So a 3 element array will always 
begin with 0 and end with 2. You have to declare Option Base to 1 if 
you want to go that way. However, I strongly recommend you modify your 
thinking to use the default zero base. (It'll make life much 
easier!<g>)

-- 
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


0
GS
9/11/2010 9:06:38 PM
GS wrote:
> on 9/11/2010, Boris P. supposed :
>> Hello!
>>
>> I am saving an array to a file and then load it again.
>>
>>
>>      Open sPath For Binary As #iFile1
>>
>>      If uLoad Then
>>          Get #iFile1, , uArray
>>      Else
>>          Put #iFile1, , uArray
>>      End If
>>
>> My array looks like this:
>>
>> Redim myArr(1 to 3) as long
>> myArr(1) = 100
>> myArr(2) = 500
>> myArr(3) = 10000
>>
>> When I load it again, I say:
>>
>> Redim MyNewArr(1 to 3) and call the above sub.
>>
>> For some reason, MyNewArr now looks like this:
>>
>> MyNewArr(0) = 100
>> MyNewArr(1) = 500
>> MyNewArr(2) = 3
>>
>> When I try to access MyNewArr(3) an out of bounds error is thrown.
>>
>> Can somebody please tell me how to save my array correctly so that it 
>> will start at 1 again, just like I declared it when redimming it?
>>
>> Thank you!
> 
> By default arrays are zero based. So a 3 element array will always begin 
> with 0 and end with 2. You have to declare Option Base to 1 if you want 
> to go that way. However, I strongly recommend you modify your thinking 
> to use the default zero base. (It'll make life much easier!<g>)

I'd say "it depends" on whether to use 0- or 1-based indexing, dependent 
upon the problem domain.

Note that ReDim() should _not_ be used except for redimensioning, not 
for initial declarations  -- see the Caution discussion in the language 
reference manual for ReDim.

In short, the above code snippet should start w/ a

Dim myArr() As Long
ReDim myArr(1 to 3)

There's a problem w/ the code and the symptoms described as well--I 
strongly suspect the code isn't as typed in (and can tell it isn't 
copied and pasted as the capitalization for keywords isn't consistent w/ 
the VB stylistic pattern).

The statement

Redim MyNewArr(1 to 3)

isn't complete, either, as the "As Long" clause is missing and the 
existence of the (1 To 3) if it were in the actual code would preclude 
the appearance of MyNewArr(0) as the upper and lower bound subscripts 
are explicitly given.

I suspect that what you _actually_ have is

ReDim MyNewArr(3)

instead which would behave as GS notes assuming there is no

Option Base 1

statement in effect.

Lesson to be learned -- when posting code that is to demonstrate a 
symptom, post the actual code and the symptom, not a paraphrasing of it.

--
0
dpb
9/11/2010 9:29:57 PM

"GS" <gesansom@netscape.net> wrote in message 
news:i6gr11$u8s$1@news.eternal-september.org...
> on 9/11/2010, Boris P. supposed :

>
> By default arrays are zero based. So a 3 element array will always begin 
> with 0 and end with 2. You have to declare Option Base to 1 if you want to 
> go that way. However, I strongly recommend you modify your thinking to use 
> the default zero base. (It'll make life much easier!<g>)


No, that's incorrect. If you do NOT specify a lower bound, only then the 
default is 0.  Otherwise, the lower bound is whatever you specify in your 
Dim/ReDim. Option Base 1 just means that if you DON'T specify the lower 
bound, it will default to 1 instead of 0.

I also can't agree with your recommendation. In some cases, sure, the array 
should be 0-based.  For example, if syncing it to something that is 0-based 
(a listbox, for example). Otherwise, I personally view it as "programmer's 
preference".

-- 
Mike
 

0
MikeD
9/11/2010 10:31:29 PM
MikeD submitted this idea :
>
> "GS" <gesansom@netscape.net> wrote in message 
> news:i6gr11$u8s$1@news.eternal-september.org...
>> on 9/11/2010, Boris P. supposed :
>
>>
>> By default arrays are zero based. So a 3 element array will always begin 
>> with 0 and end with 2. You have to declare Option Base to 1 if you want to 
>> go that way. However, I strongly recommend you modify your thinking to use 
>> the default zero base. (It'll make life much easier!<g>)
>
>
> No, that's incorrect. If you do NOT specify a lower bound, only then the 
> default is 0.

That was implied in the context of the OP's problem! Dumping a file 
into an array with 'Get' will result in a zero-based array UNLESS 
Option Base 1 is declared. That's all I was responding to.

>  Otherwise, the lower bound is whatever you specify in your 
> Dim/ReDim. Option Base 1 just means that if you DON'T specify the lower 
> bound, it will default to 1 instead of 0.

No disagreement here.

>
> I also can't agree with your recommendation. In some cases, sure, the array 
> should be 0-based.  For example, if syncing it to something that is 0-based 
> (a listbox, for example). Otherwise, I personally view it as "programmer's 
> preference".

And as a programmer's preference, it was my recommendation to not use 
it 'generally speaking'! Again, no disagreement that it does have a 
place if one chooses to accept the resposibility of managing it in 
those cases. Personally, I've never found any practical reasons to 
suffice using Option Base 1.<g>

Not to raise contention.., but just to clarify!

-- 
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


0
GS
9/12/2010 1:34:05 AM
GS wrote:
....

> That was implied in the context of the OP's problem! Dumping a file into 
> an array with 'Get' will result in a zero-based array UNLESS Option Base 
> 1 is declared. That's all I was responding to.
....

The Get doesn't really have anything to do with...unless the variable 
has been declared as a variable, it's a scalar and only a single value 
will be returned on the Get (ignoring the details of descriptors for 
dynamic strings/arrays or UDTs).

The dimensions of the array are based on the Dim or ReDim used and the 
Option Base in effect at the time.

The problem w/ OP's posting is that his posted code doesn't reflect the 
actual code--it's missing the definition of the calling sequence in the 
subroutine snippet and the location with respect to that of where he 
looked at the data and how.  The array name he complains of isn't the 
same as the array name in the routine so it isn't that that caused the 
problem.

There's also the problem noted above of whether he's reading/writing the 
same type of array -- if it was dynamic on Put, it needs to be dynamic 
matching on Get or he's going to get the descriptor information 
confounded w/ the data.  Conversely, if Put a fixed-sized array, only 
the data (no descriptors) will be written and trying to read it into a 
dynamic array will confound the data w/ descriptors the other direction.

The upshot still is, OP needs to post the actual code to be able to see 
precisely what caused the specific symptom(s) but he's got problems of 
inconsistency and poor practice at a minimum...

--
0
dpb
9/12/2010 1:52:17 AM
dpb wrote:
....

> The Get doesn't really have anything to do with...unless the variable 
> has been declared as a variable, ...

That of course was intended to read "declared as an array, ..."

--
0
dpb
9/12/2010 1:54:20 AM
Well stated.., and I agree.

Sorry I wasn't giving focus past the use of 'Get' and 'Put' in the 
concept of the posted code so much as how the array[s] were dimmed 
going to and coming back from the file.

As far as I'm concerned, my experience bears out that both you and Mike 
D have made valid points that I fully agree with. Personally, I've 
never mixed static/dynamic arrays in this usage knowing that Put/Get 
require everything be the same both ways or there's going to be 
problems.

Just seemed the LBound/UBound issue was related to the Option Base used 
(or not used)<g>

-- 
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


0
GS
9/12/2010 3:27:21 AM
Yes, you're right, thanks! I did make the mistake of typing (0 to 3) 
instead of (1 to 3).
I am glad I mistyped it and you noticed that it was not copied!

The reason why I use 1-based arrays is that else I can never say if an 
array is actually empty!

Let's take a real-life sample:

I have a public var name colMyCustomers() which is made of udts

Public Type udtCustomer
	FirstName as string
	LastName as string
	(etc...)
End Type

public sub GetCustomers()

	Redim colCustomers(0)

	dim r as dhrichclient.crecordset
	set r = cn.openrecordset("SELECT * FROM customers")

	redim colCustomers(1 to r.recordcount)
	dim l&
	for l = 1 to r.recordcount
		colCustomers(l).firstname = r!customer_firstname
		colCustomers(l).lastname = r!customer_lastname
		(etc...)
  	r.movenext
	next l

end sub

The later in the code when I access the array, I can simply say

For l = 1 to ubound(colcustomer)
	etc...
Next l

When the array is still empty, it will simply skip it.
What would I do if the array was 0 based?

I would have to store in another variable if the array is really empty 
or has 1 customer (=colCustomer(0)).

And if I had not redimmed the array to 0 (Redim colCustomers(0)), any 
attemps to do something like this

For l = 1 to ubound(colcustomer)
	etc...
Next l

would throw an error because the array is not dimmed.

Anything wrong with my method?



dpb wrote:
> GS wrote:
>> on 9/11/2010, Boris P. supposed :
>>> Hello!
>>>
>>> I am saving an array to a file and then load it again.
>>>
>>>
>>> Open sPath For Binary As #iFile1
>>>
>>> If uLoad Then
>>> Get #iFile1, , uArray
>>> Else
>>> Put #iFile1, , uArray
>>> End If
>>>
>>> My array looks like this:
>>>
>>> Redim myArr(1 to 3) as long
>>> myArr(1) = 100
>>> myArr(2) = 500
>>> myArr(3) = 10000
>>>
>>> When I load it again, I say:
>>>
>>> Redim MyNewArr(1 to 3) and call the above sub.
>>>
>>> For some reason, MyNewArr now looks like this:
>>>
>>> MyNewArr(0) = 100
>>> MyNewArr(1) = 500
>>> MyNewArr(2) = 3
>>>
>>> When I try to access MyNewArr(3) an out of bounds error is thrown.
>>>
>>> Can somebody please tell me how to save my array correctly so that it
>>> will start at 1 again, just like I declared it when redimming it?
>>>
>>> Thank you!
>>
>> By default arrays are zero based. So a 3 element array will always
>> begin with 0 and end with 2. You have to declare Option Base to 1 if
>> you want to go that way. However, I strongly recommend you modify your
>> thinking to use the default zero base. (It'll make life much easier!<g>)
>
> I'd say "it depends" on whether to use 0- or 1-based indexing, dependent
> upon the problem domain.
>
> Note that ReDim() should _not_ be used except for redimensioning, not
> for initial declarations -- see the Caution discussion in the language
> reference manual for ReDim.
>
> In short, the above code snippet should start w/ a
>
> Dim myArr() As Long
> ReDim myArr(1 to 3)
>
> There's a problem w/ the code and the symptoms described as well--I
> strongly suspect the code isn't as typed in (and can tell it isn't
> copied and pasted as the capitalization for keywords isn't consistent w/
> the VB stylistic pattern).
>
> The statement
>
> Redim MyNewArr(1 to 3)
>
> isn't complete, either, as the "As Long" clause is missing and the
> existence of the (1 To 3) if it were in the actual code would preclude
> the appearance of MyNewArr(0) as the upper and lower bound subscripts
> are explicitly given.
>
> I suspect that what you _actually_ have is
>
> ReDim MyNewArr(3)
>
> instead which would behave as GS notes assuming there is no
>
> Option Base 1
>
> statement in effect.
>
> Lesson to be learned -- when posting code that is to demonstrate a
> symptom, post the actual code and the symptom, not a paraphrasing of it.
>
> --

0
Boris
9/12/2010 7:13:05 AM
dpb wrote:
> GS wrote:
> ...
>
>> That was implied in the context of the OP's problem! Dumping a file
>> into an array with 'Get' will result in a zero-based array UNLESS
>> Option Base 1 is declared. That's all I was responding to.
> ...
>
> The Get doesn't really have anything to do with...unless the variable
> has been declared as a variable, it's a scalar and only a single value
> will be returned on the Get (ignoring the details of descriptors for
> dynamic strings/arrays or UDTs).
>
> The dimensions of the array are based on the Dim or ReDim used and the
> Option Base in effect at the time.
>
> The problem w/ OP's posting is that his posted code doesn't reflect the
> actual code--it's missing the definition of the calling sequence in the
> subroutine snippet and the location with respect to that of where he
> looked at the data and how. The array name he complains of isn't the
> same as the array name in the routine so it isn't that that caused the
> problem.
>
> There's also the problem noted above of whether he's reading/writing the
> same type of array -- if it was dynamic on Put, it needs to be dynamic
> matching on Get or he's going to get the descriptor information
> confounded w/ the data. Conversely, if Put a fixed-sized array, only the
> data (no descriptors) will be written and trying to read it into a
> dynamic array will confound the data w/ descriptors the other direction.
>
> The upshot still is, OP needs to post the actual code to be able to see
> precisely what caused the specific symptom(s) but he's got problems of
> inconsistency and poor practice at a minimum...
>
> --


My code is huge and I cannot post it without leaving out things, and to 
make it clear, I rewrote it on the fly.

But my actual problem was that when I declared/ dimmed/ redimmed it, I chose

Redim (1 to ...)
and saved it this way.

But before loading it again from the file on the disk, I said "Redim (0 
to ...)"

This caused the array to start at 0 instead of 1.

Learnt something...

Normally, I only use DBs to write my data, not binary files, that's why 
your statement of "lack of consistency and practice" is right in this case.
0
Boris
9/12/2010 7:48:44 AM
But the actual problem remains:

How would one know if an array is empty when it's zero based???

I don't understand people who use 0-based arrays because they have to 
use another variable to check if the array is really empty.

With 0-based I mean arrays that have data in the index 0 position.
I do use 0-based arrays as well, but it doesn't contain data at that 
position.

I only use it to check if the array is "empty" or not.
0
Boris
9/12/2010 7:52:18 AM
Perhaps everybody will understand my way of coding with this sample.
I think it's really beautiful code:

Private Sub Form_Load()

     Dim s() As String
     ReDim s(0)

     Debug.Print "We have " & UBound(s) & " items"
     Dim l&
     For l = 1 To UBound(s)
         'if array is redimmed to 0, it will simply skip here
         Debug.Print "Item value " & l & " is " & s(l)
     Next l

     ReDim s(1)
     Debug.Print "We have " & UBound(s) & " items"
     For l = 1 To UBound(s)
         Debug.Print "Item value " & l & " is " & s(l)
     Next l

End Sub
0
Boris
9/12/2010 8:08:17 AM
"dpb" <none@non.net> wrote in message 
news:i6hbvp$ovo$1@news.eternal-september.org...

> There's also the problem noted above of whether
> he's reading/writing the same type of array -- if it
> was dynamic on Put, it needs to be dynamic matching on Get or he's going 
> to get the descriptor
> information confounded w/ the data.  Conversely,
> if Put a fixed-sized array, only the data (no descriptors)
> will be written and trying to read it into a dynamic
> array will confound the data w/ descriptors . . .

Actually that is not true (unless the array happens to be a member of a UDT, 
which in the OP's case it does not appear to be). When writing an ordinary 
array (one that is not a member of a UDT) then Put writes /exactly/ the same 
data to the file regardless of whether that array is dynamic or fixed size.

For example, if the array is an array of Longs containing three elements 
then exactly 12 bytes will be written to the file, regardless of whether the 
array is dynamic or fixed size, and for a three element array of Doubles the 
data written would be 24 bytes, again regardless of whether it was a dynamic 
or fixed size array. Similarly, the data written to file for each element of 
a three element array of Variants would be a two byte value representing the 
Data Type of the element followed by however many bytes the contents of that 
specific element requires (which of course depends on its data type). But 
even then, exactly the same data would be written to the file regardless of 
whether the array was dynamic or fixed size.

Mike



0
Mike
9/12/2010 10:03:34 AM
"Boris P." <bpnotvalid@nospamhotmail.com> wrote in message 
news:%23GNuk8kULHA.4872@TK2MSFTNGP02.phx.gbl...

> My code is huge and I cannot post it without leaving
> out things, and to make it clear, I rewrote it on the fly.
> But my actual problem was that when I declared/
> dimmed/ redimmed it, I chose Redim (1 to ...) and
> saved it this way. But before loading it again from the
> file on the disk, I said "Redim (0 to ...)"

Well that is not what you said in your original post. In fact you said 
something quite different, and it is NOT because you failed to post your 
full project code (which is perfectly understandable) but it is in fact 
because you ADDED some code that was not actually there! If you post the 
wrong code, and especially if (as you did) you tell us you used code that 
you did not actually use, then you should expect the wrong answer! In any 
case, what you have just said still does NOT explain why after saving the 
following data . . .

           myArr(1) = 100
           myArr(2) = 500
           myArr(3) = 10000

.. . . you got back the following data . . .

           MyNewArr(0) containing the value 100
           MyNewArr(1) containing the value 500
           MyNewArr(2) containing the value 3

Where the hell did the "3" come from! What you did was to post code that you 
were not actually using, and also post results that you were not actually 
getting!

> My code is huge and I cannot post it without leaving
> out things, and to make it clear, I rewrote it on the fly.

Yes. We understand that you cannot post everything, but you really should do 
your best to post all of the code and declarations that are appropriate to 
the specific small part of the project you are having problems with, and you 
very definitely should write the code you are about to post into an actual 
VB code window, and you should check the results that the code you have just 
typed actually returns in a running VB project, so that you do not end up 
posting code that you did not actually use, or missing out important parts, 
and also so that you do not end up posting results that did not actually 
happen!

One other example example in this particular case is that we had no idea 
whether you had properly initially declared your dynamic arrays rather than 
using Redim on the fly to do it, and in view of the line Redim myArr(1 to 3) 
as Long which was contained in your posted code we quite reasonably assumed 
that you had not properly initially declared them (since it is not usual to 
specify the data type again in a Redim statement). In your case you 
specified the data type in the Redim statement for one array but you did not 
specify the data type in your Redim statement for the other array, an 
inconsistency which made it appear that you might possibly be using Redim on 
the fly rather than initially properly declaring your dynamic arrays, and an 
inconsistency which led us to make other quite reasonable assumptions about 
your code which were probably not correct, for example the assumption that 
MyNewArr might actually be an array of Variants. Now I know that if it was 
an array of Variants then you would have had other problems in your code 
that you did not appear to be having if your posted results of the Get were 
correct, but the fact that one of the values returned in your posted results 
was the number 3, which was clearly not what you were actually getting, 
caused us to treat those posted results with suspicion anyway.

If you want to get the best answers then you should post your question in 
the best way you can, otherwise you will end up having everyone else 
guessing what you have actually done and unnecessarily running around in 
circles on your behalf.

Essentially, if you want people to do some work answering your question (as 
the people here do) then you should be prepared to do at least a similar 
amount of work asking it.

Mike



0
Mike
9/12/2010 11:12:01 AM
Boris P. wrote:
> But the actual problem remains:
>
> How would one know if an array is empty when it's zero based???
>
> I don't understand people who use 0-based arrays because they have
> to use another variable to check if the array is really empty.

Or they use error trapping (errors are information, not failures). Or
they probe the SAFEARRAY structure -- as does our Stamina library,
which has a function (DxArrayDims) that returns the number of
dimensions in an array. It returns 0 if the array is currently
undimmed.

There's nothing wrong what you're doing, but some (like me) find it a
bit odd.

-- 
     Jim Mack
     MicroDexterity Inc     www.microdexterity.com

0
Jim
9/12/2010 11:58:35 AM
"Boris P." <bpnotvalid@nospamhotmail.com> wrote in message 
news:eqmkeHlULHA.4576@TK2MSFTNGP04.phx.gbl...

> Perhaps everybody will understand my way of coding
> with this sample. I think it's really beautiful code:
>     Dim s() As String
>     ReDim s(0) Debug.Print "We have " & UBound(s) & " items"
>     Dim l&
>     For l = 1 To UBound(s)
>         'if array is redimmed to 0, it will simply skip here
>         Debug.Print "Item value " & l & " is " & s(l)
>     Next l

Well, each to his own I suppose. The contents of a file only really make 
sense to code that knows the format of the data, and since it is your own 
data and your own file then that's okay. It's just a bit unusual. By the 
way, your code above will fail with a Subscript Out Of Range error if you 
happen to paste it into a module that is using Option Base 1.

Mike


0
Mike
9/12/2010 1:27:28 PM
 From a certain project size on and with lots of tweaks like subclassing 
and altering tables, VB6 crashes at simple errors, that's why I try to 
avoid errors like hell.

Jim Mack:
> Boris P. wrote:
>> But the actual problem remains:
>>
>> How would one know if an array is empty when it's zero based???
>>
>> I don't understand people who use 0-based arrays because they have
>> to use another variable to check if the array is really empty.
>
> Or they use error trapping (errors are information, not failures). Or
> they probe the SAFEARRAY structure -- as does our Stamina library,
> which has a function (DxArrayDims) that returns the number of
> dimensions in an array. It returns 0 if the array is currently
> undimmed.
>
> There's nothing wrong what you're doing, but some (like me) find it a
> bit odd.
>

0
Boris
9/12/2010 1:41:15 PM
  Mike Williams:
> "Boris P." <bpnotvalid@nospamhotmail.com> wrote in message
> news:%23GNuk8kULHA.4872@TK2MSFTNGP02.phx.gbl...
>
>> My code is huge and I cannot post it without leaving
>> out things, and to make it clear, I rewrote it on the fly.
>> But my actual problem was that when I declared/
>> dimmed/ redimmed it, I chose Redim (1 to ...) and
>> saved it this way. But before loading it again from the
>> file on the disk, I said "Redim (0 to ...)"
>
> Well that is not what you said in your original post. In fact you said
> something quite different, and it is NOT because you failed to post your
> full project code (which is perfectly understandable) but it is in fact
> because you ADDED some code that was not actually there! If you post the
> wrong code, and especially if (as you did) you tell us you used code
> that you did not actually use, then you should expect the wrong answer!
> In any case, what you have just said still does NOT explain why after
> saving the following data . . .
>
> myArr(1) = 100
> myArr(2) = 500
> myArr(3) = 10000
>
> . . . you got back the following data . . .
>
> MyNewArr(0) containing the value 100
> MyNewArr(1) containing the value 500
> MyNewArr(2) containing the value 3
>
> Where the hell did the "3" come from! What you did was to post code that
> you were not actually using, and also post results that you were not
> actually getting!
>
>> My code is huge and I cannot post it without leaving
>> out things, and to make it clear, I rewrote it on the fly.
>
> Yes. We understand that you cannot post everything, but you really
> should do your best to post all of the code and declarations that are
> appropriate to the specific small part of the project you are having
> problems with, and you very definitely should write the code you are
> about to post into an actual VB code window, and you should check the
> results that the code you have just typed actually returns in a running
> VB project, so that you do not end up posting code that you did not
> actually use, or missing out important parts, and also so that you do
> not end up posting results that did not actually happen!
>
> One other example example in this particular case is that we had no idea
> whether you had properly initially declared your dynamic arrays rather
> than using Redim on the fly to do it, and in view of the line Redim
> myArr(1 to 3) as Long which was contained in your posted code we quite
> reasonably assumed that you had not properly initially declared them
> (since it is not usual to specify the data type again in a Redim
> statement). In your case you specified the data type in the Redim
> statement for one array but you did not specify the data type in your
> Redim statement for the other array, an inconsistency which made it
> appear that you might possibly be using Redim on the fly rather than
> initially properly declaring your dynamic arrays, and an inconsistency
> which led us to make other quite reasonable assumptions about your code
> which were probably not correct, for example the assumption that
> MyNewArr might actually be an array of Variants. Now I know that if it
> was an array of Variants then you would have had other problems in your
> code that you did not appear to be having if your posted results of the
> Get were correct, but the fact that one of the values returned in your
> posted results was the number 3, which was clearly not what you were
> actually getting, caused us to treat those posted results with suspicion
> anyway.
>
> If you want to get the best answers then you should post your question
> in the best way you can, otherwise you will end up having everyone else
> guessing what you have actually done and unnecessarily running around in
> circles on your behalf.
>
> Essentially, if you want people to do some work answering your question
> (as the people here do) then you should be prepared to do at least a
> similar amount of work asking it.
>
> Mike
>
>
>

Sorry, I become only assured of what I'm doing after all the responses here.
Over the years I did it without thinking about it, so I was sure I 
posted all info that was needed. Of course with your questions and tipps 
I found out that it's not as I thought it was.

0
Boris
9/12/2010 1:43:29 PM
Mike Williams wrote:
....

> Actually that is not true (unless the array happens to be a member of a 
> UDT, which in the OP's case it does not appear to be). When writing an 
> ordinary array (one that is not a member of a UDT) then Put writes 
> /exactly/ the same data to the file regardless of whether that array is 
> dynamic or fixed size.
....

For Binary mode that's true, indeed.  I should have noted the Open was 
included in OP's posting, sorry.

--

0
dpb
9/12/2010 1:49:26 PM
"Boris P." <bpnotvalid@nospamhotmail.com> wrote in message 
news:eqmkeHlULHA.4576@TK2MSFTNGP04.phx.gbl...
> Perhaps everybody will understand my way of coding with this sample.
> I think it's really beautiful code:
>
> Private Sub Form_Load()
>
>     Dim s() As String
>     ReDim s(0)
>
>     Debug.Print "We have " & UBound(s) & " items"
>     Dim l&
>     For l = 1 To UBound(s)
>         'if array is redimmed to 0, it will simply skip here
>         Debug.Print "Item value " & l & " is " & s(l)
>     Next l
>
>     ReDim s(1)
>     Debug.Print "We have " & UBound(s) & " items"
>     For l = 1 To UBound(s)
>         Debug.Print "Item value " & l & " is " & s(l)
>     Next l
>
> End Sub

See IsDimmed() here as an alternative:

http://groups.google.com/group/microsoft.public.vb.enterprise/msg/6925f09068af63a5

It uses error trapping. Here is ArrayDimmed() that doesn't use error 
trapping:

http://groups.google.com/group/microsoft.public.vb.general.discussion/msg/8d0862e83b1a27cc


0
Nobody
9/12/2010 2:05:10 PM
"Boris P." <bpnotvalid@nospamhotmail.com> wrote

> From a certain project size on and with lots of tweaks like subclassing
> and altering tables, VB6 crashes at simple errors, that's why I try to
> avoid errors like hell.

Altering V-Tables?

That's a bit funny.  You hack into Windows messages, and maybe
muck about with VB's jump tables....

.... but try to handle data in a file and you find VB isn't doing what
you want!

Priceless!
LFS


0
Larry
9/12/2010 6:50:40 PM
dpb wrote:
> Mike Williams wrote:
> ...
> 
>> Actually that is not true (unless the array happens to be a member of 
>> a UDT, which in the OP's case it does not appear to be). When writing 
>> an ordinary array (one that is not a member of a UDT) then Put writes 
>> /exactly/ the same data to the file regardless of whether that array 
>> is dynamic or fixed size.
> ...
> 
> For Binary mode that's true, indeed.  I should have noted the Open was 
> included in OP's posting, sorry.

Actually, was really responding more to the GS posting that seemed to 
indicate that somehow the Get operation was dynamically assigning memory 
and somehow affecting what the array indices were going to be and then 
got off into thinking how anything could affect the OPs symptoms and 
that led to the thinking of Random, not Binary access and from there the 
rest followed... :)

--
0
dpb
9/12/2010 7:05:24 PM
"Boris P." <bpnotvalid@nospamhotmail.com> skrev i meddelandet 
news:eqmkeHlULHA.4576@TK2MSFTNGP04.phx.gbl...
> Perhaps everybody will understand my way of coding with this sample.
> I think it's really beautiful code:
>
> Private Sub Form_Load()
>
>     Dim s() As String
>     ReDim s(0)
>
>     Debug.Print "We have " & UBound(s) & " items"
>     Dim l&
>     For l = 1 To UBound(s)
>         'if array is redimmed to 0, it will simply skip here
>         Debug.Print "Item value " & l & " is " & s(l)
>     Next l
>
>     ReDim s(1)
>     Debug.Print "We have " & UBound(s) & " items"
>     For l = 1 To UBound(s)
>         Debug.Print "Item value " & l & " is " & s(l)
>     Next l
>
> End Sub

What is wrong about Erase, except you have to deal with an error?

/Henning


0
Henning
9/12/2010 7:15:50 PM
Reply:

Similar Artilces:

saving existing files
Just got publisher 2003 and everytime I try to save changes to an existing file the file field reads publisher2 meaning I have to type in the existing file name again. Any suggestions. Thanks Troy troy@campingpros.com wrote: > Just got publisher 2003 and everytime I try to save > changes to an existing file the file field reads > publisher2 meaning I have to type in the existing file > name again. Any suggestions. Thanks Are you saving from a template wizard? -- Brian Kvalheim Microsoft Office Publisher MVP Official Publisher MVP Site: http://www.kvalheim.org This ...

Another question concerning powershell and exchange 2003
i am really strugling with this process. I see examples on the net for doing this but they all do a lot more than I want so there is a ton of code to sift through. Basicly I will query AD for a user account. I want to be able to create the mailbox for that account. Do you have a simple example of what i need to do to get access to CDOEXM and what parameters it is wanting. I assume that is where my trouble is. ...

Question about Socket.Receive
I setup a socket to receive an 8k byte array on it's own thread in my C# application. I found that the Receive method did not read all 8k and I had to loop and read in chunks in order to receive all 8k. Why doesn't the Receive method block until all data is read? Is there another method that will "ReadFully" or is there a buffer I need to set? Thanks. Jon Berry wrote: > I setup a socket to receive an 8k byte array on it's own thread in my C# > application. > > I found that the Receive method did not read all 8k and I had to l...

Array of labels??
Hi i am trying to create an array of labels in excel userforms is thi possible in excel vba? i know it is possible in vb. e.g like the user input 5 in an input box i will horizotally create labels on the forms with the number 1 to 5 on each of the labels. thanks alo -- Message posted from http://www.ExcelForum.com upz --- Message posted from http://www.ExcelForum.com/ Hi Jaxrpc, Arrays of controls are not supported, although there are ways around it. If you have 5 labels called Label1,Label2...Label5 then you could reference them with syntax like this, For intIndex = 1 to 5 ...

Saving in Excel #3
I have a problem that just started last week. When I modify a cell with new text I cannot save the file until I click off the cell. I used to be able to click save without clicking off the box. Can someone tell me how to fix this? The save icon is still enabled for me when I'm editting a cell. (I use xl2003.) You could try resetting the standard toolbar: tools|customize|toolbars tab select the Standard toolbar click reset be aware that any customized changes you've made to that toolbar will have to be recreated. ====== If that doesn't help, how about a test: Close excel wi...

Conditional Formatting Question #13
Hi all, I have a spread sheet with 9 columns and an infinite possibility of rows (however it currently has 107 rows). What I would like to do is have each cell in column b check the row above and below and if it finds the value in either of the corresponding rows are equal then both rows that have the same value in column b are highlighted the same background color. I have tried a couple of ways in conditional formatting but can't get the coloring to work. Any help is appreciated. T.I.A. Can you post a small sample to show us what you mean? Is your first row of data row 1? I...

VBA/Macro question
My spreadsheet contains the following data: Contract Name Distributor Name How can I loop through the cells and find Contract Name, unmerge the cell and have it left justify, then move to the next occourance. Contract Name will always be in cell 1 and be underlined and bolded. You could start by using the macro recorder to record yourself going through the steps of unmerging and left justifying a cell. Then use the code found here to generalize the code and loop through the column: http://support.microsoft.com/kb/141762/EN-US/ HTH, JP On Oct 15, 2:32=A0pm, Origi...

Need X to act like my "Close without Save" button.
Hello, I’m stuck and I can’t get loose.  I have a form that opens with the following “On Open” event: Private Sub Form_Open(Cancel As Integer) DoCmd.Maximize DoCmd.GoToRecord , "", acNewRec DoCmd.Requery "lboItems" End Sub If the user makes no changes “form not dirty” and clicks the X on the upper right corner, the form closes with no problem. If the user makes changes “form dirty” and clicks X, the form closes and the record is saved, this is not good. I’ve created two buttons on the form that work well. “Save Record” with the following ...

Very basic question..
How do I get the text I enter in edit box to a String variable? Thnx in advance > How do I get the text I enter in edit box to a String variable? > Something like this: CWnd* pEditControl= GetDlgItem(IDC_YOUREDITBOX); ASSERT_VALID(pEditControl); CString text; pEditControl->GetWindowText(text); // text should have the edit control's content. Another alternative is to use DDX mechanism and bind your CString to edit control. --- Ajay You should add a variable for the edit box using the wizard then you can call UpdateData(true) to update the variable from the window or Up...

formating formula question
I want to format a non-specific row if a condition is true. If the wor "final" is placed in a non-specific cell I want the entire row to b formated with a light green background and black alpha-numeric digits. Thank yo -- Message posted from http://www.ExcelForum.com Suppose you're evaluating rows 1-100 and "non-specific cell" means any cell in the row: 1. Select rows 1-100. 2. Go to Format > Conditional Formatting 3. Select "Formula Is" and insert: =OR(1:1="final") 4. Press the "Format" radio button and format as desired. ...

Field Concatenation Question
Good Afternoon, Would anyone know how to do the following? First please view my sample dataset.... Name Address Phone TransNumber Joe 64 Lane 123 45 Joe 64 Lane 123 92 Sam 24House 456 87 Sam 24House 456 21 I would like an output that performs a "group by" on the like data, and concatenates/formats the differing data from the same field....So the output would look like this.... Name Address Phone TransNumber Joe 64 Lane 123 45, 92 Sam 24House 456 ...

How do I concatenate Array with single cell?
Hi, I would like to be able to, in a single cell, sum a number of looked up values. One way, of course, would be to make a long formula where the only change between plus-signs is which cell to concatenate with cell E2 =lookup($A$2 & $E10 ;data; $H$25)+lookup($A$3 & $E10 ;data; $H$25)+... Instead I tried the following {=sum(lookup(A2:A7 & E10;...)} with the result that only the lookup based on the first cell of the range & E10 was returned and summed, not the A3 & E10 etc. Is it possible, if so how, to use some sort of array formula where you specify a range to be con...

CImage::Save()
Hi. VC7 MFC I am reading and writing .jpg files using CImage::Save() but it is degrading the quality of the image because I don't know how to turn off or minimize the built-in compression. Does anyone know how? TIA Richard C I am not familiar with CImage but it sounds like a wrapper around GDI+ Image class? If so, the GDI+ Image::Save method allows you to specify a compression level (third parameter, a EncoderParameter object). http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/usingGDIPlus/usingimageencodersanddecoders/settingjpegcompressionlevel.asp ...

save workbook with only values and no links
I have a lot of workbooks which I have to handover to our accountant. want to save them without the links to other workbooks. I there a way to save a workbook without having to copy everything a than paste it. As it are workbooks linked with the add-in of exact globe, the value won't appear when the connection to exact globe (accounting software is not possible. Thanks in advanc -- Message posted from http://www.ExcelForum.com Hi exceller, I don't know what version it first appeared in, but if you go to Edit | Links, you can click the Break Links button to break external links...

How do I Save business cards print settings in Publisher?
Our business cards stock has 10 business cards. Say we need to print Joe's business card; we open the BC in Publisher 2003, then set the borders sizes, and print joe' cards. Later on that day we open Publishr with Mary's BC. Since we restarted Publisher, the program has reset to the "default" printing settings for business cards. So the process of setting the border widths starts all over again. Any ideas on how to establish these settings as defaults? Save the card as a Publisher template. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps....

Save file as CSV
Sub createPlanID() Sheets("2004Data").Copy ActiveWorkbook.SaveAs Filename:= _ "C:\New\PlanId.csv" _ , FileFormat:=xlCSV, CreateBackup:=False End Sub The following VBA code creates the Plan ID file. However when I go to open the file it gives me error - SYLK: File format not valid. I want to take a sheet from one book and save it as a newbook as a Comma Separated file. Thanks. You might try recording the keystrokes to save the worksheet as a .csv file and see if that works. MG >-----Original Message----- >Sub createPlanID() > >Sheets(...

Question: move log files or run ntbackup??
Hi, I've got a (full) windows 2000 exchange server; the d-drive (where the exchange store is) is full, of log files. Would it be adviseable to move 37+GB of logfiles off temporarily, or take the mail store offline for 1+ hours to run an NTBACKUP locally to clean-up the mess?? Thanks You don't need to take server offline to backup Exchange. Run NTBackup and backup the information store - that'll flush the logs. -- Bharat Suneja MVP - Exchange www.zenprise.com NEW blog location: www.exchangepedia.com/blog ---------------------------------------------- "fugowey" <...

Financial Modeling (Excel) Question
I am taking an online finance class, and was wondering if someone can help me out or point me to an appropriate financial modeling forum. Question: There's some company. Given Ending Cash Balance for May. Also given Collections and Payments for June, July, August, Sept, Oct, Nov. a) How do I calculate the Ending Cash Balance for each month? I THOUGHT the formula is: Ending Cash Balance for May + Collection for June - Disbursement for June + Current Borrowing --------------------------------- = Ending Cash Balance for June This is the formula given in the book. Please corrent me if i...

loading and saving a transparent CImage
Could someone give me some direction on how to load/save a gif with transparency using CImage? Currently I'm loading them and then setting the transparency index using gif header, but can't get them to save correctly at all, even if I rewrite the background color in the header. Thanks, Scott Hello Scott, Thanks for your post. We will look into it and reply with more information here. Please post here if you have any more concerns. Regards, HuangTM Microsoft Online Partner Support MCSE/MCSD Get Secure! -- www.microsoft.com/security This posting is provided "as is"...

Visual Studio 2008 Question
I checked out Visual Studio 2008 on Amazon.com and noticed on the system requirements it said "DVD-ROM". My main computer has a DVD-RAM though I have the cartridge which the DVD disc goes into. The cartridge is able to be opened and the disk taken out or put in. Would anybody happen to know if I would be able to slip the DVD-ROM into the DVD-RAM cartridge and be able to read/install that way? Thanks If you can read a DVD, then I don't see how it could be a problem. -- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com "Dereck L. Dietz" <...

After editing example1.xls and click SAVE, the filename changes to AABBEE.xls #2
After editing example1.xls and click SAVE, the filename changes to AABBEE.xls. The original file still exist and has been updated but the 'funny' filename also have the same content, and editable. The example1.xls is stored in a server and accessed by many people within the company. Everyone accessing to the file will change the filename unknowingly after saving it. When excel saves the file, it saves it as a temporary file with a funny name (8 characters--no extension). If the save is successful, xl will delete the original and if that's successful, xl will rename the fu...

Having Pb openning/saving fax(Tif format) on Outlook 2003 since a Week plz help
Hi All, Since a few days I can't open or save reveived faxes (.tif), I didn't chage anything to my outlook config, before that I could receive fax read and save them wihout any Pb Thank's for your help In news:uezjdMqRHHA.3500@TK2MSFTNGP05.phx.gbl, bbnimda <wahhhran-vba@yahoo.fr> typed: > Hi All, > > Since a few days I can't open or save reveived faxes (.tif), I didn't > chage anything to my outlook config, > > before that I could receive fax read and save them wihout any Pb > > > Thank's for your help Hi - I suggest you post in ...

DAP query question
I have a bunch of data access pages set up. Working showing the data for a particular data. I have new plans and need to have a date range choosen by a user on the page itself, then that data to be shown for the entered range. so the query would be a select query summing with the date criteria being between [startdate] and [enddate] problem is how do i either create a calendar control or two drop downs on the page itself that the user can change to view the nessisary data. Im assuming a command button will also be needed. is there a way to pass the values from controls on a dap to the que...

opening ms access 2000 file question
I share MS Access 2000 over a windows 2003 server.If a MS Access db is opened up by another user the only way I can openit at the same time is by opening the MS acesss program then use the File > Open option.It will let me double click the db file in windows explorer on a network drive but it does nothing. Thanks!"Ricky" <rfidler2004@earthlink.net> wrote in message news:%23qYXCkKcHHA.3648@TK2MSFTNGP02.phx.gbl...>I share MS Access 2000 over a windows 2003 server.> If a MS Access db is opened up by another user the only way I can open> it at the same time is by openi...

Mapping question 02-02-05
Hi All! I want to add on account form new field that mapped to some user. Something exactly like "owner". I've looked through Development Manager and could find a way. Can I? If so need step by step assistance. Thanx for advance. you mean you want to add a field that allows you to choose a user? Lookup fields cannot be added to crm. -- John O'Donnell Microsoft CRM MVP http://www.mscrmfaq.us "Andrey Ptashinskiy" <ptash@mail.ru> wrote in message news:1107358906.599467.57490@f14g2000cwb.googlegroups.com... > Hi All! > I want to add on account form new...