Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I am new to C#, and need a little help, i know in PHP that i can to something like this
$SelectedEvent="event selected";
if (i > 0)
{
$SelectedEvent = "event";
}
It replace a string easy, but in c# that is not working ok, here is example code i have that is not working
string SelectedEvent = "event selected";
if (i > 0)
{
SelectedEvent = "event";
}
This is not working like in PHP, i can not override the varible?
Here is my example code
EDITED
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
string SelectedEvent = "event";
}
}
You should not redeclare the variable twice but only modify its value:
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
SelectedEvent = "event";
}
// here you can use the SelectedEventVariable
// its value will be "event selected" on the first and second
// iterations and "event" on subsequent
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Ran into the notation:
for (var i = 0; i < count; i++) { }
on this tutorial. I gather it is equivalent to:
for(var i = 0; i < count; i++) { }
What is this notation? Why use it? Does it perform better?
It's an HTML parsing error. < is supposed to show up as <. You're right - it's supposed to be i < count.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Quick question:
I am writing in unity c#, and have an array containing several instances of the same object. I want to change a non-static bool in every member of the array, and set it to true. How would I do this?
I have played around with for-loops, and searched both here and at the unity forums, but cannot find an answer. I don't believe my messy, faulty code would be of any help. Anybody care to enlighten me? :)
EDIT: Thanks for the tips, will try them now. The code was asked for, excuse the mess:
Component[] toMerge;
for (int t = 0; t < mergeTargets.Length; i++) {
toMerge[t] = mergeTargets[t].gameObject.GetComponent<Enemy>();
toMerge.readyToMerge = true;
}
for (int t = 0; t < toMerge.Length; t++) {
toMerge[t].readyToMerge = true;
}
You have to allocate mergeTargets.Length items in toMerge array and address not the array but its item:
//DONE: We need mergeTargets.Length items in toMerge
Component[] toMerge = new Component[mergeTargets.Length];
for (int t = 0; t < mergeTargets.Length; i++) {
toMerge[t] = mergeTargets[t].gameObject.GetComponent<Enemy>();
//DONE: [t] - we want to change item, not the array
toMerge[t].readyToMerge = true;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have problem with for loop in C#.
I have the following code, in main method:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for(int i=0;i>20;i++)
{
if(i%2==0)
{
Console.WriteLine("{0},", i);
}
}
Console.ReadLine();
}
}
}
The for loop does not execute. Why?
Change condition, > to <:
...
for (int i = 0; i < 20; i++) // i < 20, not i > 20
...
In your original code you assign int i = 0 then check i > 20 get false and do not enter the loop at all
The following statement in your code:
for (int i = 0; i > 20; i++)
First assigns 0 to i then evaluates the condition i > 20 which is wrong, so the for block can not be executed.
Other people have pointed out the fact that the test should actually be "i < 20" rather than "i > 20) but just to make one other point: if all you're trying to do is write out all the even numbers, you can actually increment "i" by 2 every time - that way you never have to test to see if it's even (just write "i += 2" instead of "i++").
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The below code is giving me error "Cannot assign to 'writeline' because it is a 'method group;" at every Console.Writeline statement. I am trying to find out why but I could not find anything to point me in right direction. Any help appreciated.
{
class FizzBuzz
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
if ((i % 3 == 0) && (i % 5 == 0))
{
Console.WriteLine = "Fizzbuzz" ;
}
else if (i % 3 == 0)
{
Console.WriteLine = "Fizz";
}
else
(i % 5 == 0)
{
Console.WriteLine = "Buzz";
}
System.Console.ReadLine();
}
}
}
You need to set the string in parenthesis:
Console.WriteLine("This string goes to console.");
You tried to assign the method with a value, that is not possible. That works only with properties and fields.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I don't understand how to do this im new to programming but im sure its simple. help the code I have so far is:
I need the asterisks to be separating the numbers
for (int i = 0; i < 101; i++)
Console.WriteLine(i);
Console.WriteLine("********");
but the asterisks is only showing at the bottom
You should put the body of the loop into a scope:
for (int i = 0; i < 101; i++)
{
Console.WriteLine(i);
Console.WriteLine("********");
}
Otherwise only the following next command is interpreted as the body of the loop, equivalent to this:
for (int i = 0; i < 101; i++)
{
Console.WriteLine(i);
}
Console.WriteLine("********");
The way you have it is only doing the for loop on the first line. You need to put both lines into a 'block' using curly braces:
for (int i = 0; i < 101; i++)
{
Console.WriteLine(i);
Console.WriteLine("********");
}