Save array in list C#

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

Hi guys. 

I was wondering why I can’t save my array in my list. (in my IF statement)

The code is below, grateful for any help, cheers!

https://dotnetfiddle.net/vS4Tef

  • Liked by
Reply
23 on February 2, 2018

Assuming this is the same code from your other post,

Loggboken is a List of strings, but in case 1, right after you make the temporary array save, you try to add save to Loggboken. But you can’t do that. save is an array of strings.

Try removing (or commenting out) that line.

 

Also, just to make sure your answer is being recorded, maybe you could print “Save successful” to the screen in side the if statement.

 

on February 2, 2018

Yes, it is the same as the other one, I was finished with the whole code but then i realized that I was supposed to use: 

List<string[]> Loggboken = new List<string[]>();

and not

List<string> Loggboken = new List<string>();

And that changed everything so I’m quite lost at the moment.

 

 

This was my first code : https://dotnetfiddle.net/kKYEmJ

It worked until i realized I didn’t have [] in my list.

And this is what i’m currently working on: https://dotnetfiddle.net/7mKQNk

on February 2, 2018

So what is the issue with this iteration?

From what it looks like everything so far is correct. Minus a few if statements you had before.

 

on February 2, 2018

 

https://dotnetfiddle.net/NgNHAd

At the moment it seems like I can’t find any logs that has been written and when I try to print all logs: System.String[] appears. And I need to solve the if part again

 

for (int i = 0; i < Loggboken.Count; i += 2)
{
if (answer == Loggboken[])
{
Console.WriteLine(“Title: ” + Loggboken[i] + “\n” + “Text:” + “\n” + Loggboken[i + 1]);
}

}

 

I guess it’s a small fix but I can’t fint it at the moment..

on February 2, 2018

Well remember, Loggboken is now a List of arrays, not just a List. So, for example, in your Search case, you’re printing Loggboken[i], and Loggboken[i+1]. These are no longer the Title and Post at two different indexes. Now Loggboken[i] is both the Title and Post in one. The Title is now stored in the 0th place of the List item, and Post in the 1st.

for (int i = 0; i < Loggboken.Count; i++)
{
   Console.WriteLine("Title: " + Loggboken[i][0] + "\n" +
   "Text:" +  Loggboken[i][1] + "\n");
}                       

 

on February 2, 2018

But in this case, you can’t really enter several posts, right?

It seems like I only can access one person

on February 2, 2018

No you can. You’re already doing that.

 

on February 2, 2018

Okay so when i first doing a log, i can print it out and search for it.

But when i add another post i can’t print them all out, only the first one. 

And when i’ve added two logs, I find both when i’m searching for one of them.

This is so weird, sometimes it’s there and sometimes it’s not..

on February 2, 2018

That might be because you never call Loggboken.Add(innehåll). You shouldn’t be getting anything to print at all.

on February 2, 2018

Seems like I have to add the same posts two times to get it to stick

 

on February 2, 2018

Well, if I don’t have Loggboken.Add(innehåll) I’m not able to get any information at all

Show more replies
  • Liked by
Reply
Cancel