简体   繁体   English

有没有办法通过 c# 中的方法将 int 的值从一个 object 转移到同一个 class 中的另一个

[英]Is there a way to transfer a value of an int from one object to another within the same class via method in c#

    class Bank
{
    
        private String accoutNumber;
        private double credit;
        public String AccountNumber
        {
            get { return accoutNumber; }
            set { accoutNumber = value; }
        }
        public double Credit
    {
            get { return credit; }
            set { credit = value; }
        }
        

        public Bank() { }
        public Bank(String accoutNumber)
        {
            this.accoutNumber = accoutNumber;
        }
        public Bank(String accoutNumber, int credit)
        {
            this.accoutNumber = accoutNumber;
            this.credit = credit;
        }
       

        public void addBalance(int ammount) { credit += ammount; }
        public void vyber(int ammount)
        {
            if (ammount > credit)
                return;
            credit -= ammount;
        }
        public void transfer(int ammount, String accoutNumber)
        {
            if (ammount > credit)
                return;
            //else transfer from one object to another
        }

}

(in another file i am using the transfer method to transfer credit from one object to another, i just don't know how to do that, also i am not using any files for database, it is as simple as it could possible be) (在另一个文件中,我使用转移方法将信用从一个 object 转移到另一个文件,我只是不知道该怎么做,而且我没有使用任何数据库文件,它尽可能简单)

    Bank object1 = new Bank("1234567890/1234", 10000);
    Bank object2 = new Bank("7845213154/1448", 7000);
    object1.transfer("7845213154/1448", 2000)
    //object1's credit = 8000
    //object2's credit = 9000 

It sounds like what you're trying to do here is:听起来您在这里尝试做的是:

  • you have multiple objects of some type (perhaps called Account ), each of which has an identity defined by accountNumber您有多个某种类型的对象(可能称为Account ),每个对象都有一个由accountNumber定义的身份
  • when transferring funds, you want to look up a different account by number, and access that转移资金时,您想按号码查找不同的帐户,然后访问帐户

Now: there is no automatic pre-build index of objects by their accountNumber .现在:对象的accountNumber没有自动的预构建索引。 That is something you would need to add separately .这是您需要单独添加的内容 For example, you might have a Dictionary<string, Account> , that you add each instance to:例如,您可能有一个Dictionary<string, Account> ,您可以将每个实例添加到:

var foo = new Account { accoutNumber = "12391", Credit = 420 };
var bar = new Account { accoutNumber = "58u98:a24", Credit = 9000 };

var accounts = new Dictionary<string, Account>();
accounts.Add(foo.accoutNumber , foo);
accounts.Add(bar.accoutNumber , bar);
// etc

Now, we can obtain accounts by their identity:现在,我们可以通过他们的身份来获取账户:

if (!accounts.TryGetValue(someAccountId, out var someAccount))
{
    throw new KeyNotFoundException("Target bank account not found");
}
// here, someAccount is the one we want by someAccountId
someAccount.DoSomething();

This, however, presents a problem;然而,这带来了一个问题。 you wouldn't normally expect each individual Account object to keep hold of the entire set of accounts;您通常不会期望每个单独的Account object 保留整个账户集; you could have some account management type (which maintains the dictionary) perform both lookups and perform both deltas:可以让一些帐户管理类型(维护字典)执行两个查找并执行两个增量:

if (!accounts.TryGetValue(fromId, out var from))
{
    throw new KeyNotFoundException("Source bank account not found");
}
if (!accounts.TryGetValue(toId, out var to))
{
    throw new KeyNotFoundException("Destination bank account not found");
}
if (from.Balance < amount)
{
    throw new InvalidOperationException("Insufficient funds in source account");
}
from.AddBalance(-amount);
to.AddBalance(amount);

Note that this is not thread-safe.请注意,这不是线程安全的。 I'm guessing threading isn't a concern for what you're doing.我猜线程不是你在做什么的问题。

The alternative would be to pass in the accounts lookup to your method.另一种方法是将帐户查找传递给您的方法。 This could the dictionary itself, or some helper service:这可以是字典本身,也可以是一些辅助服务:

object1.Transfer("7845213154/1448", 2000, accounts);

and have the Transfer method do the lookup internally, after whatever validation you need:并在您需要的任何验证之后让Transfer方法在内部进行查找:

public void Transfer(decimal amount, string accountNumber, Dictionary<string, Account> accounts)
{
    if (!accounts.TryGetValue(accountNumber, out var to))
    {
        throw new KeyNotFoundException("Destination bank account not found");
    }
    if (amount > credit)
    {
        throw new InvalidOperationException("Insufficient funds in source account");
    }
    AddBalance(-amount);
    to.AddBalance(amount);
}

Again, this is not thread-safe in any way, and is not transactional etc.同样,这在任何方面都不是线程安全的,也不是事务性的等。

Would go something like this (refine some rules here and there about validation of account numbers and such). go 是否会像这样(在这里和那里完善一些关于验证帐号等的规则)。

public class Bank
{
    private static global::System.Collections.Generic.Dictionary<string, global::Bank> accounts = new System.Collections.Generic.Dictionary<string, global::Bank>(10000);
    private string accountNumber;
    private decimal credit;
    public string AccountNumber { get { return this.accountNumber; } }
    public decimal Credit { get { return this.credit; } }
    public void addBalance(decimal ammount) { this.credit += ammount; }
    public void vyber(int ammount) { if (ammount <= credit) { this.credit -= ammount; } }

    public static global::Bank GetAccount(string number)
    {
        if (string.IsNullOrEmpty(number)) { return null; }
        else
        {
            global::Bank bank = global::Bank.accounts[number];
            if (bank == null) { bank = new global::Bank(number); }
            return bank;
        }
    }

    public void transfer(decimal ammount, string number)
    {
        if (ammount <= credit && number != this.accountNumber)
        {
            global::Bank bank = global::Bank.GetAccount(number);
            if (bank == null) { throw new global::System.ArgumentException("Not Found"); }
            else
            {
                this.credit -= ammount;
                bank.addBalance(ammount);
            }
        }
    }

    private Bank(string number) { this.accountNumber = number; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# - 在同一类中的另一个方法中调用一个方法 - C# - calling one method inside another within the same class 如何从同一 class C# 中的另一种方法访问一种方法的变量? - How do I access the variable of one method from another method within the same class C#? 如何将一个对象从一个方法初始化为一个类中的另一个方法? - How to initialize an object from one method to another method within a class? C#如何在另一个类中更改一个int变量 - C# How to change an int variable from one class in another c# 根据另一个属性的相同值从另一个中减去一个json的属性int值 - c# Subtract one json's property int value from another based on another property's same value 如何在C#中的另一个方法中从同一类中调用一个方法? - How to call a method from same class in another method in C#? 有没有一种快速的方法可以在C#中将一个相同对象的所有变量转换为另一个变量? - Is there a fast way to transfer all the variables of one identical object into another in C#? 在同一 class -c# 中使用来自另一个方法的变量 - Using a variables from another method in the same class -c# C#:如何对依赖于同一类中的另一个方法的方法进行单元测试? - C#: How to unit test a method that relies on another method within the same class? 使用C#在Windows中将值从一个用户控件转移到Windows中的另一个用户控件 - Transfer value from one user control to another user control in windows from application using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM