# "Derivative Calculator" Question

• (2 Pages)
• 1
• 2

## 15 Replies - 2326 Views - Last Post: 15 December 2013 - 04:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=336526&amp;s=c25bd1cdfa67fdc5a3607d39f9a16231&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Watchmeprogramit

Reputation: 0
• 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

• blow up my boots

Reputation: 6541
• Posts: 26,478
• 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.

### #3 Watchmeprogramit

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

## Re: "Derivative Calculator" Question

Posted 14 December 2013 - 02:23 PM

andrewsw, 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?

### #4 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,478
• 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?

### #5 HeBTeMy

• D.I.C Regular

Reputation: 20
• 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

### #6 Watchmeprogramit

Reputation: 0
• 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

### #7 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,478
• 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.

### #8 Watchmeprogramit

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

## Re: "Derivative Calculator" Question

Posted 14 December 2013 - 04:31 PM

andrewsw, 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

### #9 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,478
• Joined: 12-December 12

## Re: "Derivative Calculator" Question

Posted 14 December 2013 - 04:55 PM

What have you typed in txtPower and txtX?

### #10 HeBTeMy

• D.I.C Regular

Reputation: 20
• 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

### #11 Watchmeprogramit

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

## Re: "Derivative Calculator" Question

Posted 14 December 2013 - 05:24 PM

HeBTeMy, 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

### #12 HeBTeMy

• D.I.C Regular

Reputation: 20
• 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

### #13 Watchmeprogramit

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

## Re: "Derivative Calculator" Question

Posted 14 December 2013 - 05:49 PM

HeBTeMy, 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

### #14 HeBTeMy

• D.I.C Regular

Reputation: 20
• 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.

### #15 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• 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)