0

I have a DataModel class in my C# program like below.

    public class DataModel
    {
        public string num1 { get; set; }
        public string num2 { get; set; }
        public string num3 { get; set; }
        public string num4 { get; set; }
        public string num5 { get; set; }
    }

I need to do value assignment as below. The left side num1..num5 are TextBlock. The right side data.num1..data.num5 are initialized in another page already and will assign them to TextBlock's Text property. How to make it by using a for() loop? Thanks!

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (e.Parameter != null && e.Parameter is DataModel)
        {
            var data = e.Parameter as DataModel;
            string str;

            num1TextBlock.Text = data.num1;
            num2TextBlock.Text = data.num2;
            num3TextBlock.Text = data.num3;
            num4TextBlock.Text = data.num4;
            num5TextBlock.Text = data.num5;

Sorry for the unclear description. Updated, please check again. Thanks!

More: If the array count is not fixed to 5 (5 is the minimal count), for example, we pull data from our server and after that we know how much data we need to do the assignment(initializing). How to do it?

Tom Xue
  • 3,169
  • 7
  • 40
  • 77
  • 3
    your question is not clear at all. – Jack M Jul 17 '19 at 09:06
  • 1
    You cannot in a nice way. Instead you could consider using an array instead of `num1`, `num2`, `num3`... – FCin Jul 17 '19 at 09:07
  • Possibly a duplicate with [how can you loop over the properties of a class](https://stackoverflow.com/questions/4276566/how-can-you-loop-over-the-properties-of-a-class) – FeRaaC Jul 17 '19 at 09:10
  • If you put your TextBlocks into a Panel you will be able to use Panel.Children property to get them and make array based on element names. https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.panel.children#Windows_UI_Xaml_Controls_Panel_Children But better use MVVM pattern – R.Titov Jul 17 '19 at 09:19

3 Answers3

1

Place your TextBlocks into an array.

var nums = new TextBlock[] {num1, num2, num3, num4, num5 };

Refactor your DataModel to also use an array instead of five fields, like so:

public class DataModel
{
   public string[] nums {get; set;} 
} 

Then you can do:

for(int i = 0; i < data.nums.Length; i++)
{
    nums[i].Text = data.nums[i];
}

Of course you should check if the number of data fields and the number of TextBlocks match up before running the loop, to avoid any IndexOutOfRange exceptions,

pappbence96
  • 1,164
  • 2
  • 12
  • 20
0

Something like below:

    TextBlock[] blocks = new[]{num1, num2, num3, num4, num5};
    int[] values = new[]{data.num1, data.num2, data.num3, data.num4, data.num5};
    for (int i = 0; i<5;++i)
       blocks[i].Text = values[i].ToString();
user299883
  • 13
  • 3
0

You can't really loop like that in C#. You can use an array, or even better, use a Dictionary. Dictionaries map really well into javascript objects, this:

var dataModel = new Dictionary<string, string>()
{
    { "num1", "string1" },
    { "num2", "string2" },
    { "num3", "string3" },
    { "num4", "string4" },
    { "num5", "string5" },
};

would easily map into this:

{
    num1: "string1",
    num2: "string2",
    num3: "string3",
    num4: "string4",
    num5: "string5"
}

You can, however, use Reflection to get all properties of DataModel, iterate over them and match them by name with properties from another model and copy the values, but I wouldn't recommend it.