1

I am not that familiar with Visual Basic and I need to run this set of VB codes.

Module VBModule
    Sub Main()
        Dim Ticket_Cost As Decimal
          Ticket_Cost = 25.0
        Dim Trainup_times() As Integer = {"9,11,13,15"}
        Dim Trainup_tickets() As Integer = {"480,480,480,480"}
        Dim Trainup_money() As Integer = {"0.0,0.0,0.0,0.0"}

        Dim Traindown_times() As Integer = {"10,12,14,16"}
        Dim Traindown_tickets() As Integer = {"480,480,480,640"}
       Dim Traindown_money() As Integer = {"0.0,0.0,0.0,0.0"}
        For count = 0 To 4
            PrintLine(Trainup_times(count), "\t\t\t", Trainup_tickets(count), "\t\t\t", Trainup_money(count))
            PrintLine(Traindown_times(count), "\t\t\t", Traindown_tickets(count), "\t\t\t", Trainup_money(count))
        Next
    End Sub
End Module

I am getting the following error when testing the codes online at https://www.onlinegdb.com/online_vb_compiler

Error message:

/home/main.vb (17,20) : error VBNC30451: 'count' is not declared. It may be inaccessible due to its protection level.

Any help would be much appreciated!

4
  • You have all strings there, as in: {"9,11,13,15"}. Then, you need For count As Integer = 0 To 3: you have only 4 elements per array, not 5. Traindown_money() will be an array of single or double, not integer values. -- The PrintLine() syntax is probably wrorg. Commented Feb 1, 2021 at 7:28
  • Thanks. Tried what you mentioned but still not working! Commented Feb 1, 2021 at 8:13
  • If you made any changes, you need to update your code. You have more problems there. Have you turned the arrays into real arrays? For now, you have a string in there. Trainup_money() and Traindown_money() should be Single, Double or Decimal? Do you need to write this to a file? Also, do you need this code to compile for VB6 or VB.Net? Pick one. Commented Feb 1, 2021 at 8:16
  • It's not VBA but as the message says *you haven't declared count. You do that with a dim statement. Commented Feb 1, 2021 at 19:29

3 Answers 3

2

You may need to declare your countvariable before using it on the loop. You can do it in the same line where you declare your For...Next loop.

Change For count = 0 To 4 for For count As Integer = 0 To 4. This may make the error disappear.

EDIT: I have been looking at your code, and there are some mistakes that should be corrected if you want it to function properly.

First of all, you need to change the way you declare your arrays since you are telling that the elements inside of them are Integer and using ". You need to remove those quotes from all the array declarations, as well as declaring the last one as decimal, since the data is not integer nor string:

Dim Trainup_times() As Integer = {9,11,13,15}
Dim Trainup_tickets() As Integer = {480,480,480,480}
Dim Trainup_money() As Decimal = {0.0,0.0,0.0,0.0}

Dim Traindown_times() As Integer = {10,12,14,16}
Dim Traindown_tickets() As Integer = {480,480,480,640}
Dim Traindown_money() As Decimal = {0.0,0.0,0.0,0.0}

You also need to rewrite the loop declaration, since you need to declare count as I told you before, and also loop only from 0 to 3, since you arrays have 4 elements, not 5:

For count as integer = 0 To 3

Finally, if you need your output to be displayed into the same window, I'd recommend using Console.WriteLine function to do it.

    For count as integer = 0 To 3
            
      console.writeline(Trainup_times(count) & "   " & Trainup_tickets(count) & "   " & Trainup_money(count) )
      console.writeline(Traindown_times(count) & "   " & Traindown_tickets(count) & "   " & Traindown_money(count) )
      console.writeline("-------------------------")
Next
           

All together it would look like this:

