0

The docs from Microsoft of c# defines for as:

for (initializer; condition; iterator)
    body

Is there any way to use a string as an initializer, condition or iterator?

Such as:

String ini = "int i = 0";
for(ini; condition; iterator){
   //do something
}
rktech
  • 28
  • 1
  • 10
  • Did you try? Reading C# specification should be possible... Even though it is very weird :) – Johnny Mar 17 '20 at 21:36
  • string n = "int i = 0";for (n; "i<9"; "i++"){}First one drops CS0201, second CS0029 and third CS0201 errors – rktech Mar 17 '20 at 21:47
  • 2
    Short answer... No. I'm curious about your intended use case – Flydog57 Mar 17 '20 at 21:50
  • Just interpreter, that I want to design in c# to future rewrite it to Arduino c++ – rktech Mar 17 '20 at 22:06
  • The is answer is no... then why would you anyways? if you think about it code is just a string..... so just read the entire code file and parse it how ever you see fit. I generate code files for all kinds of purposes. – Jonathan Alfaro Mar 17 '20 at 23:12

2 Answers2

2

Update

You can't use a string like this "int i = 0" on a for loop, the compiler can't read your mind.

You will likely have to rethink your problem, or parse the string for the information you need.


Original

Is there any way to use a string as an initializer, condition or iterator?

for (string asd = ""; asd != string.Empty; asd += "b")

Note : I'm not sure if I have understood the question, or what your use case is, but you can use a string for all the above. The question is whether you should...

It is common to use a for to iterate a pointer though:

for (char* p = pSomeValue; p < someAddress; p++)
    *p = '?'
halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1

Simple answer is NO.Please go through Microsoft documentation for "for" loop

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/for

But if the initializing and conditions are coming in string format, you need to extract the values and you need assign in the for loop.

Or if the whole logic is coming as string use reflection.

Chandu
  • 322
  • 4
  • 13