robertpas
robertpas

Reputation: 673

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). 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

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) + "%      ");

    }

}

Upvotes: 0

Views: 1394

Answers (1)

Chris Gessler
Chris Gessler

Reputation: 23113

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.

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;
}

Upvotes: 2

Related Questions