Getting info into Jagged Arrays via Split(?)

Ok, so I went and got myself all confused.  I have been researching and trying out different things and I get nowhere. I checked out some reference here with the jagged arrays, but they are not what I was hoping.

I'll just put out what I want to accomplish and I hope that someone can help???

I wish to populate two combo boxes.  (Lets call them Cbx1 and Cbx2.)   The contents of Cbx2 depend on what is chosen in Cbx1.  That part I have under control and working well.

The problem is that my code it HORRENDOUS because of the various options in the combo box and using "IF" statement for the second combo box. So I thought that he best thing would be to use a jagged array to define the contents of the two combo boxes.

I would like to define the contents of jagged array through the use of Splitting strings.   In this way:
String contents--->Jagged Array contents--->Cbx1 contents-----Cbx2 contents

In this way, I can simply add or delete string elements without changing code.

So, Here are my strings (ex.)
ChoicesStr(0) = "Ford, Mustang, Focus"
ChoicesStr(1) = "Toyota, Camry, Rav, Pilot, Matrix"
ChoicesStr(2)  = "Mazda, Protoge"
ChoicesStr(3) = "GM, Rendevous, Vibe, Aveo, G3, Cobalt"

So the strings would be split according using the "," and the first entry of each string would be placed in an array that I would load in Cbx1 and the rest of the entries would be the choices for Cbx2 based on Cbx1.  Again, once I get the strings into the Jagged Array, I can figure the rest out.

Phew!
Thanks!

BTW, this is my first question ever, and I don't understand how many points this should be!  it seems fairly easy, but what do I know???  As a side note, let me know if I've over or under pointed it?









MoeMoe7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Where is your initial data coming from and what format is it in?

Or are you hard-coding that info in your app?
0
HainKurtSr. System AnalystCommented:
use split

dim arr() as string = ChoicesStr(3).split(", ")

to get an array so you can bind it to comboboxes...
to get the first element, you can use arr(0)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
There are sooooo many ways to do this...

Here's on way:
Public Class Form1

    Private Class Cars

        Public Manufacturer As String
        Public Models As New List(Of String)

        Public Sub New(ByVal CarData As String)
            Models.AddRange(CarData.Split(","))
            Manufacturer = Models(0)
            Models.RemoveAt(0)
        End Sub

        Public Overrides Function ToString() As String
            Return Manufacturer
        End Function

    End Class

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add(New Cars("Ford, Mustang, Focus"))
        ComboBox1.Items.Add(New Cars("Toyota, Camry, Rav, Pilot, Matrix"))
        ComboBox1.Items.Add(New Cars("Mazda, Protoge"))
        ComboBox1.Items.Add(New Cars("GM, Rendevous, Vibe, Aveo, G3, Cobalt"))
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim index As Integer = ComboBox1.SelectedIndex
        If index <> -1 Then
            Dim c As Cars = CType(ComboBox1.Items(index), Cars)
            ComboBox2.DataSource = c.Models
        End If
    End Sub

End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

MoeMoe7Author Commented:
Hi Idle!

hmmmm..... I see your logic there.  Thank you for you quick response! I like the elegance, but I was looking for something more general:

Basically,  for now, I am hard-coding the "data strings", but eventually I would like to import them from a text file.  As such, I was hoping for a solution in the following format

.
.
'1) set the strings as shown above (into an array)
.
.
.'2) parse the strings into a jagged array
.
.
'3) load the first "column" of the j.a. into Cbx1
.
.
'4) lookout for SelectedIndexChanged in Cbx1 and then populate Cbx2 accordingly


Basically, I have steps #1, 3 and 4....its step 2 that is tripping me up.

Any Ideas?



But, if you could also clarify something for me....in lines 21 to 24 of your code...it seems you load the entire string into ComboBox1.  Will the user see that entire string or will the fact that you have specified Cars as a Class filter it out?









0
Mike TomlinsonMiddle School Assistant TeacherCommented:
The entire string gets passed to the Cars class constructor where it is parsed and then stored as individual items.  The entire string is simply discarded.  The Cars class has a ToString() override which determines how it will look in the ComboBox.

I'll post up a Jagged Array example later...
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You stated:

    "...eventually I would like to import them from a text file"

Using the same code, as before, all you have to do is change the Load() event:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim data As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "CarData.txt")
        For Each line As String In System.IO.File.ReadAllLines(data)
            ComboBox1.Items.Add(New Cars(line))
        Next
    End Sub

Open in new window

CarData.txt
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
*I can still show you a jagged array if you want...but I think that's more work than needed!
0
MoeMoe7Author Commented:
uuuu!!! thank you for the text file import!  I was going to get to that eventually, but I just might add it sooner rather than later!

In fact, the notion that you import the file and read each line into the ComboBox means that I can substitute:

ComboBox1.Items.Add(New Cars(line))

with the line

ComboBox1.Items.Add(New Cars(ChoicesStr(i)))
within a loop.
Will that work?

I figure then, the I can set my strings like in my original post, then loop them into the combo box in the same manner that you have.  Or am I completely bonkers?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You certainly could do that...  =)
0
MoeMoe7Author Commented:
Ok!

So, I took it and changed up a few things and wow! it worked very well!

I am a newbie to this OOP stuff!  I still think in very linear terms and I have trouble getting my head around Class, Methods and stuff like that.  I can sort of follow the code, but it is still a little bit of voodoo magic to me!

Thanks for your help Shaman IdleMind!
0
MoeMoe7Author Commented:
Not what I had in mind, but was probably better accomplished in the suggested way instead!
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hopefully that helped you a little along the way to OOP nirvana...  ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.