如何获取要在文本框中返回的值 [英] How to get a value to return in a textbox

查看:103
本文介绍了如何获取要在文本框中返回的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在工作项目中遇到问题,想知道是否有人可以帮助我.我正在尝试计算当天的翻新百分比.公式为

Hi,

I am having issues for a project for work and was wondering if anybody can help me. I am trying to calculate The percentage for refurb for the day. The formula is

Refurb_Rate = (totalRefurb / totalUnits * 100)

程序应假定对db中的翻新计数并在文本框中返回一个值.我将值初始化为0,但是我的问题是,每当用户扫描要翻新的单元时,如何获取该值以进行计算.我尝试执行的任何操作都会给我一个程序错误.现在,它返回值1,因为我添加了RefurbRate++.谁能帮我?谢谢.


贾斯汀

这是我对代码所做的事情:

The program is to supposed count the refurb in the db and return a value in a textbox. I initialized my values to 0, but my problem is how do I get it to calculate each time a user scans in a unit to be refurbed. Anything I try to do gives me an error in my program. Right now it''s returning a value of of 1 because I added RefurbRate++. Can anyone help me? Thanks.


Justin

Here is what I''ve done with my code:

private int GetRefurbRate()
{
     string sql = "";
     int Refurb_Rate = 0;
     int totalRefurb = 0;
     int totalUnits = 0;
     string error_msg = "";

     sql = "SELECT COUNT(*) " +
            "FROM " + schema + ".repair_part rp  " +
            "WHERE rp.repair_ord = '" + txtRO.Text + "' ";
     while (true)
     {
          if (!myDb.RunSql(sql, true))
          {
             error_msg = "DBError for getting Refurb Rate";
             break;
          }
          if (myDb.dbRdr.HasRows)
          {
              if (myDb.dbRdr.Read())
              {
                 try
                 {
                     Refurb_Rate = (totalRefurb / totalUnits * 100);
                 }
                 catch (Exception e)
                 {
                      Console.WriteLine(e);
                 }
               }
              //Refurb_Rate= Convert.ToInt32(myDb.dbRdr.GetValue(10));
              Refurb_Rate++;
          }
          break;
     }
     myDb.dbRdr.Close();
     if (error_msg != String.Empty)
     {
         MessageBox.Show(error_msg, "Get Refurb Rate",
                          MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
     }
     return Refurb_Rate;
}

推荐答案

我看到的问题很少.您在这里不需要此异常处理.相反,请执行以下操作:

I can see few problems. You don''t need this exception processing here. Instead, do this:

double refurbRate;
int totalRefurb = //?
int totalUnits = //?
System.Diagnostics.Debug.Assert(totalUnits >= 0 && totalRefurb >= 0 && totalUnits >= totalRefurb);
refurbRate = totalUnits > 0 ? (double)totalRefurb * 100 / totalUnits : 0;



更好:



Even better:

double refurbRate;
uint totalRefurb = //?
uint totalUnits = //?
System.Diagnostics.Debug.Assert(totalUnits >= totalRefurb);
refurbRate = totalUnits > 0 ? (double)totalRefurb * 100 / totalUnits : 0;



总单位为零的零百分比是很合逻辑的.也不应该例外.

您不应该使用整数百分比(如果从来没有).在您的代码中,由于四舍五入而导致编码错误,您可以通过先乘以100来改进它,但是double类型更好.如果您需要输出整数,则会以适当的格式在string.Format中显示.

您还会滥用异常:太早就在本地进行处理了.

在此处找到一些更好的异常处理说明:
我如何制作滚动条到达底部时将停止的循环 [当我运行应用程序时,异常是捕获了如何处理此问题? [



Zero percentage for zero total units is quite logical. It should not be the exception.

You should not use integer percentage (if fact, never). In you code, you get wrong coding due to rounding, you could improve it by multiplying by 100 first, but double type is better. If you need integer on output, it will be shown via appropriate format in string.Format.

You also misuse exceptions: handle it too locally, to early.

Find some instructions of better exception handling here:
How do i make a loop that will stop when a scrollbar reaches the bottom[^]
When i run an application an exception is caught how to handle this?[^]

Good luck,

—SA


SAKryukov给了您很好的解释.它强调的一件事是不能以这种方式处理异常.

我认为小数学可以帮助您 c = a/b is equivalent to c= a* ( b power -1)

因此,要调整.net,可以使用Refurb_Rate = (totalRefurb * System.Math.Pow(totalUnits,-1))*100.

现在不会抛出异常,但是如果将其除以零,则结果将为 NAN or Infinity.分配后检查有效值Refurb_Rate,如果无效,则根据需要分配零?.

干杯.
SAKryukov given you a good explanation. One thing it stresses don''t handle the exception in this way.

I think little mathematics helps you c = a/b is equivalent to c= a* ( b power -1)

So to tweak .net you can use Refurb_Rate = (totalRefurb * System.Math.Pow(totalUnits,-1))*100.

Now it won''t throw exceptions, but if divided by zero the result will be a NAN or Infinity. After assignment check for a valid value Refurb_Rate, if not valid value then as your wish, assign a zero?.

Cheers.


首先,我在这里看到0/0类型的划分:Refurb_Rate = (totalRefurb / totalUnits * 100),这是非法的.
其次,很高兴看看myDb.dbRdr.GetValue(10)的值是什么(据我所知,这是出现错误的那一行).如果在这里遇到异常,则很可能是从数据库中获得的值格式错误.
问候
Firstly I see here a division of type 0/0: Refurb_Rate = (totalRefurb / totalUnits * 100), which is illegal.
Second, it would be nice to see if what is the value of: myDb.dbRdr.GetValue(10) (as I''ve understood that''s the line where you get the error). If you get an exception here, then most probably the value that you get from the database is in a wrong format.
Regards


这篇关于如何获取要在文本框中返回的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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