Get the index while looping - c#

Is it possible to get the current index while looping?
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = DGV.CurrentRow.Index + " " + DGV.Rows[i].Cells[1].Value.ToString();
}
But I have in output :
0 First
0 Second
0 ...
I want to get :
1 First
2 Second
3 ...
Thanks.

for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = (i + 1).ToString() + " " + DGV.Rows[i].Cells[1].Value.ToString();
}
BTW: I'd prefer:
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = String.Format("{0} {1}", i + 1, DGV.Rows[i].Cells[1].Value);
}

It's the index just i+1 in this case?

Try:
myValue = (i+1) + " " + DGV.Rows[i].Cells[1].Value.ToString();

you can do this way also
for (int i = 1; i < DGV.Rows.Count - 1; i++)
{
myValue = i.ToString() + " " + DGV.Rows[i-1].Cells[1].Value.ToString();
}

Related

Filling a Matrix in windows form from user input

So the problem is that I should be able to first write a value and then it should be passed but, its executing all the code at once and not giving me time to write the value i want to give it
public void FillMatrix(string [,] v) {
for (int i = 2; i <= 4; i+=2) {
for (int j = 2; j <= 4; j++) {
lblFute.Text = "Write the number you want to put on index " + i + " " + j;
v[i, j] = txtVlera.Text;
if (j == 3 ) {
lblFute.Text = "Write the number you want to put on index " + j + " " + i;
v[j, i] = txtVlera.Text;
}
}
}
}

How do I can optimize inserting/replacement element in to the List<string>

I have some code:
var result = new List<string>;
...
for (var i = 0; i < level; ++i)
if (result.ElementAtOrDefault(i) == null)
result.Insert(i, " " + positions[i]);
else
result[i] += " " + positions[i];
if (result.ElementAtOrDefault(level) == null)
result.Insert(level, " " + currentPosition);
else
result[level] += " " + currentPosition;
Can I do this without checking element for null from i-position? I need to add a part of string in i-position. But I have "ArgumentOutOfRangeException" if element wasn't created. Also method "insert" don't replace the element but push back it.
I tried to get data from "ArgumentOutOfRangeException" (which index called this exception) but I've failed.
You can reduce using ElementAtOrDefault with adding some condition like this
int i;
for (i = 0; i < level && i < result.Count; ++i){
//change existing items
result[i] += " " + positions[i];
}
for (int j = 0, countAdd = level - result.Count; j < countAdd; ++j)
//add new items
result.Add(" " + positions[i+j]);
//add current
if (level >= result.Count)
result.Add(" " + currentPosition);
else
result[level] += " " + currentPosition;

alternative method of creating string from list

