Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to initialize user type array?

Posted on 2003-02-20
6
Medium Priority
?
485 Views
Last Modified: 2008-02-01
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
Comment
Question by:mikezang
6 Comments
 
LVL 12

Assisted Solution

by:guidway
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

by:inthedark
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)

uMyLunaDate = CreateNewElement(sData)


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

by:mikezang
ID: 7997618
I am using them in Excel VBA, may I use a text file to initialize array? How can I do?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Accepted Solution

by:
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

sMyData = ReadFile("c:\testfolder\testdata.txt")

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.




Function ReadFile(FileName)

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

by:CleanupPing
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

by:DanRollins
ID: 8964018
Moderator, my recommended disposition is:

    Split points between: guidway and inthedark

DanRollins -- EE database cleanup volunteer
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

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

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

Join & Ask a Question