"Derivative Calculator" Question

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1054 Views - Last Post: 15 December 2013 - 04:31 AM Rate Topic: -----

#1 Watchmeprogramit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

"Derivative Calculator" Question

Posted 14 December 2013 - 01:35 AM

Dim Xvariable As String
        Dim Power As Decimal
        Dim Derivative As String
        Xvariable = txtX.Text
        Decimal.TryParse(txtPower.Text, Power)
        Derivative = (Xvariable * Power)^(Power-1) 
        lblderivative.Text = Derivative

My problem is that I get a error when I use this code and I think the problem is that the variable power is not a string and so I cannot multiple it to Xvariable. Can anyone help me with this code, give suggestions, or tell me what am I doing wrong? Thanks in advance to anyone who helps

This post has been edited by andrewsw: 14 December 2013 - 04:26 AM
Reason for edit:: Use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: "Derivative Calculator" Question

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 04:29 AM

This is VB.NET, not VB6. Topic moved.

Xvariable is a string and you cannot multiply a string by a number (Xvariable * Power).

Add Option Strict On to the top of your code and it will be more obvious; you will have to explicitly convert between types, and not assume that VS will do this for you. Recommended.
Was This Post Helpful? 1
  • +
  • -

#3 Watchmeprogramit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 02:23 PM

View Postandrewsw, on 14 December 2013 - 04:29 AM, said:

This is VB.NET, not VB6. Topic moved.

Xvariable is a string and you cannot multiply a string by a number (Xvariable * Power).

Add Option Strict On to the top of your code and it will be more obvious; you will have to explicitly convert between types, and not assume that VS will do this for you. Recommended.

What would I convert it to? A string or Integer after turning the option strict on?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 02:42 PM

I don't know, that's up to you: you are writing the program. What is it supposed to be?
Was This Post Helpful? 0
  • +
  • -

#5 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 03:14 PM

To multiply it's logical to use integers imo... you can convert it using Convert.ToInt32
Was This Post Helpful? 0
  • +
  • -

#6 Watchmeprogramit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 04:23 PM

I tried it but, it didn't work instead it said that there was an error, and that X conversion to double is not valid.

This post has been edited by andrewsw: 14 December 2013 - 04:23 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 04:25 PM

Post your current code and the full error message that you receive.
Was This Post Helpful? 0
  • +
  • -

#8 Watchmeprogramit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 04:31 PM

View Postandrewsw, on 14 December 2013 - 04:25 PM, said:

Post your current code and the full error message that you receive.

"An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "x" to type 'Double' is not valid." This is the error that I get
        Dim X As String
        Dim Power As Decimal
        Dim Derivative As String
        Decimal.TryParse(txtPower.Text, Power)
        X = txtX.Text
        Derivative = CStr((Power * CDbl(X)) ^ (Power - 1))
        lblDerivativeIntegral.Text = Derivative

This is my code that I used. thanks for looking at it.

This post has been edited by andrewsw: 14 December 2013 - 04:51 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 04:55 PM

What have you typed in txtPower and txtX?
Was This Post Helpful? 0
  • +
  • -

#10 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 04:58 PM

To me that doesn't give me any error... but, your first using Decimal.TryParse, and for the Double conversion your using CDbl in the same block? Why not use a "single" method for all, like Double.TryParse?
The error might depend on what your textbox'es contains...

Also, what does "Power" does? Since it's with the default value... ? Do you need that at all?

This post has been edited by HeBTeMy: 14 December 2013 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

#11 Watchmeprogramit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 05:24 PM

View PostHeBTeMy, on 14 December 2013 - 04:58 PM, said:

To me that doesn't give me any error... but, your first using Decimal.TryParse, and for the Double conversion your using CDbl in the same block? Why not use a "single" method for all, like Double.TryParse?
The error might depend on what your textbox'es contains...

Also, what does "Power" does? Since it's with the default value... ? Do you need that at all?

txtX.text = X
txtPower.text = 2
Was This Post Helpful? 0
  • +
  • -

#12 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 05:27 PM

You typed "X" in TxtX? Well, that's not a number you realize that? And you can't convert it to Double...

This post has been edited by HeBTeMy: 14 December 2013 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

#13 Watchmeprogramit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 05:49 PM

View PostHeBTeMy, on 14 December 2013 - 05:27 PM, said:

You typed "X" in TxtX? Well, that's not a number you realize that? And you can't convert it to Double...

thank you, I will try using converting X to maybe a string or something in that order so that I can get the derivative of X^2
Was This Post Helpful? 0
  • +
  • -

#14 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 05:57 PM

Hmm, i never done the derivative of anything as far as i remember... i don't know how dar it goes on vb..
see if this helps, check last comment.
Was This Post Helpful? 0
  • +
  • -

#15 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: "Derivative Calculator" Question

Posted 14 December 2013 - 06:14 PM

X was already a String. Can you explain what your output should look like? Are you just making an Expression builder?

Dim derivative As String = String.Format("{0}^{1}", txtX.Text, txtPower.Text)

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2