• 0

VB School Assignment help - Parallel Arrays


Question

Alright so I have a school assignment where I have to create a group box with two Radio Buttons, one for Boys, one for Girls. I then have to create a list box which will list the names of the boys or girls, depending on which Radio button is selected. I can show you a screenshot of what it should look like.

GenderListGUIjpg.JPG?4410

The directions for the assignment say I have to use two parallel arrays to do this. I have created all of the objects on the form, and I have declared the two parallel arrays in the Declaration of the Form, like this:

  ' Declare and initialize the name array
	Dim strNames() As String = {"Joe", "Bill", "Tina", "Lisa", "Tim", "Alan", "Jacob", _
	"Emily", "Jose", "Allison", "Charlie", "Joseph", "Erin", "Lindsey", "Dominic", _
	"Eric", "Jenna", "Paul", "Kyle", "Dawn", "Cody", "Valarie", "JD", "Michelle", "Ashlei"}

	' Declare gender
	Dim strGender() As String = {"M", "M", "F", "F", "M", "M", "M", "F", "M", "F", "M", "M", _
		"F", "F", "M", "M", "F", "F", "M", "F", "M", "F", "M", "F", "F"}

But now I'm a little stumped now. How do I sort the parallel arrays and display them in the listbox alphabetically?

Link to comment
Share on other sites

21 answers to this question

Recommended Posts

  • 0
Set the listbox's Sorted property to true :)

Well putting them alphabetically is really the least of my worries. What code do i enter into the radio buttons to have it compare each item in the parallel array and then list them in the list box. Is there some sort of Index i have to declare or something. My book doesn't have anything in it that really helps me with this, that's why I'm a bit confused about what to do after I declare the parallel arrays.

Link to comment
Share on other sites

  • 0

I suppose it's something along the lines of getting the index of each entry in strGender that matches the selected gender on the Checked event of either radio button, and then simply adding those same indexes from strNames to the listbox via a loop (or possibly some other way), is it not?

Edit: or, more simply, what Doli said.

Link to comment
Share on other sites

  • 0
When you click boys, have it check the gender index, if it is "M" get the index of that array and use it to reference the name array to put in the listbox.

Arrays are objects so look in "System.Array" for more help :

http://msdn.microsoft.com/en-us/library/system.array.aspx

Can you show me some example code? How do I declare the gender index? All of the lessons in my book declare the index as an integer, usually 0, is it the same way for arrays that contain strings?

I tried checking that website, didn't really help.

Link to comment
Share on other sites

  • 0

I'd say that you want to do a loop with a Counter index. In the loop have an IF statement with a condition variable that gets it's value from your Radio Box selection (M or F). Within the loop you want each match to either output to a new Array or with extra code and go straight to the list box (Depending on your assignment conditions/restrictions).

Your new array input will need to be the Counter index - 1 (As your array will start from 0) used within strNames() so you can output the correct Kid's name then you can use the same with strGender() if you want to Output the gender also.

Do some reading on a function called Ubound() to find the length of your strGender() array so you can set your loop condition and exit the loop when it runs out of entries.

Not going to do the code for you as that would be cheating ;)

Link to comment
Share on other sites

  • 0
Can you show me some example code? How do I declare the gender index? All of the lessons in my book declare the index as an integer, usually 0, is it the same way for arrays that contain strings?

I tried checking that website, didn't really help.

Example:

Public glbid As Integer = 0 'put this just below public class form 1


'this goes in the checkchanged
ListBox1.Items.Clear()

		For Each nam As String In strNames
			If strGender.GetValue(glbid) = "M" Then
				ListBox1.Items.Add(strNames.GetValue(glbid))
			End If
			glbid = glbid + 1
		Next

May be a little overcomplex, but it works.

(Although this code works in your situation without modification, I recommend you make sure you understand it, or rewrite it yourself. You will not learn by copying code. This is for learning purposes only.)

Edited by omnicoder
Link to comment
Share on other sites

  • 0
I'd say that you want to do a loop with a Counter index. In the loop have an IF statement with a condition variable that gets it's value from your Radio Box selection (M or F). Within the loop you want each match to either output to a new Array or with extra code and go straight to the list box (Depending on your assignment conditions/restrictions).

