Question Different sized arrays?!

xceL

New member
Joined
Apr 24, 2009
Messages
4
Programming Experience
Beginner
I'm making a program which is going to read from a text file which contains this data required to draw a flag when the name of the country is selected from a list box.
Indonesia;horizontal,red,60,0
Ireland;vertical,green,80,0;vertical,orange,80,160
Benin;horizontal,yellow,60,0;horizontal,red,60,60;vertical,green,80,0
The flags will be drawn with DrawRectangle[].
Can anyone shed some light on the logic of how I would go abouts doing it?

This is what I have so far:

VB.NET:
    Structure flagStruct
        Dim flagName As String
        Dim flagColour As String
        Dim flagDirection As String           
        Dim flagHorizontal As Integer
        Dim flagVertical As Integer
    End Structure

    Dim Flags(MaxFlags) As flagStruct

    Private Sub readFlag()
        Dim oneFlag As String
        Dim FlagFields() As String
        Dim sr As IO.StreamReader
        sr = IO.File.OpenText("flags.txt")
        numFlags = 0
        oneFlag = sr.ReadLine
        Do While oneFlag <> Nothing
            FlagFields = oneFlag.Split(";")
            Flags(numFlags).flagName = FlagFields(0)
            Do While oneFlag <> Nothing
                FlagFields = oneFlag.Split(",")
                Flags(numFlags).flagDirection = FlagFields(1)
                Flags(numFlags).flagHorizontal = FlagFields(2)
                Flags(numFlags).flagVertical = FlagFields(3)
                Loop

             numFlags = numFlags + 1
             oneFlag = sr.ReadLine
            Loop
            sr.Close()

It's obviously incomplete. I can't work out how I would go abouts writing this array. I can get get the first set of information (color etc..) but how would I go abouts getting the next lot?
 
Last edited:
Your code looks to have an error in your inner loop. I believe that the following code of yours would result in an endless loop.

VB.NET:
 Do While oneFlag <> Nothing
                FlagFields = oneFlag.Split(",")
                Flags(numFlags).flagDirection = FlagFields(1)
                Flags(numFlags).flagHorizontal = FlagFields(2)
                Flags(numFlags).flagVertical = FlagFields(3)
                Loop

You are not chaning 'oneFlag' so the while statement will never fail.

If I were doing this poject I would break up your Structure up into two. I've never seen Structures so i'm going to use classes.

Classes:
VB.NET:
     Private Class myFlag
        Public name As String
        Public parts As New List(Of myFlagInfo)
    End Class

    Private Class myFlagInfo
        Public direction As String
        Public color As String
        Public horizontal As Integer
        Public verticle As Integer
    End Class

Then all you have to do is modify your inner loop to add the flagInfo class to the Flag Class.

I'm newish to this forum and trying to participate but having a little hard time deciding weather to help point people in the right direction and let them figure out the rest or just give them code. So if you want more let me know I actually wrote out the whole bit of code but then decided not to post it..
 
I am unaware of this line of code and how it works :S.
VB.NET:
Public parts As New List(Of myFlagInfo)

I'm relatively new to VB.NET and have roughly 4 weeks of experience. :(
I am unsure how to use other methods other than the Do While one I put up, to put data into an array. since the data to draw the rectangle of a flag is split with a ";" while the individual piece of data (color, width etc) are split with a "," I was thinking it would be possible to write a nested loop and perhaps use an array within flagDir, flagHor and flagVert to make it like this inside the current Do While"

............
flagHor(ix)
.............
If (FlagFields Is oneFlag.Split(","))
ix = ix+1
End If
loop

then if there is a ; add ix+1 to store the second rectangles data?

I hope this makes sense. But yeah I would be grateful if you posted the solution you had and I could work off that.
 
Last edited:
Back
Top