C# List start with value 1 - c#

When i use for (int i = 1; ..) skip the loop the first item.
How can i start with index 1 and dont skip any item?
private void buttonReadAndSort_Click(object sender, EventArgs e)
{
ReadFromFile rd = new ReadFromFile();
var fileList = rd.readFromFile();
for (int i = 0; i < fileList.Count; i++)
{
var item = (fileList[i]);
Console.WriteLine(item);
list.Add(item);
listBox1.Items.Add(item);
}
buttonReadAndSort.Enabled = false;
}

I guess you want to start with index 1 but access the item at index 0:
for (int i = 1; i <= fileList.Count; i++)
{
var item = fileList[i-1];
Console.WriteLine(item);
list.Add(item);
listBox1.Items.Add(item);
}
But you could also loop normally and add +1 where you need it 1 based:
for (int i = 0; i < fileList.Count; i++)
{
var item = fileList[i];
Console.WriteLine("item:{0} #{1}", item, i + 1);
list.Add(item);
listBox1.Items.Add(item);
}

List, like array, start at zero. Not sure why you would want it to start it at 1 and not skip any items. I do something like csvLinesPartNumber.Add("stuff") to add to the list and then I can get "stuff" from the particular index like so: csvLinesPartNumber[4]. For example, I am doing a while sqlreader dot read which will loop through the table with the particular ID. Then you can do another loop to get the data. Just make sure you put the index in the square brackets.

Related

Losing value if backwards List printing matrix

I need to display a Sheet so that the lines are aligned bottom and left. If there are no 10 characters in the word, the number of "+" characters is added so that the total of the line is 10 characters (see my output).
Why, when I print a List, I lose one row?
What's wrong with my piece of code? This matrix should be 10X10.
My output
List<string> filtredList = new List<string>() { "Jacuzzi", "Action", "Chinchilla", "Squeezebox", "Academic", "Abstract" };
int row = 10;
filtredList = Sorting(filtredList); //method is sorting by descending.
foreach (var item in filtredList) Console.WriteLine("Item: " + item + " length: " + item.Length);
Console.WriteLine("-------------------------------------");
//AFTER SORTING IN LIST:
//1)Squeezebox 2)Chinchilla 3)Academic 4)Abstract 5)Jacuzzi 6)Action
for (int i = 10; i > 0; i--)
{
try
{
if (filtredList[i].Length != 10)
{
Console.Write(filtredList[i]);
row = 10 - filtredList[i].Length;
Console.WriteLine(string.Concat(Enumerable.Repeat("+", row)));
}
else Console.WriteLine(filtredList[i]);
}
catch (SystemException)
{
row = 10;
Console.WriteLine(string.Concat(Enumerable.Repeat("+", row)));
}
}
Because your for loop is never gets to 0:
for (int i = 10; i > 0; i--)
i goes from 10 to 1 and that's why the first item of your list never prints.
Note:
Index of first item of an array or a list is 0
The better code would be:
for (int i = 9; i >= 0; i--)
Although you can use better alternatives for some of your code, but this will solve your problem.
EDIT:
You can use this approach (just change the for part to this) to not raise any exceptions (because its not normally a use case for try-catch)and also get faster results:
for (int i = 9; i >= 0; i--)
{
if (i < filtredList.Count)
{
if (filtredList[i].Length != 10)
{
Console.Write(filtredList[i]);
row = 10 - filtredList[i].Length;
Console.WriteLine(new string('+', row));
}
else Console.WriteLine(filtredList[i]);
}
else
{
row = 10;
Console.WriteLine(new string('+', row));
}
}

Select listbox item if contain a string in asp.net using c#

I'm trying to select listbox item if contains a specific string. Here is my code:
string SearchDomain="youdial.in";
for (int i = 0; i < ListBox2.Items.Count; i++)
{
var UrlList = new Uri(ListBox2.Items[i].ToString());
var UrlList = UrlList.Host;
if (UrlList == SearchDomain)
{
ListBox2.SelectedIndex = i;
urllbl.Text = ListBox2.SelectedItem.ToString();
return;
}
}
If ListBox2 have more than 1 matched results than above code select last matched item, but i want to select first matched item. e.g if Query match with 3rd, 7th and 9th item, I want to get the value of 3rd item. Thanks in Advance, sorry for my bad English.
Just change return; to break; Break statement will stop the for loop from executing further.
string SearchDomain="youdial.in";
for (int i = 0; i < ListBox2.Items.Count; i++)
{
var UrlList = new Uri(ListBox2.Items[i].ToString());
var UrlList = UrlList.Host;
if (UrlList == SearchDomain)
{
ListBox2.SelectedIndex = i;
urllbl.Text = ListBox2.SelectedItem.ToString();
break;
}
}
Just reverse the counter change the
for (int i = 0; i < ListBox2.Items.Count; i++)
to
for (int i = ListBox2.Count-1 ; i > -1 ; i--)

