如果发生状况,关于类型数据转换的错误消息 [英] ERROR MESSAGE ABOUT TYPE DATA CONVERSION IN IF CONDITION

查看:72
本文介绍了如果发生状况,关于类型数据转换的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用VB做一个中子活化计算器.在我的程序中,用户必须输入原子名称(AL,H,K,Mn等),以便计算器可以运行.每个原子都有自己的性质,例如质量,同位素丰度等.我决定编写程序 像这样运行,如果用户写AL,则mass = 2.3同位素丰度= 0.5.如果用户写H,则质量= 2.8同位素丰度= 0.2.定义该值后,程序即可运行.输入AL存储在变量"atom"中.与字符串数据类型.可变质量 和同位素丰度(双数据类型).当我运行程序时,它说将STRING转换为DOUBLE时出错.我不知道如何解决它.有人可以帮助我解决这个问题吗?非常感谢你.

解决方案

如果字符串中有数字双精度值,则要在计算中使用该值,您需要将字符串取为双精度型.为此,您可以(例如)调用一个函数来执行此操作.下面的函数可以用字符串调用 它将以数字而不是字符串的形式返回double值.

私有函数GetDouble(以字符串形式)为Double
    昏暗v为双= 0.0
    如果Double.TryParse(s,v)然后返回v
    回报0.0
结束功能

您可以像这样使用它:

'示例字符串
        Dim s As String ="12.345"
        '只是将double值放入变量中
        昏暗n作为Double = GetDouble(s)
        '在样本计算值中使用它
        昏暗的myCalculation为Double = 10 * GetDouble(s)


i want to make a neutron activation calculator with VB. in my program, user has to input the name of atom (AL, H, K, Mn, etc) so that the calculator can run. Every atom has its own property, like mass, isotopic abundance, etc. i decide to make my program run like this, IF user write AL then mass=2.3 isotopic abundance = 0.5 . IF user write H then mass=2.8 isotopic abundance = 0.2. after the value is defined, the program can run. input AL store in variable "atom" with string data type. Variable mass and isotopic abundance with double data type. when i run my program, it said error in conversion STRING to DOUBLE. i dont know how to fix it. could anyone help me with this problem? thank you very much.

解决方案

Hi

If you have a numeric double value in a string, then to use the value in a calculation, you need to take the string and change it to a double. To do this, you can (for example) call a function to do this. The function below can be called with the string and it will return the double value as a number rather than a string.

Private Function GetDouble(s As String) As Double
    Dim v As Double = 0.0
    If Double.TryParse(s, v) Then Return v
    Return 0.0
End Function

You can use it like this:

        ' example string
        Dim s As String = "12.345"
        ' just get the double value into a variable
        Dim n As Double = GetDouble(s)
        ' use it in a sample calculated value
        Dim myCalculation As Double = 10 * GetDouble(s)


这篇关于如果发生状况,关于类型数据转换的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