Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Array size trouble


Reply
 
Thread Tools Display Modes
  #1  
Old 08-26-2002, 02:11 PM
WeslyWest WeslyWest is offline
Freshman
 
Join Date: Aug 2002
Location: London
Posts: 42
Default Array size trouble


How do i create an array that has a different number of values everytime it is used. I have a variable called TotalNumber that defines the length of the array but cannot put it in the dim statement, any help, thanks.
Reply With Quote
  #2  
Old 08-26-2002, 02:18 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

Start by declaring a dynamic array with...
Dim myarray() As Long 'or whatever type you need

Then when you are ready to size it, use...
ReDim myarray(TotalNumber) 'might want to use TotalNumber - 1
__________________
Posting Guidelines
Reply With Quote
  #3  
Old 08-26-2002, 02:18 PM
Ronq's Avatar
Ronq Ronq is offline
Centurion
 
Join Date: May 2002
Location: Argentina
Posts: 168
Default

To do that you have to use redim, if you want to get it bigger or smaller but don't loose the data in the array you have to use redim preserve. For example like this:

Code:
Dim Array() as Long
Dim TotalNumber as Long

TotalNumber = 10

Redim Array(Totalnumber)

Redim Preserve Array(Totalnumber +3)

You may also want to look in the help for:
Ubound
Lbound

Bye.
Ronq.
Reply With Quote
  #4  
Old 08-26-2002, 02:19 PM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

use ReDim

You need to Dim there:

Dim YourArray() as Long

Then whe you need change the array size do this:

ReDim YourArray(NewSize)

if you don't need erase the content of the array use Preserve:

Redim Preserve YourArray(NewSize)

Preserve only use to change the first dim of the array, this mean, you can't Redim Preserve this:

Redim Preserve (12,NewValue) as long

but you can Redim Preserve this:

Redim Preserve (NewSize,12) as Long



I hope help !!
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
Reply With Quote
  #5  
Old 08-26-2002, 02:37 PM
NateBrei's Avatar
NateBrei NateBrei is offline
Contributor
 
Join Date: Jul 2002
Location: Omaha, NE
Posts: 571
Default

Quote:
Preserve only use to change the first dim of the array, this mean, you can't Redim Preserve this:

Redim Preserve (12,NewValue) as long

but you can Redim Preserve this:

Redim Preserve (NewSize,12) as Long
I thought it was the other way around. I thought you could only ReDim the LAST dimension.

Nate
Reply With Quote
  #6  
Old 08-26-2002, 03:07 PM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

You are correct Nate. Only the last one.
__________________
Posting Guidelines
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->