简体   繁体   English

C#在使用构造函数后向实例添加值

[英]C# adding values to an instance after using a constructor

Can I add values to an instance of an object after I have used the constructor? 在使用构造函数后,是否可以向对象的实例添加值?

For instance I have this code. 例如,我有这个代码。 I have to make a list of objects which require a number n, and the time (which are recieved as arguments). 我必须列出需要数字n的对象和时间(作为参数接收)。 The problem is that the time can be anywhere so I can't use it in a constructor. 问题是时间可以在任何地方,所以我不能在构造函数中使用它。

 public List<IAction> Dispatch(string[] arg)
   {
       int time;
       int i = 0;
       int j = 0;
       List<IAction> t = new List<IAction>(10);
       do
       {
           if (int.Parse(arg[j]) >= 0 && int.Parse(arg[j]) <= 20)
           {
               t.Add(new ComputeParam(int.Parse(arg[j]))); 

               i++;
               j++;

           }
           else
           {
               if (arg[i][0] == '/' && arg[i][1] == 't')
               {
                   Options opt = new Options();
                   j++;

                   time=opt.Option(arg[i]); //sets the time 0,1000 or 2000


               }


           }
       } while (i != arg.Length);
       return t;

} }

After finishing making the list can I do something like: 完成列表后,我可以这样做:

for(int i=0; i<=t.Count; i++) { *add time to t[i]* }

How do I do this? 我该怎么做呢?

Thanks in advance! 提前致谢!

Edit : 编辑:

here is the ComputeParam class 这是ComputeParam类

public class ComputeParam : IAction
{
    int n;
    int time;
    public ComputeParam()
    {
    }
    public ComputeParam(int n)
    {
        this.n = n;
    }

    public void run()
    {


        Factorial Fact = new Factorial();
        Fact.Progression += new Factorial.ProgressEventHandler(Progress);
        Console.WriteLine("                                        ");
        Console.Write("\n" + "Partial results : ");
        Console.CursorLeft = 35;
        Console.Write("Progress : ");
        int Result = Fact.CalculateFactorial(n, time);
        Console.WriteLine(" ");
        Console.WriteLine("The factorial of " + n + " is : " + Result);

        Console.WriteLine("Press Enter to continue...");
        Console.CursorTop -= 2;
        Console.CursorLeft = 0;

        Console.ReadLine();
    }

    public void Progress(ProgressEventArgs e)
    {
        int result = e.getPartialResult;
        int stack_value = e.getValue;
        double max = System.Convert.ToDouble(n);
        System.Convert.ToDouble(stack_value);
        double percent = (stack_value / max) * 100;

        Console.CursorLeft = 18;
        Console.Write(result + " ");
        Console.CursorLeft = 46;
        Console.Write(System.Convert.ToInt32(percent) + "%      ");

    }

}

If the object has a public property, I don't see why not. 如果对象有公共属性,我不明白为什么不。
Edit : Looks like you need to add a public property to your class. 编辑 :看起来您需要向您的班级添加公共属性。 Also note, given that there is a public constructor that takes 0 params, you should also add a property for n. 另请注意,假设有一个公共构造函数需要0个参数,您还应该为n添加一个属性。

public class ComputeParam : IAction    
{    
    int _n;    
    int _time;    
    public ComputeParam()    
    {    
    }    
    public ComputeParam(int n)    
    {    
        this._n = n;    
    }  
    public int Time 
    { 
        get { return this._time; }
        set { this._time = value; }
    }

    public int N
    {
        get { return this._n; }
        set { this._n = value; }
    }


for(int i = 0; i < t.Count; i++) 
{ 
    ((ComputeParam)t[i]).Time = 6;
}

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

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