1

Am new to VB.Net. I have been given a task to write user methods for Power, Square, Cube using Interface. When I compile the code, I get the error:

Exception Caught: Array index is out of range

I don't know where am I making the mistake. I have just started learning VB.Net.

Here's the source code of Interface:

Interface Calc
    Sub Square(ByVal number As Double)
    Sub Cube(ByVal number As Double)
    Sub Power(ByVal number As Double, ByVal raiseTo As Double)
End Interface

Here's the source code of the class where I am implementing the above interface:

Class Calculation
    Implements Calc

    Sub Square(ByVal number As Double) Implements Calc.Square
        Console.WriteLine("Square of " & number & " = " (number * number))
    End Sub

    Sub Cube(ByVal number As Double) Implements Calc.Cube
        Console.WriteLine("Power of " & number & " = " (number * number * number))
    End Sub

    Sub Power(ByVal number As Double, ByVal raiseTo As Double) Implements Calc.Power
        Console.WriteLine("Power of " & number & " = " (number ^ raiseTo))
    End Sub
End Class

And here's the main method:

Sub Main()
    Console.WriteLine(vbCrLf & vbTab & "********** Calculation Using Interface *********")

    Dim obj As Calculation = New Calculation()

    Try
        obj.Square(5.0)
        obj.Cube(5.0)
        obj.Power(5.0, 4.0)
    Catch ex As Exception
        Console.WriteLine(vbCrLf & "Exception Caught: " & ex.Message.ToString())
    End Try

    Console.WriteLine(vbCrLf & "Press any key to exit....")
    Console.ReadLine()
End Sub
5
  • where your are getting the error ?? Commented Jun 24, 2014 at 8:33
  • Umm... Console.WriteLine("Square of " & number.ToString() & " = " & (number * number).ToString()) (missing operator, missing .ToString()) etc.? Commented Jun 24, 2014 at 8:35
  • @hector I don't know.. No specific line. Commented Jun 24, 2014 at 8:46
  • @AndrewMorton Tried your solution, but same error... Commented Jun 24, 2014 at 8:46
  • You should switch Option Strict on. Your code won't compile with that switched on. This will allow you to find errors at design time rather than runt time. Commented Jun 24, 2014 at 8:55

2 Answers 2

1

In your output you are concatenating a string, but you have missed out an & in your concatenation

This line:

Console.WriteLine("Square of " & number & " = " (number * number))

Should be:

Console.WriteLine("Square of " & number & " = " & (number * number))

The error message is (I think) due to the compiler evaluating the " = " As a string array and trying to return the (number * number) th element of it. Consider the following:

Console.WriteLine((New String("foo"))(1)) 'this fails

As a side note, you should switch Option Strict On. See here for reasons why. This won't fix your issue but it will not allow the code to compile until you have fixed this issue.

It is much better to spend time fixing a compile time error before deploying your application than trying to remotely debug runtime errors.

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

2 Comments

Wilko yes ofcource Console.WriteLine("Square of " & number.ToString() & " = " & (number * number).ToString()) this should definitely works
@Matt, thanks for pointing out the mistake.. Also thanks for the links, those are really helpful for a beginner like me
0

try this

Public Class Calculation
Implements CalcIFace
Sub Square(ByVal number As Double) Implements CalcIFace.Square
    Console.WriteLine("Square of " & number & " = " & number * number & "")
End Sub
Sub Cube(ByVal number As Double) Implements CalcIFace.Cube
    Console.WriteLine("Power of " & number & " = " & number * number * number & "")
End Sub
Sub Power(ByVal number As Double, ByVal raiseTo As Double) Implements CalcIFace.Power
    Console.WriteLine("Power of " & number & " = " & number ^ raiseTo & "")
End Sub
End Class
  • In OP's Method the Concatenation was wrong

  • OP did Console.WriteLine("Square of " & number & " = " (number * number)) and this should be concatenate correctly like Console.WriteLine("Square of " & number & " = " & (number * number))

2 Comments

Can you add some explanation to your answer please. The OP has probably copied and pasted your answer without understanding what was wrong in the first place
The issue was nothing to do with the parentheses

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.