C
C#14mo ago
joejoejoe

I'm having trouble with an IndexOf returning the position in a array

I looked online and wasn't able to find a solution so any help would be appreciated.
25 Replies
HimmDawg
HimmDawg14mo ago
so which line is the culprit, what did you expect and what was actually happening?
joejoejoe
joejoejoe14mo ago
line 36. I was expecting it to find the position of the name entered, return the value and then print the friend's details.
HimmDawg
HimmDawg14mo ago
You have a Friend[] but you are actually searching with a string
int pos = Array.LastIndexOf(friends, Nombre);
int pos = Array.LastIndexOf(friends, Nombre);
This method would only work if Nombre was a Friend
joejoejoe
joejoejoe14mo ago
That's right. For instance, on the initial prompt to enter friend details into the array, I include "John". Later, I would be prompted again to find the birthday of the friend I want. If I type in "John" it should return the array position of that friend. The program is not doing that.
HimmDawg
HimmDawg14mo ago
Yeah, I know Does it have to be LastIndexOf specifically?
joejoejoe
joejoejoe14mo ago
No. Anything that works would be fine with me lol
HimmDawg
HimmDawg14mo ago
Multiple ways to do it: - loop through the friends and just do some if-magic - use System.Linq and directly obtain the Friend with Friend? f = friends.LastOrDefault(f => f.Name == Nombre);. Instead of checking for the index, you can check f is null
joejoejoe
joejoejoe14mo ago
We haven't been taught how to use linq yet so it'll have to be a loop.
HimmDawg
HimmDawg14mo ago
Aight. Encapsulate it into a function then 😉
joejoejoe
joejoejoe14mo ago
Sorry I don't follow.
HimmDawg
HimmDawg14mo ago
You have a functionGetFriendData() This function encapsulates logic to obtain data and create a Friend object Makes it reusable and more readable Now do the same with "loop through my friend array and check if the name of the current friend is the name the user provided" Or well... you dont have to do it, but it looks nicer + has the aforementioned benefits
joejoejoe
joejoejoe14mo ago
Creating a function to loop through the array returned the same result. I must be missing something.
HimmDawg
HimmDawg14mo ago
How did you loop through your array? Also, can you provide the name you are providing for the data and for the search?
joejoejoe
joejoejoe14mo ago
Actually, I have another question, would a Array.IndexOf() even work in this context?
HimmDawg
HimmDawg14mo ago
Array.IndexOf Methode (System)
Sucht das angegebene Objekt und gibt den Index seines ersten Auftretens in einem eindimensionalen Array oder in einem Elementbereich im Array zurück.
HimmDawg
HimmDawg14mo ago
As you can see
public static int IndexOf (Array array, object? value);
public static int IndexOf (Array array, object? value);
expects an object? as second parameter. The function would try to match any element of the array with that object But since you are providing a string, it'd try to compare Friend objects with a string
joejoejoe
joejoejoe14mo ago
If the Friend objects aren't string values, then what are they?
HimmDawg
HimmDawg14mo ago
They are Friend objects Friends are Friends and strings are strings But your Friend object contains a property that is a string Name
joejoejoe
joejoejoe14mo ago
I understand what you mean but if the user input is a string data type how do I covert it to a Friend data type?
HimmDawg
HimmDawg14mo ago
You don't. Your idea is the other way around. You don't convert a string to a Friend. All you need to do is to take a Friends Name property and compare this to the user input. Both are strings, so they can be compared
Pobiega
Pobiega14mo ago
List<Friend> friends = new();

// pretend we add some friends here...

string lookingForName = "Steve";

foreach (var friend in friends)
{
// how do we check if `friend` has the name "Steve" here?
}
List<Friend> friends = new();

// pretend we add some friends here...

string lookingForName = "Steve";

foreach (var friend in friends)
{
// how do we check if `friend` has the name "Steve" here?
}
Haze.
Haze.14mo ago
are my eyes deceiving me, or are your sub routines all in Main?
Pobiega
Pobiega14mo ago
they are indeed local functions declared inside Main.
Haze.
Haze.14mo ago
thats painful
Want results from more Discord servers?
Add your server
More Posts