简体   繁体   English

如何在C#中为变量添加“char”

[英]How to add a "char" to variable in C#

Yo, do you guys have any idea on how to add something to variable name/id?哟,你们知道如何向变量名/id 添加一些东西吗? I can't really explain that so I'll just paste my code and explain what I want to do, or maybe you have a better idea to how to resolve that.我真的无法解释,所以我只是粘贴我的代码并解释我想要做什么,或者您可能对如何解决这个问题有更好的想法。

int[] array = new int[9];
            for (int i=0; i < 9; i++)
            {
                j=i+1.ToString();
                FindViewById<Button>(Resource.Id.***Num1***).Click += (o,e)=> 
                {
                    Result.Text += "j"; 
                };          
            }    

I've got 9 buttons and all of them are numbered from 1 to 9. I wan't to create an array to avoid many lines of useless code.我有 9 个按钮,所有按钮的编号都是从 1 到 9。我不想创建一个数组来避免许多无用的代码行。 So the question is: how to add a number ( or a string ) to this "Num" id, so in every iteration there will be another slot of array fulled with another button click event.所以问题是:如何向这个“Num”id 添加一个数字(或一个字符串),所以在每次迭代中,都会有另一个数组槽充满另一个按钮点击事件。 Is it even possible to do this this way?甚至有可能这样做吗? Thanks for an answer :>.感谢您的回答:>。

If you want to get a resource identifier from its name, you need to use Resources.GetIdentifier() .如果您想从名称中获取资源标识符,则需要使用Resources.GetIdentifier() For example, assuming the ID of your button is Num5 :例如,假设您的按钮的 ID 是Num5

var buttonNumber = 5;
var resourceId = Resources.GetIdentifier($"Num{buttonNumber}", nameof(Resource.Id).ToLower(), _activity.PackageName);
var button = FindViewById<Button>(resourceId);

I hope it helps!我希望它有帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM