英文:
Part of variable name has to be dynamic
问题
I want dynamicVariable
below to hold different name each time:
我希望 dynamicVariable
每次都能保存不同的名称。
Data.cs:
数据.cs:
namespace ProjectName.Shared.Price
{
public class Price
{
public static readonly Tuple<double?, double?> applePrice = new Tuple<double?, double?>(2, 10);
public static readonly Tuple<double?, double?> bananaPrice = new Tuple<double?, double?>(1, 2);
public static readonly Tuple<double?, double?> orangePrice = new Tuple<double?, double?>(2, 4);
}
}
ItemComponent.cs:
项目组件.cs:
using ProjectName.Shared.Price
// I will be getting ID
as "applePrice", "bananaPrice" or "orangePrice" dynamically
// 动态地获取ID
,可能是 "applePrice"、"bananaPrice" 或 "orangePrice"
Id = dynamicVariable
// how to substitute ID below with dynamic ID's based on what we receive
// 如何根据接收到的内容将下面的ID替换为动态ID
Min = Price.ID.Item1
Max = Price.ID.Item2
If Id passed was "bananaPrice", I expect ID to hold "bananaPrice". So, it will become
如果传递的ID是 "bananaPrice",我希望ID保持为 "bananaPrice"。因此,它将变成
Min = Price.bananaPrice.Item1 // so that value received would be 1 (from Data.cs)
Max = Price.bananaPrice.Item2 // value received would be 2
如果传递的ID是 "bananaPrice",我希望ID保持为 "bananaPrice"。因此,它将变成
Min = Price.bananaPrice.Item1 // 这样接收到的值将为1(来自Data.cs)
Max = Price.bananaPrice.Item2 // 接收到的值将为2
May I know how I can achieve this. Thank you.
我可以知道如何实现这一点吗?谢谢。
英文:
I want dynamicVariable
below to hold different name each time
Price.dynamicVariable.Item1
Data.cs:
namespace ProjectName.Shared.Price
{
public class Price
{
public static readonly Tuple<double?, double?> applePrice = new Tuple<double?, double?>(2, 10);
public static readonly Tuple<double?, double?> bananaPrice = new Tuple<double?, double?>(1, 2);
public static readonly Tuple<double?, double?> orangePrice = new Tuple<double?, double?>(2, 4);
}
}
ItemComponent.cs:
using ProjectName.Shared.Price
// I will be getting `ID` as "applePrice", "bananaPrice" or "orangePrice" dynamically
Id = dynamicVariable
// how to substitute ID below with dynamic ID's based on what we receive
Min = Price.ID.Item1
Max = Price.ID.Item2
If Id passed was "bananaPrice", I expect ID to hold "bananaPrice".So, it will become
Min = Price.bananaPrice.Item1 // so that value received would be 1 (from Data.cs)
Max = Price.bananaPrice.Item2 // value received would be 2
May I know how I can achieve this. Thank you.
答案1
得分: 0
做类似这样的事情的方法是使代码更抽象,这样您就不必与任何“动态”内容纠缠。例如,使用字典和枚举:
public enum Fruit { banana, apple, orange }
public Dictionary<Fruit, (decimal? Min, decimal? Max)> Price = new () {...}
public double? GetMinPrice(Fruit fruit) => Price[fruit].Min;
如果您需要将字符串转换为水果,请参阅 enum.TryParse。对于更严重的用例,您应考虑使用数据库。
英文:
The way to do things like this is to make the code more abstract so you do not have to fiddle around with anything 'dynamic'. For example by using a dictionary and a enum:
public enum Fruit{banana, apple, orange}
public Dictionary<Fruit, (decimal? Min, decimal? Max)> Price = new (){...}
public double? GetMinPrice(Fruit fruit) => Price[fruit].Min;
If you need to convert a string to a fruit, see enum.TryParse. For more serious use cases you should consider using a database.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论