user3115933
user3115933

Reputation: 4443

Why am I getting this specific error message with my VB codes?

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!

Upvotes: 1

Views: 124

Answers (3)

Craig
Craig

Reputation: 2474

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).

Upvotes: 1

mcutrin
mcutrin

Reputation: 134

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! =)

Upvotes: 2

Gray Programmerz
Gray Programmerz

Reputation: 609

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

Upvotes: 1

Related Questions