items.IndexOf not printing out position [closed] - c#

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 am fairly new to C# programming and have just finished learning about placeholders and the great convenience of the Array libraries/methods that can be used in C#.
I have written a very simple program that start with a populated array list of 3 strings. I have everything correct, as far as I know, but for whatever reason my items.IndexOf() method is only and always printing out 0 into the console.
Can anyone tell/teach me as to why this may be happening? As far as I know a foreach statement should update the values of the array so each pass through the items.IndexOf() value should change but it is not.
Here is my short code and I appreciate any advice, tips and help!
namespace ConsoleApplication01
{
class Program
{
static void Main(string[] args)
{
string[] items = { "sword", "shield", "potion" };
WriteLine("Quick! You're being attacked by a Goblin Bruiser!");
WriteLine("Which item will you use?");
foreach (string item in items)
{
WriteLine("{0} {1}",items.IndexOf(item), item);
}
ReadLine();
}
}
}
UPDATE:
The issue was they syntax of items.IndexOf and Array.IndexOf.
I assumed that because my items variable was an array it could be used in the way my code below shows. Thank you for all of the people who have taught me !

you have your variables mixed up
it should be items not item in the for loop. like this :
foreach (string item in items)
{
WriteLine("{0} {1}", Array.IndexOf(items,item), item);
}

Related

How to remove items from one ListBox which already exists in other ListBox items [closed]

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
How can I make matches between two ListBoxes and remove them from second ListBox?
For example, I have foo in listbox1, if it is also in listbox2 then remove foo from listbox1.
Very simple:
foreach (var item in listBox1.Items)
listBox2.Items.Remove(item);
I hope to be helpful:)
This should do it:
foreach(var item in listBox1.Items)
{
// check if listbox2 contains the current item in the foreach loop.
// don't forget to use: using System.Linq;
bool hasItem = listBox2.Items.Contains(item);
// if it has it than remove it
if(hasItem)
listBox2.Items.Remove(item);
}

Index and length must refer to a location with the string. Parameter name: length error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
My program has been receiving this error recently and I am not sure why. The code that is triggering it is:
foreach (var lot in item.LotNum.Split('|'))
{
string vendor = string.Empty;
if (lot.Trim().Contains("-"))
vendor = lot.Trim().Substring(0, item.LotNum.IndexOf("-"));
}
LotNum = "br549 | BR549 | 570-PRIOR" and lot is "570-PRIOR" (without quotes) when the error triggers. I've not used IndexOf before and so I am not sure what is wrong with the string that is being sent in. I want to check for what causes the error beforehand because the exception is stopping the program and the bad data will be there for a while until it is fixed, and more may be added in the future.
Any help would be appreciated!
New answer according your code update:
var lots = item.LotNum.Split('|');
foreach (var lot in lots)
{
string vendor = string.Empty;
if (lot.Contains("-"))
vendor = lot.Substring(0, lot.IndexOf("-")).Trim();
}
Again, you were using IndexOf for a variable different than the one you want to get a substring
You are using IndexOf for a variable different than the one you want to get a substring, so, the index will be out of range.
Try with: edited
variable = variable.Trim();
int index = variable.IndexOf("-");
if (index > 0)
variable.Substring(0, index);
Another way to do this is to check for the existence of the character using Contains, and if it's found use the IndexOf, otherwise just use the Length of the string (and put Trim at the end to avoid issues with using the Length after trimming):
variable.Substring(0, variable.Contains('-')
? variable.IndexOf('-')
: variable.Length)
.Trim();

For loop breaks after one [closed]

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 7 years ago.
Improve this question
I have a for loop and it keeps breaking after going around once however I'm unsure on why it is doing this.
for (int fX = 1; fX < 17; fX++)
{
foreach (RoomItem OldItem in Room.GetRoomItemHandler().GetFurniObjects(fX, 26))
{
Logging.WriteLine(OldItem.BaseItem.ToString());
Room.GetRoomItemHandler().RemoveRoomItem(OldItem, false);
}
}
NOTE:
Logging.WriteLine is the same as Console.WriteLine, its just my logging system.
Result of console:
1
But the console should be outputting (1,2,3 and so fourth)
Any ideas? I even tried locking the foreach iteration but that didn't work.
You modify the collection while you iterate through it. This results in undefined behaviour.
You may fix this with using a constant list for the foreach like so:
foreach (RoomItem OldItem in
Room.GetRoomItemHandler().GetFurniObjects(fX, 26).ToList())
{
...
}
But I think you should rather think through your design again.
You should not be changing a collection while iterating through it using a for-loop, re-write using while or do-while.

Index must be within the bounds of the List - C# error [closed]

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'm getting this error when attempting to insert into a list. I'm trying to insert into the start of the list using
myList.Insert(myValue, 0);
The weird thing is, when the debugger comes up, myList has a Count of 1. So 0 is within the bounds of the list. So why would I get this exception?
The signature of Insert Is
public void Insert(int index, T item)
so your arguments are reversed.
The first parameter of List.Insert is the index and the second is the value that you insert.
So this should work as expected:
myList.Insert(0, myValue);
By the way, it works even if the list was empty.
Not sure if that is your problem but I think that List.Insert definition is
public void Insert(int index, T item)
So with your code, your "myValue" is actually the index that List will use to insert in your List and "0" is your value.
Hopes that will help you !

There is an identifier expected but i don't know what i need to put there [closed]

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
What kind of identifier does the visual studio needs ? i just want to put from outside one int array with 2 different numbers between 0 and 66.
public static void playerLocationChange(int[])
{
}
You have to give the parameter a name. Otherwise, you have no way of referring to it from the function.
public static void playerLocationChange(int[] myIntArrayParam)
//Notice the name next to int[]
//This is the parameter's name
{
}

Categories

Resources