-1

Having trouble with arrays in VB. I'm converting a program from C to VB, and have it down for the most part except for an issue involving entering a number in an array. Code below.

    Sub convertNumber()

    Do While (numberToConvert <> 0)

        convertedNumber(digit) = numberToConvert Mod base 'Line 47
        digit += 1
        numberToConvert /= base

    Loop

End Sub

This is the particular sub where it crashes. The issue seems to be with "convertedNumber(digit)", however I can't figure it out. I tried doing some debugging, and every seems to be working fine. Its just the value I need entered into the array seems to break the whole thing. The whole program is posted below.

    Module Module1

Dim convertedNumber() As Integer
Dim numberToConvert As Integer
Dim base As Integer
Dim digit As Integer
Dim moddedNumber As Integer

Sub Main()

    'Calls in other SubModules
    Call getNumberAndBase()
    Call convertNumber()
    Call displayConvertedNumber()

    Console.ReadLine() ' Waits to terminate program

End Sub

Sub displayConvertedNumber()

    Dim baseDigit() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}

    Dim nextDigit As Integer
    Console.WriteLine("Converted Number = ")

    digit = digit - 1
    For digit As Integer = digit To 0
        nextDigit = convertedNumber(digit)
        Console.WriteLine(baseDigit(nextDigit))
        digit -= 1
    Next

    Console.WriteLine()

End Sub

Sub convertNumber()

    Do While (numberToConvert <> 0)

        convertedNumber(digit) = numberToConvert Mod base 'Line 47
        digit += 1
        numberToConvert /= base

    Loop

End Sub

Sub getNumberAndBase()

    Console.WriteLine("Number to be converted?")
    numberToConvert = Console.ReadLine()
    Console.WriteLine("Base?")
    base = Console.ReadLine()

    If base < 2 Or base > 16 Then
        Console.WriteLine("Bad base - Must be between 2 and 16")
        base = 10
    End If

End Sub

    End Module
  • ...Questions without a **clear problem statement** are not useful to other readers. – Ňɏssa Pøngjǣrdenlarp Nov 24 '15 at 23:46
  • 1
    What's the value of digit? If it's > 0, then you need `For digit As Integer = digit To 0 Step -1` Does that compile with your `digit` being declared twice? – LarsTech Nov 24 '15 at 23:47
  • 1
    You don't say what the issue is, but it looks like `convertedNumber` is never initialized and so is null (are you getting a [`NullReferenceException`](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it)?). If so, you could just initialize it to something big enough for most cases (`Dim convertedNumber(20) As Integer`) or `ReDim Preserve` it as you loop around. Maybe [this](https://msdn.microsoft.com/en-us/library/wak0wfyt.aspx) will help. – Mark Nov 25 '15 at 00:03
  • Sorry, new to this whole thing. The issue that the compiler is coming up with is "Object variable or With Block Variable not set. " The value of the digit should be what ever the user enters. I tracked it by having the console print it before it passed to convertedNumber, so its gathering everything alright, its just crashing once it sets the mod to convertedNumber Edit* Just changed my for loop. It works. Thanks everyone – Matthew Rawlings Nov 25 '15 at 00:19

1 Answers1

0

As posted by LarsTech, it was an issue with my For loop. By adding "Step -1" to the end of it, it fixed the problem.

Thank you!