变量不包含值

时间:2018-06-24 08:31:47

标签: c#

我要创建的程序有问题。这个程序就像一张信用卡,问题在于我放入'Credit'变量中的金额似乎根本不会影响该变量,而是保持空白。 以下是信用卡类:

我已经编辑了代码,因为我刚刚注意到我没有插入所有内容,并且还包含了program.cs!该程序运行时的结果:https://prnt.sc/jynpt1

select  * from
(
    select unnest(name) as name_in_array, id from 
    (
    select 1 as id, ARRAY['value1','valu','lav'] as name
    union all
    select 2 as id, ARRAY['value2','orange','yellow'] as name
    )t1
) t2
where levenshtein(name_in_array, 'value') < 2;

Program.cs

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

namespace CreditCardExample
{
    public class CreditCard
    {
            private double credit;
            public double Credit
            {
                get { return credit; }
                set { credit = value; }
            }

            public CreditCard(double credit)
            {
                this.credit = Credit;
            }

            public bool enoughCredit (double creditAmt)
            {
                bool state = false;
                if (Credit >= creditAmt)
                {
                    Console.WriteLine("You have sufficient funds.");
                    state = true;
                }
                else
                {
                    Console.WriteLine("You do not have sufficient amount");
                    state = false;
                }

                return state;
            }
        public double getAmtInCreditCard()
        {
            Console.WriteLine("Amount of Money in your CreditCard: ", Credit);
            return Credit;
        }
    }
}

}

2 个答案:

答案 0 :(得分:6)

这是因为在构造函数中,您是将属性分配回自身,而不是要传递的值。

this.credit = Credit // notice the casing?

更改为

this.credit = credit;

答案 1 :(得分:3)

您的代码中有错误。

 public CreditCard(double credit)
 {
      Credit = credit;
 }

测试:

class CreditCard
{
    private double credit;
    public double Credit
    {
        get { return credit; }
        set { credit = value; }
    }

    public CreditCard(double credit)
    {
        Credit = credit;
    }
}

static void Main()
{
    //Outputs "10"
    Console.WriteLine(new CreditCard(10).Credit);
}