我的if else循环如何不访问正确的值? [英] how my if else loop is not accessing correct value?

查看:57
本文介绍了我的if else循环如何不访问正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



这里我使用if else循环来访问值。但它没有正确读取值。

我的代码块是:



  string  Huet = sdr [  MA_Is_HUET]。ToString() ; 

if (Huet == Y
{
myrow [ Huet] = ;
}
else if (Huet == N
{
myrow [ Huet] = ;
}





现在我使用调试器字符串如果数据库字段为Y n,则Huet显示值Y它将是N,但它就像在双倒置逗号等Y中使用一个空格。但该循环不会继续if-else if条件。直接进入下一个字符串。 PLZ帮帮我。

解决方案

我不知道你在哪里拍摄这个sdr [MA_Is_HUET]。一个快速的解决方案是修剪sdr变量,如下所示



  string  Huet = sdr [  MA_Is_HUET]。ToString()。Trim(); 





这将删除似乎导致问题的尾随空格。一个更好的方法是检查你为什么首先得到一个尾随空格!


使用 String.Trim方法 [ ^ ]


我们无法直接帮你 - 如果你说的是真的,那么它应该有效。



所以要做的第一件事就是真的非常仔细地查看你的数据。

首先添加一行:

  string  Huet = sdr [  MA_Is_HUET]。ToString(); 
string testing = - + Huet + - ;

并使用调试器查看确切地说,新变量包含什么。它应该是

  -  Y  -  

  -  N  -  

如果有的话是任何空格,那么你需要查看你的数据库:最可能的原因是你存储数据的字段是一个固定长度的NCHAR字段,可能是十个字符 - 这是默认值 - 你的一个字符串正在填充到固定长度。



如果不是这样,那么你需要使用调试器逐步查看它的确切位置。


Hello,

Here i am using if else loop for accessing value. But it is not read value properly.
My code block is:

string Huet = sdr["MA_Is_HUET"].ToString();

                if (Huet == "Y")
                {
                    myrow["Huet"] = "Yes";
                }
                else if(Huet == "N")
                {
                    myrow["Huet"] = "No";
                }



Now when i use debugger string Huet is showing value "Y " if database field is Y n if N then it will be "N " but it is like using one space in double inverted comma etc "Y ". but that loop is not continuing the if-else if condition. directly it goes to next string. plz help me for that.

解决方案

I don't know from where you are pouplating this sdr["MA_Is_HUET"]. A quick solution is to trim the sdr variable like this

string Huet = sdr["MA_Is_HUET"].ToString().Trim();



This will remove the trailing space which seems causing the problem. A better way would be to check why you are getting a trailing space in the first place!


Remove any extra white space around Huet using String.Trim Method[^]


We can't help you directly - if what you say is true, then it should work.

So the first thing to do is look at your data really, really closely.
Start by adding a line:

string Huet = sdr["MA_Is_HUET"].ToString();
string testing = "-" + Huet + "-";

And use the debugger to look at exactly what the new variable contains. It should be

-Y-

Or

-N-

If there are any spaces, then you need to look at your database: the most likely reason is that the field you are storing the data in is a fixed length NCHAR field, probably ten characters - that is the default - and your one character strings are being "padded" to that fixed length.

If that isn't the case, then you need to use the debugger to step through and see exactly where it is going.


这篇关于我的if else循环如何不访问正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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