Module VBModule
    Sub Main()
        Dim Ticket_Cost As Decimal
          Ticket_Cost = 25.0
        Dim Trainup_times() As Integer = {9,11,13,15}
        Dim Trainup_tickets() As Integer = {480,480,480,480}
        Dim Trainup_money() As Decimal = {0.0,0.0,0.0,0.0}

        Dim Traindown_times() As Integer = {10,12,14,16}
        Dim Traindown_tickets() As Integer = {480,480,480,640}
        Dim Traindown_money() As Decimal = {0.0,0.0,0.0,0.0}
        For count as integer = 0 To 3
        
           console.writeline(Trainup_times(count) & "   " & Trainup_tickets(count) & "   " & Trainup_money(count) )
           console.writeline(Traindown_times(count) & "   " & Traindown_tickets(count) & "   " & Traindown_money(count) )
           console.writeline("-------------------------")
           
        Next
    End Sub
End Module

Try this out and let's see if it Works for you! =)

Sign up to request clarification or add additional context in comments.

6 Comments

Tried what you suggested and I get the following error: "Unhandled Exception: System.IO.IOException: Bad file name or number."
You are trying to print your information in a file using PrintLine, but you are not opening a file or anything. The correct syntax to use PrintLine should be something like this: FileOpen(1, "c:\trash.txt", OpenMode.Output) PrintLine(1, "This is a test.") You need to tell the PrintLine function where to print the data you want.
I want the output in the VB window. Should I use Print then instead of PrintLine?
Thanks for helping out here! I am still getting the error message "Unhandled Exception: System.IO.IOException: Bad file name or number." when running your set of codes.
Hmm, I feel like you have something in the code you are testing that doesn't work. If it Works correctly when you do it from a link but it doesn't when you paste the code on your compiler, there must be something wrong there.
|
1

Not sure about older vb versions, but this code have alot of mistakes according to new vb.net version.

Mistake 1 (defining array of integers) :

If you put quotes like below, then it would called array with 1 string. Not array of integers.

Dim Trainup_times() As Integer = {"9,11,13,15"} 'Incorrect

Just remove quotes like below:

Dim Trainup_times() As Integer = {9,11,13,15} 'Correct

Mistake 2 (Declare count as integer) :

Compiler doesn't know what count type is.

For count = 0 To 4 'Incorrect

You should declare count as integer like below:

For count As Integer = 0 To 4 'Correct

Mistake 3 (Use vbTab instead of \t) :

\t is basically Tab space which is used in C-Like languages. Not in vb.net . So just replace , "\t\t\t", with & vbTab &. And thats it.

Final code:

There were other minor mistakes as well. But I assume you can learn by seeing this full code below:

Module VBModule
    Sub Main()
        Dim Ticket_Cost As Decimal = 25.0
        Dim Trainup_times() As Integer =   {9,  11, 13, 15}
        Dim Trainup_tickets() As Integer = {480,480,480,480}
        Dim Trainup_money() As Integer = {0.0,0.0,0.0,0.0}
        Dim Traindown_times() As Integer = {10,12,14,16}
        Dim Traindown_tickets() As Integer = {480,480,480,640}
        Dim Traindown_money() As Integer = {0.0,0.0,0.0,0.0}
        For count as Integer = 0 To 3
            Console.WriteLine(Trainup_times(count) & vbTab & Trainup_tickets(count) & vbTab & Trainup_money(count))
            Console.WriteLine(Traindown_times(count) & vbTab & Traindown_tickets(count) & vbTab & Trainup_money(count))
        Next
    End Sub
End Module

https://onlinegdb.com/r1M6MSrgO

2 Comments

Thank you for this very elaborate answer! Highly appreciated. When I copy and paste your code in that online VB compiler, it fails with the following error message: "Unhandled Exception: System.IO.IOException: Bad file name or number." However when I click on the link you gave, it works fine! Any idea about this behavior?
@user3115933 Its other problem. Its common error when you open a file and try to read. Many resources address this: microsoft, stackoverflow. If you share your full code, we can easily know which line causes issue. And also Debugger will helps you.
1

In addition to the suggestions in the other answers, the immediate problem can also be fixed by turning on type inference. This can be done in the project settings to activate it for the project as a whole, or it can be done in an individual file by adding Option Infer On as a single line to the top of the project file.

With type inference on, you won't need to explicitly declare the type of a local variable in code in a context where the compiler can figure out what the type should be. For loop index variables are one such context: the compiler can figure out that you need to declare count and it can infer that the type should be Integer from the type of the indices (0 and 3 are of type Integer).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.