locked
Need to Create N number of Feild's @ runtime RRS feed

  • Question

  • Need to Create N number of Feild's @ runtime

      public partial class MainWindow : Window
    {
      public Driver A1;
      public Driver A2;
      public Driver A3;
    }
    
    

    My Problem here is when I Click a Button Which Contains a For loop I need Exact Number of Fields

    Example :

    Button Click :

    int[] Test = new int[] { 0, 1, 1, 2, 3 }; //Example Only (Comes from Different Source)
    
    Test Comes from External Source...So there is No Exact Count.
    
    foreach (int element in Test)
    {
        System.Console.WriteLine(element);
    }
    

    Test Count Comes @ runtime.....I need the same Number of Fields Generated (i.e Test Count);

    Example : Test Count 4

    public Driver A1;
    public Driver A2;
    public Driver A3;
    public Driver A4;

    Thursday, March 15, 2018 7:56 AM

Answers

  • Normally you don't need that. Especially you don't want this in your forms. Cause this would lead to untested code. *yuck*.

    Use a List<> or a Dictionary<>:

    public class Drivers
    {
    	private List<Driver> drivers = new List<Driver>();
    	
    	public List<Drivers> Drivers{
    		get {
    			// ToDo: Generate on demand a clone for an immutable list.
    			return this.drivers;
    		}
    	}
    	
    	public Drivers(int[] numbers)
    	{
    		foreach (int element in numbers)
    		{
    			this.drivers.Add(new Driver());
    		}
    	}
    }

    • Marked as answer by ID GO Thursday, March 15, 2018 1:21 PM
    Thursday, March 15, 2018 9:48 AM