Link to home
Start Free TrialLog in
Avatar of nouvelle
nouvelle

asked on

VB question

Hello,

Please help, I am lost.

I have a regular list box, I need to keep this part of the code. (attached).  

My question is when do I need to declare my array, because when I do it in the begnining, I have no errors but my listbox is empty. Please Help
Thanks

Dim composer(100) As String

Dim highestIndex As Integer = lstBox.Items.Count - 1

For n As Integer = 0 To highestIndex

composer(n) = CStr(lstBox.Items(n)) 'Composer() is a String array

Next

lstBox.Items.Clear()

lstBox.Sorted = False

For n As Integer = highestIndex To 0 Step -1

lstBox.Items.Add(composer(n))

Next

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

May I ask, what are you using this code for?
Avatar of nouvelle
nouvelle

ASKER

I do not know, I have yet to determine its effect. All I know, is I was asked to keep it. I forgot to mention, that the sorting has to be set to TRUE.

I did this but it does not work:
dim composer(100) as string
composer(0) = "Bach"
composer(1) = "Beethoven" 
composer(2) = "Chopin"
composer(3) = "Mozart"
composer(4)= "Tchaikovsky"

Open in new window

>I have yet to determine its effect.
The effect is that you move the listbox items to an array, clear the listbox, and add the items back to the listbox. Are you just trying to sort the listbox? Try using the Sort() function.
You seem to be filling the array from the list box, clearing the list box, and then loading it back again, so it isn't clear what you are trying to achieve.


This will load the listbox
composer(0) = "Bach"
        composer(1) = "Beethoven"
        composer(2) = "Chopin"
        composer(3) = "Mozart"
        composer(4) = "Tchaikovsky"
        Dim highestIndex As Integer = 4

        lstBox.Items.Clear()
        lstBox.Sorted = False
        For n As Integer = highestIndex To 0 Step -1
            lstBox.Items.Add(composer(n))
        Next

Open in new window

Thanks for your answer CodeCruiser. I am just making sure, I explained as I should.

I was shown a listbox, and was asked to determine the effect of the code I attached in my first post while the sorting property is set to true.

I do not know how to do what you just said.

So is this homework?
Graham Skan,

Thank you for your answer, I did it but it does not work. I have the same effect as with the way I tried before posting here. no errors but the listbox is empty.

Neilsr:

This question does not get old at all... :) I think, it is obvious that I tried and I am stuck.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>I have yet to determine its effect.

The listbox will be sorted descending.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys for your help. Code Cruiser, your solution works perfect!