为什么我的c#类函数返回显示红色波浪形?

时间:2017-03-02 16:22:20

标签: c#

我是一名VB.net编程器已经有好几年了。由于软件公司将默认SDK更改为C#,我最近被迫学习C#。我不介意挑战。我确实遇到了我目前正在处理的这个Array函数的问题。我认为我已正确调出变量并且我的If语句结构正确。我唯一的问题是在代码块末尾的返回变量下面有一条红色波浪线。红色波浪形意味着我有错误或返回语法不正确。我没有看到我错在哪里,所以我要求第二意见。

这是代码类。这个类的作用是从表单接收一个Integer,并且它应该返回一个带有4到5位小数的double。

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

    namespace CarterCalculationTools
    {
        public class FcmValue
        {
            public int FcmVal(int[] NeeNum)
            {
                var arrFcmVal = new double[61];
                double fcmResult;

                arrFcmVal[6] = 6276;
                arrFcmVal[7] = 7014.48155;
                arrFcmVal[8] = 7553.5088;
                arrFcmVal[9] = 7927.82976;
                arrFcmVal[10] = 8210.66384;
                arrFcmVal[11] = 8407.07949;
                arrFcmVal[12] = 8544.72716;
                arrFcmVal[13] = 8649.93887;
                arrFcmVal[14] = 8707.52206;
                arrFcmVal[15] = 8750.81112;
                arrFcmVal[16] = 8771.81307;
                arrFcmVal[17] = 8777.41987;
                arrFcmVal[18] = 8770.4081;
                arrFcmVal[19] = 8755.49639;
                arrFcmVal[20] = 8735.9733;
                arrFcmVal[21] = 8712.949;
                arrFcmVal[22] = 8679.01338;
                arrFcmVal[23] = 8644.57568;
                arrFcmVal[24] = 8611.92784;
                arrFcmVal[25] = 8575.37948;
                arrFcmVal[26] = 8542.55544;
                arrFcmVal[27] = 8495.9123;
                arrFcmVal[28] = 8451.60508;
                arrFcmVal[29] = 8409.80863;
                arrFcmVal[30] = 8370.20725;
                arrFcmVal[31] = 8334.23982;
                arrFcmVal[32] = 8293.85668;
                arrFcmVal[33] = 8252.51027;
                arrFcmVal[34] = 8214.39145;
                arrFcmVal[35] = 8177.75277;
                arrFcmVal[36] = 8135.08678;
                arrFcmVal[37] = 8095.31568;
                arrFcmVal[38] = 8058.21202;
                arrFcmVal[39] = 8023.5711;
                arrFcmVal[40] = 7986.15127;
                arrFcmVal[41] = 7949.5622;
                arrFcmVal[42] = 7915.15875;
                arrFcmVal[43] = 7882.78926;
                arrFcmVal[44] = 7852.31547;
                arrFcmVal[45] = 7822.26029;
                arrFcmVal[46] = 7786.75829;
                arrFcmVal[47] = 7753.07449;
                arrFcmVal[48] = 7721.09674;
                arrFcmVal[49] = 7690.72174;
                arrFcmVal[50] = 7661.85424;
                arrFcmVal[51] = 7634.40623;
                arrFcmVal[52] = 7608.29631;
                arrFcmVal[53] = 7578.34532;
                arrFcmVal[54] = 7547.26788;
                arrFcmVal[55] = 7517.52062;
                arrFcmVal[56] = 7489.03351;
                arrFcmVal[57] = 7461.74129;
                arrFcmVal[58] = 7435.58307;
                arrFcmVal[59] = 7410.50192;
                arrFcmVal[60] = 7386.44464;
                arrFcmVal[64] = 7291.45697;     

                if (Enumerable.Range(6,60).Contains(FcmVal(NeeNum)))
                {   fcmResult = arrFcmVal[FcmVal(NeeNum)];

                }
                else if (FcmVal(NeeNum) == 64)
                {
                    fcmResult = arrFcmVal[FcmVal(NeeNum)];
                }

                else
                {                         
                   fcmResult = -1;
                }
                return fcmResult;
            }
        }
    }

= UPDATE = 以下是使用SQL表中的数据填充文本框的完成代码:

public void FcmValue()
    {
        string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        string str = "SELECT [fcmVal] FROM [CalcTools].[dbo].[FcmValue] WHERE [neeNumCount] = @neeNumCount";
        using (SqlConnection con = new SqlConnection(ConString))
        {
            //using (SqlCommand cmd = new SqlCommand("sp_frmNeedleCalc_FcmValue", con))
            using (SqlCommand cmd = new SqlCommand(str, con))
            {
                cmd.Parameters.AddWithValue("@neeNumCount", nudNeeNum.Value);

                if (con != null && con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    string fcmFactor = rdr["fcmVal"].ToString();
                    txtFcmFactor.Text = fcmFactor;
                }
                con.Close();
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

您的方法返回一个int,fcmResult的类型为double。 Red squigly存在,因为double不能隐式转换为int。你的方法应该返回double。