Removing item and value in Listview

I have a problem with removing items from my listview. This program calculates the total value of the remaining values in the list. The problem is that when I remove an item it removes the value of the first item added in the listview.
For example:
/*I added this items in order.
item1 = 20,
item2 = 10,
item3 = 5
When I remove item2 its rtbTcost is 15 based on the program below.
Which means that the value of item1 was removed.*/
int totalRemoved = 0;
for (int i = 0; i < lvCart.SelectedItems.Count; i++)
{
totalRemoved += int.Parse(lvCart.Items[i].SubItems[1].Text);
lvCart.Items.Remove(lvCart.SelectedItems[i]);
}
_listTotal -= totalRemoved;
rtbTcost.Text = _listTotal.ToString();
you must use removeat
http://msdn.microsoft.com/en-us/library/aa983548(v=vs.71).aspx
1Start at the end and count down.
for (int i = lvCart.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem itm = lvCart.SelectedItems[i];
lvCart.Items[i].Remove();
}
Total Removed is
lvCart.SelectedItems.Count
if you are using the selected items from the list view,as the items to delete.
Sum after the delete, by summing the values in the column.
int _listTotal = 0;
foreach (ListViewItem li in lvCart) {
_listTotal += int.Parse(li.Subitems[1].Text);
}
rtbTcost.Text = _listTotal.ToString();

select items in a checkbox list based on their text

i have a check box list in my asp.net page ...i need to select the check box based on their text...am getting these string values form the database and storing it in a array.....the below code works fine for a single text ..What should i do in case of array..how should i pass the array values in the if loop
for (int i = 0; i < chkbx.Items.Count; i++)
{
if (chkbx.Text == "Dress" )
{
chkbx.Items[i].Selected = true;
}
}
Below code should work for you -
string[] array = { "Dress", "Pen", "Table"};
for (int i = 0; i < chkbx.Items.Count; i++)
{
if (array.Contains(chkbx.Items[i].Text))
{
chkbx.Items[i].Selected = true;
}
}
You probably want to use the Contains method of the array that contains your text values:
for (int i = 0; i < chkbx.Items.Count; i++)
{
if (myArray.Contains(chkbx.Items[i].Text))
{
chkbx.Items[i].Selected = true;
}
}
where myArray is the array of values you're populating from the database.
The following is a nice way of doing it
foreach (string item in myarr)
{
checkboxlist1.Items[checkboxlist1.Items.IndexOf(checkboxlist1.Items.FindByText(item))].Selected = true;
}

Looping array inside list in C#

I have a list of arrays that contains multiple arrays.
Each array has 2 indexes.
First, I want to loop the list. Then I want to loop the array inside the list.
How can I do that ?
I tried to use this way, but it doesn't work:
1. foreach (string[] s in ArrangList1)
2. {
3. int freq1 = int.Parse(s[1]);
4. foreach (string[] s1 in ArrangList)
5. {
6. int freq2 = int.Parse(s1[1]);
7. if (freq1 < freq2)
8. {
9. backup = s;
10. index1 = ArrangList1.IndexOf(s);
11. index2 = ArrangList.IndexOf(s1);
12. ArrangList[index1] = s1;
13. ArrangList[index2] = s;
14. }
15. backup = null;
16. }
17. }
It give me error in line 4.
I try to do the loop using other way, but I don't know how to continue.
for (int i = 0; i < ArrangList1.Count; i++)
{
for (int j = 0; j < ArrangList1[i].Length; j++)
{
ArrangList1[i][1];
}
}
I use C#.
How can I fix this problem?
The error in line4 might be due to a typo using ArrangList. I think you only have ArrangList1 defined. Regardless, the logic is wrong. If you have a list containing arrays, you want to do a foreach on the list, then a foreach on each list item (which is an array).
It's not clear what your types are, so I'm assuming that ArrangList is a list of string arrays.
foreach(string[] s in ArrangList1)
{
foreach(string innerS in s)
{
//now you have your innerString
Console.WriteLine(innerS);
}
}
In your second example,
You have a typo as well...
for (int i = 0; i < ArrangList1.Count; i++)
{
for (int j = 0; j < ArrangList1[i].Length; j++)
{
ArrangList1[i][1]; //the 1 here should be j
}
}
You can also use LINQ and one foreach loop:
// assuming ArrangList1 is List<string[]>
var query = from a in ArrangList1
from b in a
select b;
foreach (String s in query)
{
System.Console.Writeline(s);
}

Categories