Visual Basic Minor Problem

Associate
Joined
21 Sep 2005
Posts
180
Location
Dundee
Hi all,

My son is doing Visual Basic at school. Unfortunately they are using Visual Basic 6 which doesn't seem that up to date. I know PHP but am floundering with this as can't seem to find what I'm looking for although I know what sort of thing I'm looking for.

He's been given the task of creating a form that takes 5 integers and then does some other stuff with it. I'm guessing arrays here. Have been managing to work out some of it. Can get it to take the range of numbers but only ever prints out the last item in the array. Am sure it's something very simple and if someone could point out the error I'd appreciate it.

Thanks in advance.

Code:
Private Sub Form_Activate()

Dim scores(5) As Integer
Dim numbers As Integer


For numbers = 1 To 5
    scores(numbers) = InputBox("enter the scores")
    
    
    
Next numbers

List1.Items.Add scores(numbers)



End Sub
 
Thanks for the reply. Think there is a variety of ways to declare the array and think the original example is ok. Had already looked at that page when trying to help my son.

The below link shows a variety of methods.

http://www.vb6.us/tutorials/understanding-arrays

We've got the declaration slightly wrong though. Should be as below for a 5 position array. Going to try working with that.

Code:
Dim scores(4) As Integer

Edit - Think the add at the bottom is wrong too as he's trying to print.
 
Last edited:
Think you will need to add each entry in the array individually btw - a long time since I used VB6 but I don't think it auto fills a list item from an array. (Either add another for loop or move the add bit into the for loop).


EDIT: "print" functionality doesn't really apply to visual basic (you can print to certain objects i.e. the main form) but that is more a throw over from console/fullscreen programs before proper UIs existed and not really something you want to encourage. (i.e. put me.print scores(numbers) after the inputbox line ).
 
Last edited:
Have got it working with 2 loops. Am pretty sure it's not very elegant though but it's a start. Thanks for the help.

Code:
Private Sub Form_Activate()

Dim scores(4) As Integer
Dim numbers As Integer


For numbers = 0 To 4
    scores(numbers) = InputBox("enter the scores")
Next


For numbers = 0 To 4
    Form1.Print scores(numbers)
Next




End Sub
 
Afaik it is(unless you work in public sector in which case you can only dream of using vb6:D). However if I'm assuming correctly its not a private school thus he's lucky to even have access to vb6 :(. My ICT web designed was limited to excel and word :((5years ago now). I even got in trouble for using dreamweaver at home.
 
Last edited:
His school does actually have VB10 as well but they were having problems with it on their network so they dropped down to VB6 as it was issue free from a school use perspective. I do wish they were using something more up to date tho but for learning coding basics at their level then I guess it's ok.
 
We use Python at KS4 and Java at KS5 though I might change the latter to C# next year.

Python is a fairly nice starter language as it is dynamically typed and isn't too fussy when setting up sub routines etc.
 
Yes, so why learn on a dead language? They could start straight on VB.net.

How many teachers do you think know vb.net?

Anyone that does know vb.net to a decent standard as to enable them to teach it, wont take a pay cut to go into teaching.

Chicken and egg situation it teaching IT I'm afraid.
 
How many teachers do you think know vb.net?

Anyone that does know vb.net to a decent standard as to enable them to teach it, wont take a pay cut to go into teaching.

Chicken and egg situation it teaching IT I'm afraid.

Had that when I was studying (over 10 years ago), any decent programmers got poached by IBM before they'd been teaching more than a few months* and I was basically helping the others out with the work they were setting for the next class :S still remember the look on my lecturers face when I took his bubble sort routine - based on a textbook example no less - and reduced it to 1/3rd the code and 10x the performance heh.


* As one said when the choice was between 15-20K salary or closer to 40 with the chance of going far higher it was no choice.
 
Yup, it's one reason why IT grads get paid £25k to do a 1yr pgce (I might do it at the end of my degree).
 
How many teachers do you think know vb.net?

Anyone that does know vb.net to a decent standard as to enable them to teach it, wont take a pay cut to go into teaching.

Chicken and egg situation it teaching IT I'm afraid.

Well not "anyone" though I am better with C# than VB... :D
 
Code:
Private Sub Form_Activate()

Dim scores(5) As Integer
Dim numbers As Integer

For numbers = 0 To 5
    scores(numbers) = InputBox("enter the scores")
    List1.Items.Add scores(numbers)    
Next numbers

End Sub

I thought InputBox only accepted strings though so not sure if this will work.
 
Code:
Private Sub Form_Activate()

Dim scores(5) As Integer
Dim numbers As Integer

For numbers = 0 To 5
    scores(numbers) = InputBox("enter the scores")
    List1.Items.Add scores(numbers)    
Next numbers

End Sub

I thought InputBox only accepted strings though so not sure if this will work.

It will do an implicit conversion from string to int unless you have Option Explicit turned on (from memory, been a long time since I've used VB)
 
From memory it is Option Strict to stop the implicit conversions, Option Explicit means you need to declare all of your variables. Option Explicit should always be turned on.
 
Last edited:
Back
Top Bottom