变量名的一部分需要是动态的。

huangapple go评论60阅读模式
英文:

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&lt;double?, double?&gt; applePrice = new Tuple&lt;double?, double?&gt;(2, 10);

            public static readonly Tuple&lt;double?, double?&gt; bananaPrice = new Tuple&lt;double?, double?&gt;(1, 2);

           public static readonly Tuple&lt;double?, double?&gt; orangePrice = new Tuple&lt;double?, double?&gt;(2, 4);

    }

}

ItemComponent.cs:

using ProjectName.Shared.Price   

// I will be getting `ID` as &quot;applePrice&quot;, &quot;bananaPrice&quot; or &quot;orangePrice&quot; dynamically
Id = dynamicVariable  
    
    // how to substitute ID below with dynamic ID&#39;s based on what we receive
    Min = Price.ID.Item1 
    Max = Price.ID.Item2
    
If Id passed was &quot;bananaPrice&quot;, I expect ID to hold &quot;bananaPrice&quot;.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&lt;Fruit, (decimal? Min, decimal? Max)&gt; Price = new (){...}

public double? GetMinPrice(Fruit fruit) =&gt; 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.

huangapple
  • 本文由 发表于 2023年6月8日 12:56:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定