Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Generate variables suing for loop

C# for loop loop

  • Please log in to reply
3 replies to this topic

#1 JakeWelton

JakeWelton

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 152 posts
  • Programming Language:Java, C++, C#, PHP, JavaScript, PL/SQL
  • Learning:Objective-C, Python, Perl, Ruby, Visual Basic .NET, Haskell

Posted 06 September 2012 - 02:45 AM

Hello all,

Is there a way to generate variables using a for loop. For example if you got the loop to loop 5 times then you would have 5 new variables declared with names such as variable1, variable2 etc.

Can this be done in c#??

Thanks,
Jake
  • 0

sig.jpg

"Before you judge someone, walk a mile in their shoes; after that, who cares?! Their a mile away and you've got their shoes!"


#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 06 September 2012 - 06:46 AM

Yes, there is a way to do it, but it would be a workaround. Why would you want to? It's not a conventional thing to do.

Another option is to use a dictionary, and fill it in the loop.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 06 September 2012 - 11:55 AM

Yes, just as lespauled said, it would not be a saint practice of coding, anyway have in mind that variables created normally into loops lives only inside that loops, so at some point you need to declare them which means you have to know how much of them beforehand.

Why not using a array ? a Dictionary ? or keyed collection, they are all variable in size and you can call them by array position or string name, like : vars["Variable3"]

i think that is the way to go
  • 0

#4 JakeWelton

JakeWelton

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 152 posts
  • Programming Language:Java, C++, C#, PHP, JavaScript, PL/SQL
  • Learning:Objective-C, Python, Perl, Ruby, Visual Basic .NET, Haskell

Posted 07 September 2012 - 08:58 AM

Thanks for the replies. I finally got around my problem by using an array. Looping through using the elements in the array rather than creating a new variable for each one. Thanks for all the help!!

Thanks again,
Jake
  • 0

sig.jpg

"Before you judge someone, walk a mile in their shoes; after that, who cares?! Their a mile away and you've got their shoes!"






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download