价值在代码中的某处丢失。 [英] Value being lost somewhere in code.

查看:55
本文介绍了价值在代码中的某处丢失。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真是一个愚蠢的问题。我有一段时间没有使用过C#,现在我的一些知识似乎有点朦胧。无论如何,我正在创建一个计算器来计算你的BMI。单击计算按钮后,值将发送到ViewModel。 ViewModel计算BMI并将结果保存在域中。在调试中,当我将鼠标悬停在结果变量上时,结果显示很棒。但它在我的应用程序中返回0。某处价值正在丧失。我承认我忘记了如何准确地实现构造函数。这是我的代码。



申请代码:



 < span class =code-keyword>使用 Biogym.Domain; 
使用 Biogym.ViewModel;
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;

命名空间 Biogym.Views.BMI_Calculator
{
public partial class bmiCalculator:表格
{
private bmiViewModel _viewModel = new bmiViewModel();

BMI bmi = new BMI();

public bmiCalculator()
{
InitializeComponent();
}

private void bmiCalculator_Load( object sender,EventArgs e)
{
cmbUnits.Items.Add( 公制);
cmbUnits.Items.Add( English);
}

private void btnCalculate_Click( object sender,EventArgs e)
{
string Unit = cmbUnits.SelectedText;
_viewModel.CalculateBMI(Unit,txtMass.Text,txtHeight.Text);
lblResult.Visible = true ;
txtResult.Visible = true ;
txtResult.Text = bmi.Result.ToString();这是我的ViewModel代码:



 使用 Biogym.Domain; 
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 Biogym.ViewModel
{
public class bmiViewModel
{
public void CalculateBMI( string 个单位,字符串质量, string height)
{
int Mass = Convert.ToInt32(mass);
decimal 高度= Convert.ToDecimal(height);
十进制结果;

BMI bmi = new BMI();

if (units == 公制
{
result =(质量/(高度*高度));
bmi.Result =结果;
}
else
{
result =(质量/(高度*高度))* 703 ;
bmi.Result =结果;
}
}
}
}





这是我的域名代码:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 Biogym.Domain
{
public class BMI
{
public 字符串单位{获取; set ; }
public int Mass { get ; set ; }
public decimal 高度{ get ; set ; }
public decimal 结果{ get ; set ; }
}
}





我很确定问题是我必须使用构造函数。但是我完全忘记了如何在上面的代码中实现它。请帮忙!



问候,

克里斯

解决方案

如果你采取好好看看你的代码:

  public   void  CalculateBMI(字符串单位,字符串质量,字符串身高)
{
int Mass = Convert.ToInt32(mass);
decimal 高度= Convert.ToDecimal(height);
十进制结果;

BMI bmi = new BMI();

if (units == 公制
{
result =(质量/(高度*高度));
bmi.Result =结果;
}
else
{
result =(质量/(高度*高度))* 703 ;
bmi.Result =结果;
}
}

这很明显,真的。让我把它缩小到你需要看的位:

  public   void  CalculateBMI(字符串单位,字符串质量, string  height)
{
BMI bmi = new BMI();
result = 666 ;
bmi.Result =结果;
}

变量 bmi 在方法中构造,并赋值。它们在方法结束时超出范围,并且对您创建的实例的唯一现有引用将被丢弃 - 将计算结果留在堆中,等待垃圾收集器切入并处理它。



您在 bmiViewModel 中引用的 bmi 从calculate方法返回时的类是一个完全不同的实例,它特定于 bmiViewModel 类的当前实例(this)。它与您输入计算结果的版本没有任何关系,所以它确实会有零结果。



可能,你想将BMI传递给计算方法 - 或者更好,返回结果并将其放入bmi实例。

  public  十进制 CalculateBMI(字符串单位,字符串质量, string  height)
{
int Mass = Convert.ToInt32(mass);
decimal 高度= Convert.ToDecimal(height);
十进制结果;

if (units == 公制
{
result =(质量/(高度*高度));
}
else
{
result =(质量/(高度*高度))* 703 ;
}
返回结果;
}



  string 单位= cmbUnits.SelectedText; 
bmi.Result = _viewModel.CalculateBMI(Unit,txtMass.Text,txtHeight.Text);
lblResult.Visible = true ;


This is really a dumb question. I haven't been using C# for a while and now some of my knowledge seems to be a little bit hazy. Anyway, I am creating a calculator to work out your BMI. Once you click on the calculate button the values are sent to the ViewModel. The ViewModel calculates the BMI and saves the result in the Domain. In debugging the result shows wonderful when I hover over my result variable. But it returns 0 in my application. Somewhere the value is being lost. I admit that I have forgotten how to exactly implement a constructor. Here is my code.

The application code:

using Biogym.Domain;
using Biogym.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Biogym.Views.BMI_Calculator
{
    public partial class bmiCalculator : Form
    {
        private bmiViewModel _viewModel = new bmiViewModel();

        BMI bmi = new BMI();

        public bmiCalculator()
        {
            InitializeComponent();
        }

        private void bmiCalculator_Load(object sender, EventArgs e)
        {
            cmbUnits.Items.Add("Metric");
            cmbUnits.Items.Add("English");
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            string Unit = cmbUnits.SelectedText;
            _viewModel.CalculateBMI(Unit, txtMass.Text, txtHeight.Text);
            lblResult.Visible = true;
            txtResult.Visible = true;
            txtResult.Text = bmi.Result.ToString();
        }        
    }
}



Here is my ViewModel code:

using Biogym.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Biogym.ViewModel
{
    public class bmiViewModel
    {
        public void CalculateBMI(string units, string mass, string height)
        {
            int Mass = Convert.ToInt32(mass);
            decimal Height = Convert.ToDecimal(height);
            decimal result;

            BMI bmi = new BMI();

            if (units == "Metric")
            {
                result = (Mass / (Height * Height));
                bmi.Result = result;
            }
            else
            {
                result = (Mass / (Height * Height)) * 703;
                bmi.Result = result;
            }
        }
    }
}



Here is my domain code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Biogym.Domain
{
    public class BMI
    {
        public string Units { get; set; }
        public int Mass { get; set; }
        public decimal Height { get; set; }
        public decimal Result { get; set; }
    }
}



I am quite sure that the problem is I have to use a constructor. But I have totally forgotten how, and cannot implement it in my code above. Please help !

Regards,
Chris

解决方案

If you take a good look at your code:

public void CalculateBMI(string units, string mass, string height)
{
    int Mass = Convert.ToInt32(mass);
    decimal Height = Convert.ToDecimal(height);
    decimal result;

    BMI bmi = new BMI();

    if (units == "Metric")
    {
        result = (Mass / (Height * Height));
        bmi.Result = result;
    }
    else
    {
        result = (Mass / (Height * Height)) * 703;
        bmi.Result = result;
    }
}

It's pretty obvious, really. Let me condense this to the bits you need to look at:

public void CalculateBMI(string units, string mass, string height)
{
    BMI bmi = new BMI();
    result = 666;
    bmi.Result = result;
}

the variable bmi is constructed in the method, and assigned a value. It them goes out of scope at the end of the method, and the only existing reference to the instance you created is discarded - leaving the result of your calculation floating about in the heap waiting for the Garbage Collector to cut in and Dispose of it.

The bmi you refer to in your bmiViewModel class when you return from the calculate method is a completely difference instance which is specific to the current instance ("this") of the bmiViewModel class. It shares nothing with the version you put the calculation result with, so it will indeed have a result of zero.

Probably, you want to pass the BMI into the calculate method - or better, return the result and put it into the bmi instance.

public decimal CalculateBMI(string units, string mass, string height)
{
    int Mass = Convert.ToInt32(mass);
    decimal Height = Convert.ToDecimal(height);
    decimal result;

    if (units == "Metric")
    {
        result = (Mass / (Height * Height));
    }
    else
    {
        result = (Mass / (Height * Height)) * 703;
    }
    return result;
}


string Unit = cmbUnits.SelectedText;
bmi.Result = _viewModel.CalculateBMI(Unit, txtMass.Text, txtHeight.Text);
lblResult.Visible = true;


这篇关于价值在代码中的某处丢失。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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