Click here to Skip to main content
16,022,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

For example i have this :
private Stopwatch stopwatch;


i want to make 10 stopwatch property using for loop or anyother loop

private Stopwatch stopwatch1;

private Stopwatch stopwatch2;

private Stopwatch stopwatch3;


any suggestion?

ps :
 i want to make each stopwatch by specific name to use eachone in diffrent places 
so i mean is it possible to do it like this : 


for (int i = 0; i < 10; ++i)
            {
        private Stopwatch stopwatch+i;

        i = i + 1 ;
            }
i want something like this


What I have tried:

...................................................
Posted
Updated 13-Apr-22 5:41am
v2

Create a List<T> of Stopwatch'es:
C#
private List<Stopwatch> stopwatches = new List<Stopwatch>();
for (int i = 0; i < 10; ++i)
{
    stopwatches.Add(new Stopwatch());
}
 
Share this answer
 
v2
Comments
Ashkan X 13-Apr-22 11:40am    
Thanks for your help but i want to make each stopwatch by specific name to use eachone in diffrent places
so i mean is it possible to do it like this :


for (int i = 0; i < 10; ++i)
{
private Stopwatch stopwatch+i;

i = i + 1 ;
}
i want something like this
[no name] 13-Apr-22 11:43am    
As far as I am aware you cannot do it like that. But it is not clear what you are trying to do or why you would need this.
Quote:
how can i use it now ?
Like i used stopwatch = new Stopwatch(); but with this loop how ?


That's going to depend on exactly what you are trying to do - and we have no idea what that is!
So sit down and think about the problem you are trying to solve before you decide what to code: creating a bunch of stopwatches is one thing, but you need to consider what you are going to use them for rather than a simple "how do I do it?"

Certainly, creating ten stopwatches in a property that returns a single one probably isn't what you want to do at all!
 
Share this answer
 
Comments
Ashkan X 13-Apr-22 11:43am    
Thanks this problem solved but i need other way i improved my question
thanks for your time

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