[英]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:听起来您在这里尝试做的是:
Account
), each of which has an identity defined by accountNumber
您有多个某种类型的对象(可能称为Account
),每个对象都有一个由accountNumber
定义的身份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.