Resolved How to Print array into list box?


New member
Jul 22, 2021
Programming Experience
Software: Visual Studio 2019
NET 5.0

First button prompts user to populate an array using input boxes and the second button should print that array into a list box.
When I run it, button 1 seems to work properly but button 2 doesnt print anything into the list box. No error messages pop out either.
What should I change in the code?

There's a lot to address here. First and foremost, DO NOT post code just as a picture. There may be cases where a screenshot can add value but, even then, you should ALWAYS post the code as text and formatted as code. We can't copy and paste code to quote it or test it for ourselves from a screenshot and they are also hard to view on mobile devices.

I'll ignore a number of the programming faux pas there as this is probably an assigned exercise and you can't do things differently. The reason your code is not working is that you are using two different arrays. You create one at the class level, then create another array in the first event handler and populate it, then use the original array to populate the ListBox. You didn't put anything in that array so of course you have nothing to put in the ListBox.

Now I want to copy some of your code to show you what you did wrong and I can't because you posted a picture. Let that be the last time. This:
Dim My_Original() As String = {}
will work but has issues. Firstly, don't use Dim to declare member variables. ALWAYS explicitly specify an access level for everything and that should pretty much ALWAYS be Private for fields. Secondly, you're assigning an empty array to the field that you will never use. Don't create objects you don't need. If you're going to assign an object to the variable later, don't assign anything to it when you declare it. Finally, it's a smaller thing but the current recommendation is to put the parentheses on the type rather than the variable when declaring an array variable. Only put the parentheses on the variable if you are initialising the variable by specifying an upper bound. With all that in mind, your code should be this:
Private My_Original As String()
That variable name is terrible too. It doesn't describe what the variable is for and, while you can use whatever naming conventions you want, there's rarely a good reason to not use the recommended camel casing, e.g.
Private userInputValues As String()
The real issue is here:
Dim My_original(elems - 1) As String
That is declaring a new local variable and assigning an array of the specified size to it. Why would you want another variable? You already have a variable at the class level. That's where you should be assigning the new array object you just created:
My_Original = New String(elems - 1) {}
You then populate the array assigned to that field and thus you'll get the input back from the array to add to the ListBox.

I think I just realised why you were pointlessly assigning an empty array to the field when you declared it. Without that, you'd get an exception when you tried to get the data from it to populate the ListBox. If that's the case, that should have been a clue that you weren't actually setting that field in the first event handler. You avoided the worked arounf the problem of the exception without solving the actual problem that caused it.
On an unrelated note, if you have the choice, I would suggest that you stick to targeting .NET Framework 4.8 for now. There are advantages to targeting .NET 5.0 but they really don't apply to beginners creating WinForms apps. .NET 5.0 is based on .NET Core and there are still a few things that have not been implemented in VB WinForms that people are used to from .NET Framework. Even if those few remaining holes are plugged in .NET 6.0, there will still be some differences that you should be cognisant of. Given that almost all the information on WinForms available on the web applies to .NET Framework and .NET Framework 4.8 will be supported for a long time yet, it makes sense to target .NET Framework 4.8 until you have a reason to do otherwise. That means using the project templates that specifically indicate .NET Framework.
Thank you so much for the explanation and the tips, I'll be sure to make use of it in our next projects.
And, I'm so sorry about the formatting of the thread. It won't happen again.