CodeBlue
CodeBlue

Reputation: 15389

Do VB6 declarations work like Arrays?

I am new to VB. I am reading some VB6 code and I come across declaration statements like

  PQR_SSN(8) As Byte // this probably refers to social security number
  TR_DATA(7) As TransactionDetail

In another file, TransactionDetial is defined

  Public Type TransactionDetail
  A(0) As Byte  
  B(0) As Byte
  Comment(40) As Byte
  //... etc
  //...
  End Type

Does TR_DATA(7) mean that it is an "array" that can store 8 instances of TransactionDetail?

Also, Consider Comment(40). Can I access individual bytes of the comment like this -

  Comment(3) 

Also, suppose that I do not assign all 41 bytes to Comment. Then will the rest of the bytes contain garbage values?

Please help. Thanks.

Upvotes: 0

Views: 163

Answers (2)

BobRodes
BobRodes

Reputation: 6165

@Nick: yes, VB helpfully does exactly as you surmise.

@CodeBlue: your last question suggests that you may want to investigate dynamic arrays. If so, I would suggest that you investigate in particular the Redim and Preserve statements.

Upvotes: 0

Nick Shaw
Nick Shaw

Reputation: 2113

Yes, TR_DATA(7) is an array of 8 elements of type TransactionDetail.

Yes, the Comments array can be accessed through individual elements as you show.

Unassigned elements may contain garbage values - I wouldn't trust them - but I can't recall whether VB helpfully pre-initialises variables. I would expect it would, just to be helpful to users, and that it would initialise numeric variables to 0, fixed-length strings to all zeros, and objects to Empty.

Found this web link which gives some useful guidance on arrays in VB6.

Also just found this: VB6 Variable Scope; which says:

Unlike many other languages, VB does not allow you to initialize variables; this must be done with an executable statement. However, each variable does have a default initialization value. Numeric variable types are initialized to zero, Strings are initialized to "", Booleans are initialized to False, etc.

Upvotes: 1

Related Questions