2 Replies - 1847 Views - Last Post: 17 October 2012 - 05:57 PM Rate Topic: -----

#1 fsdfsdfsd1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 04-October 12

purpose of redim

Posted 08 October 2012 - 03:52 AM

im quite new to vb,i couldnt understand the concept of redim in vb can anybody explain.......

This post has been edited by Atli: 08 October 2012 - 04:17 AM
Reason for edit:: The [code] tags are for code, not normal text.

Is This A Good Question/Topic? 0
  • +

Replies To: purpose of redim

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: purpose of redim

Posted 08 October 2012 - 04:01 AM

Redim can be used for arrays. Here is an example:
Say I had an array of, patients, and I wanted to add another one to the list. You can simply use this:
Redim Preserve array(<bounds>)

Redim can also be used to clear an array. Just omit the preserve part and it will clear the array out.
It can only be used within a procedure, (sub or function) and not at class or module level. You can not change the dimensions of an array (ie if it is 2-dimensional it cannot be 3 dimensional or 1 dimensional again), it will just reuse those dimensions.
Further reading

Oops, I just gave you VB.Net information. Sorry about that. I'll keep it here though just in case it helps :|

This post has been edited by DimitriV: 08 October 2012 - 04:07 AM

Was This Post Helpful? 2
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: purpose of redim

Posted 17 October 2012 - 05:57 PM

This info also applies to VB6 for the most part. However, in VB6 you can change the number of dimensions as well as the bound.

Also, you can only use ReDim if you haven't specified the array size when you declare it (a "dynamic array"). Like this:
dim x(9) as integer 'static array
dim y() as integer 'dynamic array
ReDim x(12) 'This will cause an error
ReDim y(12) 'Works fine
ReDim Preserve y(Ubound(y)+1) 'Used very often; adds one to the existing array while keeping existing values

Note as Dimitri has said that ReDim always clears the existing array of all its values, unless you use the Preserve option.

This post has been edited by BobRodes: 17 October 2012 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1