Have such function, which creates string from List of ids
I try to run it in 50 threads, and after 30 seconds it hangs app
public string genText(List<string> list)
{
string text = #"{""text"":""";
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text += "!" + " ";
else
text += "!" + #""", ";
}
text += #"""start"":[";
for (int i = 0, lim = -2; i < list.Count; i++)
{
lim += 2;
if (i < list.Count - 1)
text += #"""" + lim + #""",";
else
text += #"""" + lim + #"""], ";
}
text += #"""end"":[";
for (int i = 1, lim = -1; i < list.Count + 1; i++)
{
lim += 2;
if (i < list.Count)
text += #"""" + lim + #""",";
else
text += #"""" + lim + #"""], ";
}
text += #"""type"":[";
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text += #"""USER"",";
else
text += #"""USER""], ";
}
text += #"""objectId"":[";
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text += #"""" + list[i] + #""",";
else
text += #"""" + list[i] + #"""]}";
}
return text;
}
What i­­­­­­­­­­­­­­s the best (alternative) way of writing such functio­­­­­­­­­­­­­­­­­­­­­­­n?
Expected result: {"text":"! ! ! ! ! ! ! ! ! ! ! ! ! ! !", "start":["0","2","4","6","8","10","12","14","16","18","20","22","24","26","28"], "end":["1","3","5","7","9","11","13","15","17","19","21","23","25","27","29"], "type":["USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER"], "objectId":["524231066205","363249609235","509321690322","551832845250","273337925148","553378679618","552270073142","256641407","545454406232","548096729194","555315805314","553271555117","573655339037","518779453704","486606264340"]}
Instead of concatenating strings it is much more efficient to use the StringBuilder class.
public string genText(List<string> list)
{
StringBuilder text = new StringBuilder();
text.Append(#"{""text"":""");
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text.Append("!" + " ");
...
return text.ToString();
}
but as other have mentioned in the comments, use a Json library if you want to create Json data.

Need help to create a c# bubblesort. Whats wrong with what i have so far?

I am trying to use a bubble sort, to order numbers from least to greatest in c#, whats wrong with what i have so far?
private void Order_Click(object sender, EventArgs e) {
value1 = Convert.ToInt32(textBox1.Text);
value2 = Convert.ToInt32(textBox2.Text);
value3 = Convert.ToInt32(textBox3.Text);
value4 = Convert.ToInt32(textBox4.Text);
value5 = Convert.ToInt32(textBox5.Text);
int[] myarray = { value1, value2, value3, value4, value5 };
int n = 0;
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++)
{
if (myarray[i] > myarray[i + 1]) {
tmp = myarray[i];
myarray[i] = myarray[i + 1];
myarray[i + 1] = tmp;
swapped = true;
order1.Text = Convert.ToString(myarray[0] + "," +
myarray[1] + "," +
myarray[2] + "," +
myarray[3] + "," +
myarray[4]);
}
}
}
}
First thing which is wrong with your code is:
for (int i = 0; i < n - j; i++)
Your check i < n - j will never let the control to fall through the loop. Because you have initialized n to 0 and j is 1 (after j++), so i is not going to be less then -1, Thus your loop will not work as intended. Since you set the swapped to false, the control will fall out of the while loop, hence no sorting.
You need to change your code to:
while (swapped)
{
swapped = false;
for (int i = 0; i < myarray.Length - 1; i++)
{
if (myarray[i] > myarray[i + 1])
{
int t = myarray[i];
myarray[i] = myarray[i + 1];
myarray[i + 1] = t;
swapped = true;
Console.WriteLine(Convert.ToString(myarray[0] + "," + myarray[1] + "," + myarray[2] + "," + myarray[3] + "," + myarray[4]));
}
}
}
the n and j variables seem unnessesary. leave n and j out of your program, and in your for loop, do this:
for (int i = 0; i < (array1.Length - 1); i++)
Bubble Sort
using System;
class AscendingBubbleSort
{
public static void Main()
{
int i = 0,j = 0,t = 0;
int []c=new int[20];
for(i=0;i<20;i++)
{
Console.WriteLine("Enter Value p[{0}]:", i);
c[i]=int.Parse(Console.ReadLine());
}
// Sorting: Bubble Sort
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
if(c[i]>c[j])
{
Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]);
t=c[i];
c[i]=c[j];
c[j]=t;
}
}
}
Console.WriteLine("Here comes the sorted array:");
// Print the contents of the sorted array
for(i=0;i<20;i++)
{
Console.WriteLine ("c[{0}]={1}", i, c[i]);
}
}
}
source

How to create a loop for the following case?

/html/body/table/tr[1]/td[2]
/html/body/table/tr[1]/td[4]
/html/body/table/tr[3]/td[2]
/html/body/table/tr[3]/td[4]
/html/body/table/tr[5]/td[2]
/html/body/table/tr[5]/td[4]
So, the index of tr[ ] would be odd numbers, and td[ ] would always be either 2 or 4.
for(int i = 1; i < bound; i += 2) {
for(int j = 2; j <= 4; j += 2) {
Console.WriteLine(
String.Format("/html/body/table/tr[{0}]/td[{1}]", i, j)
);
}
Console.WriteLine();
}
You could do something as simple as
for(tr = 1; tr < maxodd+1; tr += 2;)
{
//pseudoimplementation
/html/bod/table/tr[tr]/td[2]
/html/bod/table/tr[tr]/td[4]
}
The most naive case:
for(int i = 1; i < 6; i += 2) {
Console.WriteLine("html/body/table/tr[" + i + "]/td[2]");
Console.WriteLine("html/body/table/tr[" + i + "]/td[4]");
}

Categories

Resources