Solved

# How to initialize user type array?

Posted on 2003-02-20
Medium Priority
483 Views
I have a user defined type array as
Type LunarDate
BaseWeekday As Integer      ' Weekday of Solar Jan.1 (1=Sunday, 2=Monday, ...
DaysDiff As Integer         ' Days from Solar Jan.1 to Lunar Jan.1
Intercalation As Integer    ' Month of intercalation, 0=No
MonthDays(13) As Integer    ' Days of Lunar month, 0=29, 1=30
TermDays(2, 12) As Integer  ' 24 terms
End Type

The data as below:
'{26, 5, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 5, 20, 4, 19, 6, 21, 5, 20, 6, 21, 6, 21, 7, 23, 8, 23, 8, 23, 8, 24, 8, 22, 7, 22},
'{31, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 6, 20, 4, 19, 6, 21, 5, 20, 6, 21, 6, 22, 7, 23, 8, 23, 8, 23, 9, 24, 8, 23, 7, 22},

How can I initialize this array?
0
Question by:mikezang
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 12

Assisted Solution

guidway earned 100 total points
ID: 7991945
Visual Basic doesn't provide any way to declare an array and initialize its elements at the same time. In most cases you end up with setting individual elements one by one, as in:

Dim strArray(0 To 3) As String
strArray(0) = "Spring"
strArray(1) = "Summer"
strArray(2) = "Fall"
strArray(3) = "Winter"

Under VB4, VB5, and VB6 you can create an array of Variants on the fly, using the Array() function:
Dim varArray() As Variant
varArray() = Array("Spring", "Summer", "Fall", "Winter")

but there is no similar function to create arrays of data types other than Variant. If you're using VB6, however, you can create String arrays using the Split() function:
Dim varArray() As String
' arrays returned by Split are always zero-based
varArray() = Split("Spring;Summer;Fall;Winter", ";")

Under VB6 you can also take advantage of the capability for a Function to return an array, and build your own array initialization routines, such as the following one:
Function ArrayInt(ParamArray values() As Variant) As Integer()
Dim i As Long
ReDim res(0 To UBound(values)) As Integer
For i = 0 To UBound(values)
res(i) = values(i)
Next
ArrayInt = res()
End Function

You could also build a routine that tests the type of values passed to it, and returns an array of the correct type. In this case, the function should be declared to return a Variant.

hope this helps
guidway
0

LVL 17

Expert Comment

ID: 7991996
You could do it in a function:

The data as below:
'{31, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 6, 20, 4, 19, 6, 21, 5, 20, 6, 21, 6, 22, 7, 23, 8, 23, 8, 23, 9, 24, 8, 23, 7, 22},

Dim uMyLunaDate as LunarDate

Dim sData As String

' ignore the first 2 characters
sData = Mid("'{26, 5, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 5, 20, 4, 19, 6, 21, 5, 20, 6, 21, 6, 21, 7, 23, 8, 23, 8, 23, 8, 24, 8, 22, 7, 22},", 3)

Function CreateNewElement(sInputDataString) As LunarDate

Dim uLD as LunaDate
Dim Values
' convert string to an array
Values = Split(sInputDataString, ",")

ULD.BaseWeekday = Values(0)
ULD.DaysDiff = Values(1)
ULD.Intercalation = Values(2)

For c = 0 to 12
ULD.MonthDays(C) = Values(3+c)
Next c

' not shore how your data fits into the array but use the above as an example.

' now return the UDT
CreateNewElement = uLD

End Function

0

LVL 5

Author Comment

ID: 7997618
I am using them in Excel VBA, may I use a text file to initialize array? How can I do?
0

LVL 17

Accepted Solution

inthedark earned 100 total points
ID: 7998049
Intersting application....I have done a tide calculator using simplefied harmonic method.  What type of app is it?

Back to the problem, in Excel you need to use a function like this:

Dim sMyData as String

Your file data is now in MyData.  Next you need to extract the values.  This can be done like this:

Dim Values ' this is a variant

' chop the line into an array using comma to denote end of each field
' the first 2 bytes are ignored
Values = Split(Mid(sMyData,3), ",") ' it is now a variant array

' Now convert into numeric values
For lC = 0 To Ubound(Values)
' to convert you can also use Val(), cDBL(), cLng()
Values(lC) = cSng(Values(lC))
Next

The rest of the code you already have.

Dim lfn
lfn = FreeFile ' get a free file lfn
Open FileName For Input Shared As #lfn 'open the file
ReadFile = Input(LOF(lfn), #lfn) ' read all bytes; lfn=file length
Close lfn ' close file

End Function
0

Expert Comment

ID: 8901337
mikezang:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
Experts: Post your closing recommendations!  Who deserves points here?
0

LVL 49

Expert Comment

ID: 8964018
Moderator, my recommended disposition is:

Split points between: guidway and inthedark

DanRollins -- EE database cleanup volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month14 days, 4 hours left to enroll

#### 801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.