Help! list and arrays

Updated on February 2, 2018 in [C] Beginner
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on February 1, 2018

Hi guys!

 

I’m new at coding (C#) and could use some help, I’m doing a logbook 

 

So the logbook is supposed to be a list and every log is supposed to be in an array. And the array is supposed to at least hold two elements…

 

So the thing is. I want the user to make a Title and then add some text and there after be able to search for the title and get both the title and the text and show it for the user. I’ve been able to get the first title and the text to it but when I write a new log the old one disappear. 

 

So it’s supposed to be able to hold several logs.

 

I don’t know if you guys get what i mean but here is the code: https://dotnetfiddle.net/noHiB8

Any suggestions?

  • Liked by
Reply
4 on February 1, 2018

It’s because you only ever return Index[1] at line 85. Index[1] is always the most recent input post, that’s why it looks like it being overridden.

Try replacing your foreach loop at line 81 with a regular for loop like this:

for(int i = 0; i < Loggboken.Length; i += 2) // you want to increment by 2 because the title is always on an even index
{
    if(sokning.ToUpper == Loggboken[i].ToUpper())
     {
          Console.WriteLine("You found: " + Loggboken[i] + " : " + Loggboken[i+1] + ", in your log.");
     }
}

 

 

Edit: I also recommend switching from a List to a Dictionary. Then you can just store the title as the Key for the dictionary. The only downside to this is you can’t have multiple entries with the same title.

on February 1, 2018
1234567for(int i = 0; i < Loggboken.Length; i += 2) // you want to increment by 2 because the title is always on an even index{    if(sokning.ToUpper == Loggboken[i].ToUpper())     {          Console.WriteLine(“You found: ” + Loggboken[i] + ” : ” + Loggboken[i+1] + “, in your log.”);     }}From Mouledoux

Okay so i tried the code you posted, Loggboken is a list, so I have to use Count and not Length, I’m still in the basics and use list for this. And two errors occurs with the code you wrote, it says:

——————————————————————————————————————–

Severity Code Description Project File Line Suppression State
Error CS0019 Operator ‘==’ cannot be applied to operands of type ‘method group’ and ‘string’

-and————————————————————————————————————–

Severity Code Description Project File Line Suppression State
Error CS0163 Control cannot fall through from one case label (‘case 2:’) to another 

on February 1, 2018

I forgot to add the () after sokning.ToUpper, it should be sokning.ToUpper()

Is that the issue?

 

on February 2, 2018

Yes it worked, thank you!

on February 2, 2018

Okay so I just noticed that I didn’t have any [] in my list, and I guess that I need that, so nothing works right now. 

Thanks anyway -.-‘

Show more replies
  • Liked by
Reply
Cancel