Your new array input will need to be the Counter index - 1 (As your array will start from 0) used within strNames() so you can output the correct Kid's name then you can use the same with strGender() if you want to Output the gender also.

Do some reading on a function called Ubound() to find the length of your strGender() array so you can set your loop condition and exit the loop when it runs out of entries.

Not going to do the code for you as that would be cheating ;)

So this is what I've created so far from your statements... Am I close, or no cigar? If i'm close, any hints on what I should do next?

		'Declare a loop control variable to use as an index
		Dim intIndex As Integer

		'Process each item in the list
		For intIndex = 0 To intGender.Length - 1

Example:

Public glbid As Integer = 0 'put this just below public class form 1


'this goes in the checkchanged
ListBox1.Items.Clear()

		For Each nam As String In strNames
			If strGender.GetValue(glbid) = "M" Then
				ListBox1.Items.Add(strNames.GetValue(glbid))
			End If
			glbid = glbid + 1
		Next

May be a little overcomplex, but it works.

(Although this code works in your situation without modification, I recommend you make sure you understand it, or rewrite it yourself. You will not learn by copying code. This is for learning purposes only.)

Actually, that looks like it is far simpler to understand than the method Aergan mentioned. I will use this if I can't figure out how to do the other method.

EDIT:

Just tried that method, I applied it to both the Male and the Female buttons, but it only works for one, the other option gives this error:

Index was outside the bounds of the array.
Edited by apreichner
Link to comment
Share on other sites

  • 0
EDIT:

Just tried that method, I applied it to both the Male and the Female buttons, but it only works for one, the other option gives this error:

Try setting glbid to 0 at the start of the code. Not sure but seems that it's continuing using what glbid is after the first pass.

Link to comment
Share on other sites

  • 0
Try setting glbid to 0 at the start of the code. Not sure but seems that it's continuing using what glbid is after the first pass.

You mean by doing this at the beginning:

Public glbid As Integer = 0

Link to comment
Share on other sites

  • 0

No. Currently it starts the second loop with the index from the first one which is too high and goes out of bounds. It needs to be reset.

After the end of the For loop, put glbid = 0.

Link to comment
Share on other sites

  • 0
You mean by doing this at the beginning:

Public glbid As Integer = 0

That's a type of variable declaration, it should only do that once per program instance.

Just add:

glbid = 0

below it

Link to comment
Share on other sites

  • 0
No. Currently it starts the second loop with the index from the first one which is too high and goes out of bounds. It needs to be reset.

After the end of the For loop, put glbid = 0.

Actually I solved this one myself. Instead of doing the glbid as a global variable to 0, I set glbid as an individual variable for each CheckChanged, by doing:

Dim glbid As Integer = 0

Link to comment
Share on other sites

  • 0

I think we're confusing him because I keep saying put it after the loop and you keep saying put it before. Isn't it better for performance to clear it once your done rather then when its needed again?

And good job finding your own solution.

Also, you might want to change the name. A local variable named glbid makes no sense :p

Link to comment
Share on other sites

  • 0
I think we're confusing him because I keep saying put it after the loop and you keep saying put it before. Isn't it better for performance to clear it once your done rather then when its needed again?

And good job finding your own solution.

Also, you might want to change the name. A local variable named glbid makes no sense :p

I agree with that final thought. I'll probably change it to intIndex.

Link to comment
Share on other sites

  • 0
I think we're confusing him because I keep saying put it after the loop and you keep saying put it before. Isn't it better for performance to clear it once your done rather then when its needed again?

And good job finding your own solution.

I've always set and cleared variables at the start of my code, suppose really it's good practice to do it at the start and end of your code.

I do know that from a marking point of view they prefer you not to do "unnecessary" lines after your main function has executed, which for some coding can be quite cowboy-ish.

Grats on working it out, now you've got to do the paper work ;)

Link to comment
Share on other sites

  • 0
I've always set and cleared variables at the start of my code, suppose really it's good practice to do it at the start and end of your code.

I do know that from a marking point of view they prefer you not to do "unnecessary" lines after your main function has executed, which for some coding can be quite cowboy-ish.

Grats on working it out, now you've got to do the paper work ;)

No, just a quiz after I finish. Which shouldn't be long. I finished the Menu, and now I've got to work on the small details like tab order and Alt key stuff.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.