我寫了一個小的 WF 程式來計算一些冷凝器和歐姆定律。我現在想整理一下。我遇到了一個問題,我使用了 2 個雙精度值,它們都被賦值為 0。我可以從電壓雙精度值中洗掉該值。但不是現在的。我不明白為什么。有什么我想念的嗎?錯誤訊息是 CS0165 使用未分配的區域變數“當前”并發生在呼叫 CalcResistance 方法的行中
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Kondensator_Ohmsches_Gesetz_Calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void calcResistance_Click(object sender, EventArgs e)
{
double voltage;
double current;
bool ok = double.TryParse(textBox1.Text, out voltage) && double.TryParse(textBox2.Text, out current);
if (ok)
{
textBox3.Text = Formulacollection.CalcResistance(voltage, current);
}
else
{
textBox3.Text = "Error Format not found";
}
textBox4.Text = Formulacollection.ConvertMicro(textBox3.Text);
textBox5.Text = Formulacollection.ConvertMilli(textBox3.Text);
textBox6.Text = Formulacollection.ConvertKilo(textBox3.Text);
textBox7.Text = Formulacollection.ConvertMega(textBox3.Text);
}
}
Formulacollection 類的作業方式如下:
using System;
using System.Collections.Generic;
using System.Text;
using static System.Math;
using System.Numerics;
using System.Globalization;
namespace Kondensator_Ohmsches_Gesetz_Calc
{
public static class Formulacollection
{
public static string CalcResistance(double voltage, double current)
{
var resistance = voltage / current;
return resistance.ToString();
}
}
uj5u.com熱心網友回復:
當您使用 && 運算子時,不會總是評估第二部分。嘗試使用 & 代替它,或者在宣告它時將默認值設定為 current 。
uj5u.com熱心網友回復:
首先CS0165是使用未初始化的變數時出現的錯誤。但是這里錯誤發生在current正確初始化的變數上。關于錯誤的簡介在這里
所以嘗試在類中全域初始化變數,然后嘗試編譯它。
然后有一個概率of確定可變保持的兩個值TryParse同時,也替換&&用&。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/372032.html
