3 Replies - 1635 Views - Last Post: 05 February 2010 - 06:52 AM Rate Topic: -----

#1 cis170   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 10

Error List Description "Integer" and "option strict on dis

Posted 04 February 2010 - 11:12 PM

Hi:

I am a beginner in VB, I do know a little code, I am not asking any one to do my work for me. I am just looking for an explantion of what I am doing wrong within my code. I am creating a program where I have to find the answer to the base and exponent. I had everything working until I put option strict on (which is required)for my assignment. My first error is "option strict on disallows implicit conversions from double to integer"and the other is "integer". The error is found in the statement Result = numOne ^ numTwo. Can someone please explain what I have done wrong. If possible in simple terms,as I am new to programming.
Public Class Form1

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Function ExponentNumbers(ByVal numOne As Integer, ByVal numTwo As Integer) As Integer
        Dim Result As Integer
        Result = numOne ^ numTwo
        Return Result

    End Function

    Private Sub CalFunction_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalFunction.Click

        Dim intNumOne, intNumTwo As Integer
        Dim intResult As Integer

        If Not IsNumeric(Me.numOne.Text) Then
            intNumOne = CInt(Me.numOne.Text)
        Else
            MessageBox.Show("Error", "Non-numeric entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
            End


            If Not IsNumeric(Me.numTwo.Text) Then
                intNumTwo = CInt(Me.numTwo.Text)

            Else
                MessageBox.Show("Error", "Non-numeric entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
                End
                intResult = ExponentNumbers(intNumOne, intNumTwo)

            End If

        End If

    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Error List Description "Integer" and "option strict on dis

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,015
  • Joined: 23-August 08

Re: Error List Description "Integer" and "option strict on dis

Posted 05 February 2010 - 06:05 AM

Moved to VB.NET
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: Error List Description "Integer" and "option strict on dis

Posted 05 February 2010 - 06:36 AM

Well, basically what is happening is the compiler can make assumptions about how you declare variables. Option strict On basically turns off these assumptions so the compiler has to know what it needs to do.

If you look at the end of the blue squiggly line where the error occurs. There is a red box. put your mouse on it and you should get a red exclamation icon. Click it and nine times out of ten it will tell you how to correct the option strict error for that particular problem.

Notice my tag line... I understand your pain :)

EDIT:
One other thing. If Not IsNumeric(me.numOne.Text) Then...
This line is basically saying
If numOne.Text is not a number then. You need to change this

This post has been edited by CharlieMay: 05 February 2010 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 nmgod   User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Re: Error List Description "Integer" and "option strict on dis

Posted 05 February 2010 - 06:52 AM

Result = numOne ^ numTwo

either change the result to a double or typecast it to and integer like this.
Result = CInt(numOne ^ numTwo)

It is doing this because it is designed so that if a user was to say do 1.4 ^ 2.5 it would need to return a double, but since you are parsing two integers it will have to be a whole number so typecasting wont loose any data.


and btw you can replace

Dim Result As Integer
Result = Cint(numOne ^ numTwo)
Return Result

with
Return Cint(numOne ^ numTwo)

This post has been edited by nmgod: 05 February 2010 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1