Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Setter不';我根本不工作。它没有';不要调用任何代码_C#_.net_Properties_Setter - Fatal编程技术网

C#Setter不';我根本不工作。它没有';不要调用任何代码

C#Setter不';我根本不工作。它没有';不要调用任何代码,c#,.net,properties,setter,C#,.net,Properties,Setter,我试图在下面的代码中更改美分。但是我已经被困了将近一个小时了。我就是换不了这几分钱。美分里面的Setter根本不起作用。它不会改变值,甚至当我创建Console.WriteLine(“它是用美分setter编写的”)它也不会被调用 using System; using System.Collections; using System.Collections.Generic; class Program { static void Main(string[] args

我试图在下面的代码中更改
美分。但是我已经被困了将近一个小时了。我就是换不了这几分钱。美分里面的Setter根本不起作用。它不会改变值,甚至当我创建
Console.WriteLine(“它是用美分setter编写的”)它也不会被调用

using System;
using System.Collections;
using System.Collections.Generic;
class Program
    {


        static void Main(string[] args)
        {
        BankAccount a = new BankAccount(50,200);
        Console.WriteLine(a.Cents);

        Console.ReadLine();
        }
    }


class BankAccount
{
    public BankAccount(uint dollars,uint cents)
    {
        this.Dollars = dollars;
        this.cents = cents;
    }
    public uint Dollars { get; private set;}
    private uint cents;
    public uint Cents
    {
        get 
        {
            return cents;
        }
        private set
        {
            cents = 500; //It doesnt change
            Console.WriteLine("It's been written in Cents setter"); // I can't invoke it
        }
    }
}

您将备份字段
cents
与属性
cents
混淆。在
BankAccount
的构造函数中,将
cents
更改为
cents
,然后将调用setter


同样在setter中,您可能想说
cents=value

这是正确的实现

using System;

class BankAccount
{
    private uint _dollars;
    private uint _cents;

    public BankAccount(uint dollars, uint cents)
    {
        this._dollars = dollars;
        this._cents = cents;
    }

    // default constructor
    public BankAccount()
    {
        this._dollars = 0;
        this._cents = 0;
    }

    public uint Cents
    {
        get
        {
            return _cents;
        }
        set
        {
            _cents = value;
        }
    }

    public uint Dollars
    {
        get
        {
            return _dollars;
        }
        set
        {
            _dollars = value;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        BankAccount account1 = new BankAccount(100, 5);
        Console.WriteLine($"There are {account1.Dollars} dollars and {account1.Cents} cents on your bank account");

        account1.Dollars = 200;
        account1.Cents = 28;

        Console.WriteLine($"There are {account1.Dollars} dollars and {account1.Cents} cents on your bank account"); 
    }
}

您没有在这里任何地方调用setter。您可能是指
this.Cents=Cents@KlausGütter。是的,你说得对。我很惭愧,我几乎一个小时没看到它。谢谢。如果这是你的目标,那么为什么你有一个以美分为输入的构造函数呢?史蒂夫:你可以自己用美元计算美分,我想你不明白我的意思。我不想把美分换成美元,反之亦然。我只想要一个美元和美分的账户。例如,14美元250美分的账户=16美元50美分。幸运的是,我找到了解决办法。