开发者

adding variables numical values (newb question)

Yesterday i had a look at how to set values of variables from nummbers stored in external txt files

the variables then needed to be added up so i used trial and error first

((XVAL) + (NEWVAL))

assuming that XVAL was set to 10 and NEWVAL was set to 20 i expected to get the answer of thirty but waqs presented with the new value of 10 20

VB.net pysicaly added the two values together but i wanted the mathematical product of the two which is ((10) + (20)) = 30

yep its a newb question could anyone explain how to achieve wh开发者_Python百科at im affter


XVAL and NEWVAL are strings, so they are simply being concatenated together. You need to convert them to integers, so that VB.NET will treat them as such. To do this, use the Int32.Parse() method.

Dim intXVAL As Integer = Int32.Parse(XVAL)
Dim intNEWVAL as Integer = Int32.Parse(NEWVAL)

Dim result = intXVAL + intNEWVAL


You want to cast them to a number first. Try CDbl. See http://msdn.microsoft.com/en-us/library/Aa263426 for more.

edit: Oops, thought you were talking about VBA. Try using Double.Parse(YOURVALUE) if you're talking about VB.NET.


Have you tried the Val() function?

Val(XVAL) + Val(NEWVAL)


The + operator in VB.NET (for backwards-compatibility reasons) means both add and concatenate depending on the types of the variables it is being used with. With two numeric types (Integer, Single, Double, etc.), it adds the values together as you would expect. However, with String types, it concatenates the two strings.

Presumably, then, your XVAL and NEWVAL variables are String types because they're being read out of a text file, which is causing VB.NET to concatenate them into a new string instead of add them together. To get the behavior you're expecting, you need to convert them to numeric types.

Some of the other answers suggest casting simply casting the string values to numeric types (CInt, CSng, CDbl, etc.), but this may not work as expected if the value contained by your string cannot be converted to number. The Int32.Parse method will throw an exception if the value held by your string cannot be represented as a number. This is especially important to keep in mind if you're reading values from a text file that are not guaranteed to adhere to any particular constraints.

Instead, you probably want to use something like Int32.TryParse, which returns a Boolean value indicating whether or not the conversion succeeded and will not throw an exception.


As you are reading from a text file I assume that you are reading your values out as strings, so when you do this:

((XVAL) + (NEWVAL))

It is effectively concatenating the two strings together. In order to get the mathematical product of the two values these need to be int/integers which is the number type.

There are a number of ways you can do this, but in essence you have to 'cast' the strings to ints and then do your calculation.

So in vb.net it would be something like this (pseudo code):

Dim xval As String = "10"
Dim newval As String = "20"

Dim x As Integer = Int32.Parse(xval)
Dim n As Integer = Int32.Parse(newval)

Dim prod As Integer = x + n

Console.WriteLine(prod)

There are a number of other methods of doing this, for example using:

int.Parse(...)

or

Integer.TryParse(...)

More information on these sorts of type conversions can be found here:

http://dotnetperls.com/integer-parse-vbnet

One thing to bear in mind with these sorts of conversions is that you have to be certain that your input data is convertable. Otherwise your code will throw exceptions. This is where TryParse is useful as you can use this to check the inputs and handle invalid inputs without the need for exceptions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