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?

4
  • 3
    your question is not clear at all. Commented Jul 17, 2019 at 9:06
  • 1
    You cannot in a nice way. Instead you could consider using an array instead of num1, num2, num3... Commented Jul 17, 2019 at 9:07
  • Possibly a duplicate with how can you loop over the properties of a class Commented Jul 17, 2019 at 9: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. learn.microsoft.com/en-us/uwp/api/… But better use MVVM pattern Commented Jul 17, 2019 at 9:19

3 Answers 3

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,

Sign up to request clarification or add additional context in comments.

Comments

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();

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.