none
value of 'integer' cannot be converted to a '2-dimensional array' RRS feed

  • Question

  • Hi, im having a problem w/ this game i have to program 4 hmk

    i need to display the array in a grid format (5 x 6)

       

     Sub Main()
            Dim i As Integer = 0
            Dim x As Integer = 0
            Dim n As Integer
            Dim name As String
            Dim numbers(4, 5) As Integer
            Dim numbersarray(4, 5) As Integer
            numbers(4, 5) = 69
            Console.WriteLine("Press 1 to enter your name, 2 to play and 3 for the scoreboard")
            n = Console.ReadLine
            If n = 1 Then
                Console.WriteLine("Enter your player name:")
                name = Console.ReadLine
                Console.WriteLine("Enter another number to perform another action")
                If n = 2 Then
                    numbersarray(4, 5) = game(name, numbers(4, 5))
                    For i = 0 To 5
                        For x = 0 To 6
                            Console.WriteLine(numbersarray(i, x))
                        Next
                        Console.WriteLine(numbersarray(i, x))
                    Next
                ElseIf n = 3 Then

                End If
            ElseIf n = 2 Then
                name = "Player"
                numbersarray(4, 5) = game(name, numbers(4, 5))
                For i = 0 To 5
                    For x = 0 To 6
                        Console.WriteLine(numbersarray(i, x))
                    Next
                    Console.WriteLine(numbersarray(i, x))
                Next
            ElseIf n = 3 Then

            End If
            Console.Read()
        End Sub

        Function game(ByRef name, ByRef numbers(,))
            Console.WriteLine(name & ", it's time to play!")
            Dim i As Integer
            Dim x As Integer
            Randomize()
            For i = 0 To 4
                For x = 0 To 5
                    numbers(i, x) = Int(Rnd() * 1000) + 1
                Next
            Next

            Return numbers
        End Function

    im not very advanced :(

    Wednesday, March 21, 2018 10:15 PM

All replies

  • Hi, im having a problem w/ this game i have to program 4 hmk

    This is very old code that needs quite a few changes to bring it up to .Net standards.  You might be better off starting from scratch. Is there a reason for using a console app instead of a form?

    You should always indicate the line of code where the error occurs.   I can't see the problem line with a quick scan but it could be something like

      numbers = 3

    numbers is an array and that code is trying to assign an integer to an array, which won't work.

    You have not specified a Type for the game function.  Randomize has been replaced by the Random object.



    • Edited by Acamar Thursday, March 22, 2018 9:14 PM sp
    Wednesday, March 21, 2018 10:31 PM
  • Hi NConnnolly,

    According to your code,  you create function that has two parameters, one is string, another is 2-dimensional array as numbers(,). There are two place that you could need to take case.

       numbers(4, 5) = 69
    numbersarray(4, 5) = game(name, numbers(4, 5))

    You take numbers(4,5)=69 as game's parameter, but the game need one string, and 2-dimensional array as numbers(,).

    Here is declare 2-dimensional array

    Dim numbers(,) As Integer

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 22, 2018 9:01 AM
    Moderator
  • Hi cherry,

    numbersarray(4, 5) = game(name, numbers(, ))

    it now says that the array subscript is missing

    pls help

    Thursday, March 22, 2018 2:59 PM
  • Yea, I'm also curious why she wrote this. 

    She uses a way of MS Basic 1.0

    Try to change your program using an arraylist, or even better a generic List. 

    A pity from the current Microsoft information is that they want to show legal information first and then by the end user needed information so you have to scroll very deep to the bottom. 

    https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx


    Success
    Cor


    Thursday, March 22, 2018 3:17 PM
  • it now says that the array subscript is missing

    The problem is that you are using code that is not designed for VB .Net.  It might work, or it might not, but it will be very difficult to understand why.

     Function game(ByRef name, ByRef numbers(,))

    defines a function that takes two parameters, but the type for the parameters is not specified and there is no return type.  Therefore it will default.  Using those defaults is difficult, and in any case you should not rely on defaults.  Make your code specific.

    The function appears to create an integer array of 5x6 then fills the array with random values and returns the array.  So it should probably be:

        Function game(Name As Integer) As Integer(,)
            Console.WriteLine(Name & ", it's time to play!")
            Dim i As Integer
            Dim x As Integer
            Dim Numbers(4, 5) As Integer
            Randomize()
            For i = 0 To 4
                For x = 0 To 5
                    Numbers(i, x) = Int(Rnd() * 1000) + 1
                Next
            Next
            Return Numbers
        End Function

    That is, a function that takes a single argument (the player name) and returns an array initialized with random numbers.

    The problem is, that's not the way the function is used.  

        numbersarray(4, 5) = game(name, numbers(4, 5))

    That means 'Assign the result of calling the game function to cell(4,5) in the array 'numberarray''.  You can't assign an array of integers to one cell in another array of integers.  It is not clear what that code is supposed to do.  Either the function is wrong, or it is being used incorrectly.  Is it possible that you got the code for the game function confused with some other code?

    I would recommend starting over again with a good description of the game design and some pseudo-code describing how it's going to work.  Then start coding the routines using .Net coding standards.


    • Edited by Acamar Thursday, March 22, 2018 9:39 PM fmt
    Thursday, March 22, 2018 9:38 PM
  • Well the below code works, corrects the errors in your original code. However the logic is unsound for the If statements and there is no reason to have the function write anything to the console really.

    You need to develop logic such that if a person enters 1 and types their name it is displayed and saved. So then when the person enters 2 or 3 the name is still available.

    I don't know if the game is supposed to exit after being played one time but I would suspsect not.

    So you would need a loop to constantly loop if 1, 2 or 3 (whatever 3 was supposed to be for) is selected and then perform something as well if the person doesn't select 1, 2 or 3 what to do since it is in error.

    Which is why pseudocode or the flow of operation of logic needs to be determined so you can code to that flow of logic really. The below code for a grade would probably not pass in my opinion depending on the teachers line of reasoning. However it does work as far as the function goes if 2 is selected. But that isn't enough to consider it effective grade wise IMO.

    Option Strict On
    
    Module Module1
    
        Sub Main()
    
            Dim n As Integer
            Dim name As String
            Dim numbers(4, 5) As Integer
            Dim numbersarray(4, 5) As Integer
    
            numbers(4, 5) = 69
            Console.WriteLine("Press 1 to enter your name, 2 to play and 3 for the scoreboard")
            n = CInt(Console.ReadLine)
            If n = 1 Then
                Console.WriteLine("Enter your player name:")
                name = Console.ReadLine
                Console.WriteLine("Enter another number to perform another action")
                If n = 2 Then
                    numbersarray = game(name, numbers)
                    For i = 0 To 4
                        For x = 0 To 5
                            Console.WriteLine(numbersarray(i, x))
                        Next
                    Next
                ElseIf n = 3 Then
    
                End If
            ElseIf n = 2 Then
                name = "Player"
                numbersarray = game(name, numbers)
                For i = 0 To 4
                    For x = 0 To 5
                        Console.WriteLine(numbersarray(i, x))
                    Next
                Next
                Console.ReadLine()
            ElseIf n = 3 Then
    
            End If
            Console.ReadLine()
        End Sub
    
        Function game(ByRef name As String, ByRef numbers(,) As Integer) As Integer(,)
            Console.WriteLine(name & ", it's time to play!")
            Dim i As Integer
            Dim x As Integer
            Randomize()
            For i = 0 To 4
                For x = 0 To 5
                    numbers(i, x) = CInt(Rnd() * 1000) + 1
                Next
            Next
            Return numbers
        End Function
    
    End Module
    



    La vida loca

    Saturday, March 24, 2018 3:35 AM
  • Here's a loop you can play with to see what it does.

    Option Strict On
    
    Module Module1
    
        Dim cki As ConsoleKeyInfo
        Dim SomeString As String = ""
    
        Sub Main()
            Console.WriteLine("Please select 1 to enter a string or press Esc (Escape) to exit application.")
            Do
                cki = Console.ReadKey(True)
                Select Case cki.Key
                    Case ConsoleKey.D1
                        Console.Clear()
                        Console.WriteLine("Enter a string of characters.")
                        SomeString = Console.ReadLine
                        Console.Clear()
                        Console.WriteLine("The string of characters entered was " & SomeString & " so press 1 to continue or press Esc (Escape) to exit the application.")
                    Case Else
                        Console.Clear()
                        Console.WriteLine("Please select 1 to enter a string or press Esc (Escape) to exit application.")
                End Select
            Loop While cki.Key <> ConsoleKey.Escape
        End Sub
    
    End Module


    La vida loca

    Saturday, March 24, 2018 4:01 AM