声明一个变量并进行比较 [英] declare a variable and compare

查看:64
本文介绍了声明一个变量并进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我想声明一个变量,然后将其与几个整数值进行比较,
但我已声明为

字符串Basic = get_data.Tables [0] .Rows [0] ["BASIC"].ToString();

当我比较相同时,它说,字符串不能与整数进行比较.
我是.Net的新手,请提出如何声明以及应声明的内容.
我知道这是一个简单的问题,但抱歉,建议您这样做.

解决方案

您无法在C#中将字符串与整数进行比较-它们不兼容.就编译器而言,您可能正在尝试将单词"Dolphins"与数字42进行比较-这很愚蠢.

有多种方法可以满足您的需求,但是它们在很大程度上取决于您必须使用哪种数据.例如,首先要从数据库中检索的数据是数字的吗?看一下表定义,然后找出答案.如果是数字,则不要将其转换为字符串,而应将其转换为数字(在将(int)(double)放在get_data调用前面并更改类型,根据需要将其设置为intdouble的基本"),然后使用该名称.

如果它是字符串,则需要先将其解析为数字,然后才能使用它,但是如果它是字符串中的数字,那么为什么要首先将其存储为字符串?

Dear Friends,

I want declare a variable and compare the same with few integer value,
but i have declared as

string Basic = get_data.Tables[0].Rows[0]["BASIC"].ToString();

While I am comparing the same it say that, string cannot be compared with integer.
I am new to .Net, Kindly suggest how to declare and as what should I declare.
I know it is a simple question, but sorry kindly suggest.

解决方案

You can''t compare a string to an integer in C# - they are not compatible. As far as the compiler is concerned, you might be trying to compare the word "Dolphins" with the number 42 - which is just silly.

There are ways to do what you want, but they will depend to a large extent on what kind of data you have to play with. For example, is the data you are retrieving from your database numeric in the first place? Have a look at the table definition, and find out. If it is a number, then don''t convert it to a string, convert it to a number instead (which is pretty must a case of placing (int) or (double) in front of the get_data call and changing the type of "Basic" to an int or double as needed), and then use that.

If it is a string, then you need to Parse it to a number before you can use it, but if it is a number-in-a-string then why are you storing it as a string in the first place?


这篇关于声明一个变量并进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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