hello im new to programing with c# and i wanted to re-create an old proyect i made in scratch but with c#
but i have several problems with the code i wrote.
int qny = 4;
int ADD = 1;
int B = 1;
string C = " ";
for (int i = 0; i < qny; i++) ;
{
int R = qny + ADD;
for (int i = 0; i < R; i++) ;
{
string C = string.Join(C, B);
}
int qny = qny - 1;
int B = B + 1;
}
Console.WriteLine(C);
the main problem is that i cant use the variables "qny","B" and "C" inside the "for". the only one that doesnt show an error message is the "ADD" variable
its suposed to write numbers like this
qny
result
2
112
3
111223
4
1111222334
errors:
Error CS0841 Cannot use local variable 'qny' before it is declared. A variable must be declared before it is used.
Error CS0136 A local variable named 'C' cannot be declared in this scope because it would give a different meaning to 'C', which is already used in a 'parent or current/child' scope to denote something else
Error CS0165 Use of unassigned local variable 'C'
Error CS0841 Cannot use local variable 'B' before it is declared.
Error CS0136 A local variable named 'qny' cannot be declared in this scope because it would give a different meaning to 'qny', which is already used in a 'parent or current/child' scope to denote something else
Error CS0136 A local variable named 'B' cannot be declared in this scope because it would give a different meaning to 'B', which is already used in a 'parent or current/child' scope to denote something else
Your for loops end with a semicolon, which ends the loop. The following code blocks are not treated as part of the loop body because of that. Additionally as aybe mentioned you use i for both loops which will update the same variable.
EDIT: As mentioned below you also shouldn't redeclare the variable C as it won't update the variable C at the top, same reason as to why you don't want to use i for both loops.
EDIT: Code below still has redeclared variables, check author's answer for updated code.
I suggest changing it to this:
int qny = 4;
int ADD = 1;
int B = 1;
string C = " ";
for (int i = 0; i < qny; i++)
{
int R = qny + ADD;
for (int j = 0; j < R; j++)
{
C = string.Join(C, B);
}
int qny = qny - 1;
int B = B + 1;
}
Console.WriteLine(C);
To avoid such errors in the future I would recommend checking out some tutorials on the basics of C#. I wish you good luck on your coding journey!
Thanks to the people that helped me. As someone suggested, I’ll check out some tutorials. The final code looks like this:
int qny = 4;
int ADD = 1;
int B = 1;
string C = " ";
for (int i = 0; i < qny; i++)
{
int R = qny + ADD;
for (int j = 0; j < R; j++)
{
C = string.Join(C, B);
}
qny = qny - 1;
B = B + 1;
}
Console.WriteLine(C);
Related
This question already has answers here:
Create dynamic variable name
(5 answers)
Closed last year.
Ok this might be a weird question and I don't really know how to phrase it so I just show you an example:
int i = 0;
int k = 100;
while (i <5) {
***response+i*** = k + i;
i++;
}
I want to declare multiple Variables (response1, response2, respons3 ...) using a loop, so that in the end the result is:
response1 = 101
response2 = 102
response3 = 103
etc.
I am still a beginner at C# and programming in general, so I don't know if that is even possible how I imagine it, hope you can help me, thanks.
First, You can not define a variable by using two variable. That is not how compiler work.
Maybe you should try to create a Array or List to store the value
like this example
int i = 0;
int k = 100;
int[] response = new int[100];
while (i < 5)
{
response[i] = k + i;
i++
}
if you don't know the array size you want , try to do with List
List<int> response = new List<int>();
int i = 0;
int k = 100;
while (i < 5)
{
response.Add(i + k);
i++;
}
You should really use an array for these values, it's not really possible to use dynamic variable names.
I would also use a for loop where you need the index variable rather than having to manually update it in the while loop.
var results = int[5];
int k = 100;
for (var i = 0; i < results.Length; i++)
results[i] = k + i;
int a, b, c;
a = 1;
b = 2;
c = a + 2*b;
for (a=1; c<15; a++)
{
Console.WriteLine(c.ToString());
}
Console.ReadLine();
I have a simple question. Do I have to define my condition depending on my initializing variable?
In the example above, I want the loop to stop when a variable "a" becomes 10, so variable "c" will be less than 15.
But the output is infinite lines of "5". I expect my output to be 14.
what am I doing wrong?
move c change inside of the loop, or you will itereate without end:
for (a=1; c<15; a++)
{
c = a + 2*b;
Console.WriteLine(c.ToString());
}
If you really wanted you could omit all 3 parts of a for loop so it essentially becomes a while(true) loop
for(;;)
So no, it doesn't need to.
But to make your program work, you'll need to update the value of c inside the loop
for (a=1; c<15; a++)
{
c = a + 2*b;
Console.WriteLine(c.ToString());
}
do i have to define my condition depending on my initializing variable?
no you don't you can define any condition you want.
what am I doing wrong?
You never so something to make this condition false it will run forever like in your case.
You change c only once before the loop starts:
c = a + 2*b;
for (a=1; c<15; a++)
{
Console.WriteLine(c.ToString());
}
as soon as you enter the loop c is never changed again! Move the changing part into the loop:
for (a=1; c<15; a++)
{
c = a + 2*b;
Console.WriteLine(c.ToString());
}
You could even write the loop using a bool variable
bool condition = true;
for (int a = 0; condition; i++)
{
c = a + 2*b;
if (c > 15)
{
condition = false;
}
}
EDIT
You could even do the computation inside the comparison block of the for-loop and it would work:
for (a = 0; (c = a + 2 * b) < 15; a++)
{
Console.WriteLine(c.ToString());
}
Disclaimer: I would not recommend it because it is cryptic and horrible to read.
A inside condition:
for (a=1; a<=10; a++)
{
c = a + 2*b;
Console.WriteLine(c.ToString());
}
Loop will stop when, c becomes 14.
Is it faster to process the unwrapping of Tuple and using a few variables instead of just using Tuple as it is. Consider this example :
Tuple<int,int> test = new Tuple<int,int>;
int numberOne = int.Parse(Console.Readline());
int numberTwo = int.Parse(Console.Readline());
test.Item1 = numberOne;
test.Item2 = numberTwo;
for(int i = 0; i < 10; i++)
{
if(test.Item1 * i > test.Item2 * i)
{// do stuff}
else
{// do stuff}
}
VS
Tuple<int,int> test = new Tuple<int,int>;
int numberOne = int.Parse(Console.Readline());
int numberTwo = int.Parse(Console.Readline());
test.Item1 = numberOne;
test.Item2 = numberTwo;
for(int i = 0; i < 1000; i ++)
{
int tempItem1 = test.Item1;
int tempItem2 = test.Item2;
if(tempItem1 * i > tempItem2 * i)
{// do stuff}
else
{// do stuff}
}
I also want to know if unwrapping the Tuple with 2 variables is faster than using item1 and item2. Will the result still be the same if we were using more than 2 variables ?
Thanks in advance
Both versions load the tuple items from memory into a temporary value. The only difference is whether it's a temporary on the IL execution stack or an actual IL local. Normally, this should be within reach of the JIT.
If this was a C compiler it would be 100% certain that the performance is the same. Any case where that was not so would be a bug.
The .NET JIT has a very poor optimizer so you might always get unlucky and step into an optimizer hole.
I am trying to pass DataGridView values into string and then to a TextBox. But I don't understand where am I going wrong.
int CFee = 0;
int CLAIMAMT = 0;
int requests = 0;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string requests = (dataGridView1.RowCount);
while (requests.Length < 5)
requests = "0" + requests;
textBox2.Text = requests.ToString();
string CFee = (dataGridView1.Rows[i].Cells["CFee"].Value);
for (int i = 0; i < 5; i++)
CFee = "0" + CFee;
while (CFee.Length < 9)
CFee += "0";
textBox3.Text = CFee.ToString();
string CLAIMAMT = (dataGridView1.Rows[i].Cells["CLAIMAMT"].Value);
for (int i = 0; i < 5; i++)
CLAIMAMT = "0" + CLAIMAMT;
while (CLAIMAMT.Length < 10)
CLAIMAMT += "0";
textBox4.Text = CLAIMAMT.ToString();
}
My required output is this:
no of requests = 00002; // 5 characters
court fee = 000006000; // 9 characters
claim amount = 0000020000; // 10 characters
If I normally assign values to string like string requests = "2", I get that. But I want to know how to pass values of DataGridView as string. I am getting an error like:
A local variable named 'CFee' cannot be declared in this scope because it would give a different meaning to 'CFee', which is already used in a 'parent or current' scope to denote something else.
Rename string CFee to string strCFee - its conflicting with int CFee
You need to rename all of your string variables because even variables with different types cannot have the same name, otherwise the compiler wouldn't know what to do.
And, to use Length, you must first use ToString() to convert what you want.
Concerning the counter variables 'i', they're conflicting with the 'i' already declared at the first for loop. Just give another name to them.
And just another thing: to get your datagridview values you need to convert to string before assigning them to string variables:
string strCLAIMAMT = dataGridView1.Rows[i].Cells["CLAIMAMT"].Value.ToString();
Friends
I have got an error which tells "Index was outside the bounds of the array" I dont know y it was happening since after completeting the for loop and entering the loop fresh again it was showing the variable value when exited from the loop before.
int[,] arrScr = new int[lstTest.Count, cnt2 + 3];
string[,] arrName = new string[lstTest.Count, cnt2 + 3];
int p;
for (i = 0; i < lstTest.Count; i++)
{
using (DataTableReader dtr3 = ds.Tables["scord_mark_table" + (i + 1).ToString()].CreateDataReader())
{
p = 0;
while (dtr3.Read())
{
arrName[i, 2 + p] = dtr3[15].ToString();
for (int k = 2; k < 12; k++)
{
arrScr[i, 2 + p] += Convert.ToInt32(dtr3[k].ToString());
}
p++;
}
}
What is in dtr3[12]? does it return null?
What is the value of cnt2?
Change k <= 12 to k < 12.
If this is not the cause of your problem you should rewrite k <= 12 to k < 13 as that is the convention most coders are used to read and write.
This means that your array does not contain as many elements as you are trying to access.
It's going to exit from the loop as k = 13 because the last valid value of k is 12, then it went through the loop, executed k++ (making it 13). At which point it fails the condition because 13 > 12 and that's when it actually exits.