Actuarial Outpost
 
Go Back   Actuarial Outpost > Actuarial Discussion Forum > Software & Technology
FlashChat Actuarial Discussion Preliminary Exams CAS/SOA Exams Cyberchat Around the World Suggestions

Meet Lindsey Nelson, Senior Recruiter at DW Simpson

Reply
 
Thread Tools Display Modes
  #1  
Old 06-23-2004, 04:44 AM
LLJ LLJ is offline
Member
 
Join Date: Jan 2004
Posts: 41
Default Array with variable as index?

I am trying to define an array as the following:

Dim MyArray(x to x+10) as integer

But it seems that VB does not allow me to do so.

I wonder if there is a way to circumvent this so that I can greatly simplify my code using the index number to do something? Otherwise it will be quite tedious.

Thanks
Reply With Quote
  #2  
Old 06-23-2004, 06:43 AM
E. Blackadder's Avatar
E. Blackadder E. Blackadder is offline
Member
 
Join Date: Sep 2001
Location: Not far from US 1.
Favorite beer: Beer?! Blech. But Dad likes Dortmunder Union.
Posts: 20,778
Blog Entries: 1
Default

ReDim will do the trick.
__________________
If once a man indulges himself in murder, very soon he comes to think little of robbing; and from robbing he comes next to drinking and Sabbath-breaking, and from that to incivility and procrastination. Once begun upon this downward path, you never know where you are to stop. Many a man has dated his ruin from some murder or other that perhaps he thought little of
at the time.
Reply With Quote
  #3  
Old 06-23-2004, 08:53 AM
Brad Gile's Avatar
Brad Gile Brad Gile is offline
Member
CAS SOA AAA
 
Join Date: Sep 2001
Studying for whatever I feel like
College: Alumnus of Brown and UW-Madison
Posts: 11,104
Default

Quote:
Originally Posted by E. Blackadder
ReDim will do the trick.
Yup. More specifically, start with

Dim MyArray() as Integer

Then after the value of x has been determined, put in the line

ReDim MyArray(x to x+10)

Brad
__________________
Brad Gile, FSA, MAAA
Affiliate Member of the CAS
Dedicated Retired Actuary

Spoiler:
Obama sucks and we all know it-TDA


Spoiler:

That's been the funniest subplot of this whole thing, the people on the left attacking this bill for not being even more of a steaming pile. - erosewater
Reply With Quote
  #4  
Old 06-23-2004, 09:40 AM
Bama Gambler's Avatar
Bama Gambler Bama Gambler is offline
James Washer / Notes Contributor
SOA
 
Join Date: Jan 2002
Location: B'ham, AL
Posts: 16,136
Default

LLJ,

I'm not sure why you want to delare an array from x to x+10. If you know you want an array with 11 elements simply declare the array as MyArray(1 to 11). The traditional purpose for ReDim is when you need an array with a variable number of elements.
__________________

Now offering online seminars and live seminars for the Spring 2013 exams.

Follow us on Twitter, Facebook, and LinkedIn.
Reply With Quote
  #5  
Old 06-23-2004, 11:04 AM
Chuck Chuck is offline
Member
SOA AAA
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,233
Default

Also, if VBA migrates to be consistent with .Net, you will lose the ability to Dim anything other than zero-based arrays. So if you think you'll ever need to convert your code, you're better off just using Dim MyArray(10).

Chuck
Reply With Quote
  #6  
Old 06-23-2004, 12:13 PM
Brad Gile's Avatar
Brad Gile Brad Gile is offline
Member
CAS SOA AAA
 
Join Date: Sep 2001
Studying for whatever I feel like
College: Alumnus of Brown and UW-Madison
Posts: 11,104
Default

Quote:
Originally Posted by Bama Gambler
LLJ,

I'm not sure why you want to delare an array from x to x+10. If you know you want an array with 11 elements simply declare the array as MyArray(1 to 11). The traditional purpose for ReDim is when you need an array with a variable number of elements.
If you truly want an array that has a variable number of elements at any given point in time, you want to create a Collection.

Brad
__________________
Brad Gile, FSA, MAAA
Affiliate Member of the CAS
Dedicated Retired Actuary

Spoiler:
Obama sucks and we all know it-TDA


Spoiler:

That's been the funniest subplot of this whole thing, the people on the left attacking this bill for not being even more of a steaming pile. - erosewater
Reply With Quote
  #7  
Old 06-23-2004, 12:29 PM
LLJ LLJ is offline
Member
 
Join Date: Jan 2004
Posts: 41
Default

Thanks all-

I cannot say too much about the details. I wanted to do this because I retrive the value of x and manipulate it a lot, if I make it array(1 to 10), I'll have to adjust the index to the X I want all the time.

BTW,it is already working! Thanks
Reply With Quote
  #8  
Old 07-26-2004, 03:16 PM
ACCtuary's Avatar
ACCtuary ACCtuary is offline
Member
SOA
 
Join Date: Oct 2001
Location: Montgomery County, PA (Phila area)
Studying for none
College: MIT
Favorite beer: Sam Adams
Posts: 6,496
Default

Quote:
Originally Posted by Bama Gambler
LLJ,

I'm not sure why you want to delare an array from x to x+10. If you know you want an array with 11 elements simply declare the array as MyArray(1 to 11). The traditional purpose for ReDim is when you need an array with a variable number of elements.
Because sometimes it is nice to do the minimal number of mental chores to write up a solution to a problem. The better the syntax lines up with how a programmer thinks, the easier the solution will be (for that programmer).

Let the compiler do the chores, and let people think!

Why does perl provide hashes when the programmer can easily implement them him/herself? Because if you want to write Phone('fred')=15, then by golly why not, and let the language do the hard work of writing the optimal code.
__________________
Sticks and Stones may break my bones, but Markov Chains excite me.
Reply With Quote
  #9  
Old 07-26-2004, 08:05 PM
Chevy Chef
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by Chuck
Also, if VBA migrates to be consistent with .Net, you will lose the ability to Dim anything other than zero-based arrays. So if you think you'll ever need to convert your code, you're better off just using Dim MyArray(10).

Chuck
The .NET Common Language Runtime does support non-zero based arrays. Method Array.CreateInstance is used to create them. Various .NET languages may not support non-zero based arrays in syntactically nice ways, though.
Reply With Quote
  #10  
Old 07-27-2004, 09:53 AM
Brad Gile's Avatar
Brad Gile Brad Gile is offline
Member
CAS SOA AAA
 
Join Date: Sep 2001
Studying for whatever I feel like
College: Alumnus of Brown and UW-Madison
Posts: 11,104
Default

Quote:
Originally Posted by Chevy Chef
Quote:
Originally Posted by Chuck
Also, if VBA migrates to be consistent with .Net, you will lose the ability to Dim anything other than zero-based arrays. So if you think you'll ever need to convert your code, you're better off just using Dim MyArray(10).

Chuck
The .NET Common Language Runtime does support non-zero based arrays. Method Array.CreateInstance is used to create them. Various .NET languages may not support non-zero based arrays in syntactically nice ways, though.
Indeed. IIRC, support for non-zero based arrays was added during the beta period when some VB developers howled that code with non-zero based arrays would be broken. This from Jeffrey Richter at about the time when Visual Studio.NET was released in 2002:
http://msdn.microsoft.com/msdnmag/issues/02/02/NET/

Quote:
Originally Posted by Jeffrey Richter
Common Language Specification (CLS) compliance requires that all arrays be zero-based. This allows a method written in C# to create an array and pass the array's reference to code written in another language such as Visual Basic®. In addition, since zero-based arrays are by far the most common, Microsoft has spent a lot of time optimizing their performance. However, the CLR does support non-zero-based arrays but they are discouraged. For those of you who do not care about performance and cross-language portability, I will demonstrate how to create and use non-zero-based arrays later in this section.
Brad
__________________
Brad Gile, FSA, MAAA
Affiliate Member of the CAS
Dedicated Retired Actuary

Spoiler:
Obama sucks and we all know it-TDA


Spoiler:

That's been the funniest subplot of this whole thing, the people on the left attacking this bill for not being even more of a steaming pile. - erosewater
Reply With Quote
Reply

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 On
HTML code is Off


All times are GMT -4. The time now is 05:48 AM.


Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
*PLEASE NOTE: Posts are not checked for accuracy, and do not
represent the views of the Actuarial Outpost or its sponsors.
Page generated in 0.35496 seconds with 7 queries