Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

Below is a pseudo code which i want to realize:


function(string name)
{

Timer t+name = new Timer();
(t+name).Start();

}


My parameter name name will change. Depending upon the name of the parameter i will create an
object and use it.

In short i want to dynamically change the name of the object and use its properties.



I tried out:
Reflection : But in this we can only get the properties of an object not set them.

Used Object Class: I used object class, did typecasting, but eventually you will have to change the name of the object.... so couldnt achieve it.



Please advice,

Thanks and Regards,
Rahul
Posted

If the name of the object it is just used in your code to access the object reference, then you could use a Dictionary<string, object> for associate a custom, runtime, name to the object reference, obtaining, basically, the same effect.
 
Share this answer
 
v3
Comments
Rahul VB 4-Apr-14 8:34am    
Please look at my above solution. Will that help?
Below i have declared MyTimer class and in its constructor i have done some thing like below:
Sort of a customized timer:

C#
Timer t;
               String Name;
    public MyTimer(string name)
    {
        Name = name;
        t = new Timer();

    }

    public void StartTimer(int interval)
    {
        mydictionary.Add(t,t.GetHashCode());
        t.Interval = interval;
        t.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
        t.Enabled = true;
        t.Start();
    }

    public void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
    {

        t.AutoReset = false;
        t.Enabled = false;
        t.Stop();
    }

 }

}

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900