-1

im completly new to programing, im trying to create something like a manga organizing tool in windows forms, but im getting stuck in populating a listbox with data from a deserialized json string, it currently is only displaying boolean values correctly all other values are "0" even strings. i have a button to do this:

using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Json Files|*.json", ValidateNames = true, Multiselect = false })
{
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        var fileStream = openFileDialog.OpenFile();
        using (StreamReader sr = new StreamReader(fileStream))
        {
            string fileContent = sr.ReadToEnd();
            ComicList comics = JsonConvert.DeserializeObject<ComicList>(fileContent);
            Manga_listBox.DataSource = DisplayComic.FullList;


            //ignore this little bit it's just so i can see what's happening
            label1.Text = Convert.ToString(comics.Comics.Count);
            label1.Text = Convert.ToString(DisplayComic.FullList);
        }
    }
}

and i have the following classes like so:

public class Comic
{
    private string Manga;
    private int Chapters;
    private bool isFinished;
    private int LastReadCH;

    public string Manga1 { get => Manga; set => Manga = value; }
    public int Chapters1 { get => Chapters; set => Chapters = value; }
    public bool IsFinished { get => isFinished; set => isFinished = value; }
    public int LastReadCH1 { get => LastReadCH; set => LastReadCH = value; }



    public Comic(Comic asd)
    {
        this.Manga = Manga1;
        this.Chapters = Chapters1;
        this.IsFinished = IsFinished;
        this.LastReadCH = LastReadCH1;
    }
    public override string ToString()
    {
        return string.Format("{0}, {1}, {2}, {3}",
                this.Manga, this.Chapters, this.IsFinished, this.LastReadCH);
    }

}

and

public class ComicList
{
    private List<Comic> comics;

    public List<Comic> Comics { get => comics; set => comics = value; }

}

and

public class DisplayComic
{
    static DisplayComic()
    {
        using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Json Files|*.json", ValidateNames = true, Multiselect = false })
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                var fileStream = openFileDialog.OpenFile();
                using (StreamReader sr = new StreamReader(fileStream))
                {
                    string fileContent = sr.ReadToEnd();
                    ComicList comics = JsonConvert.DeserializeObject<ComicList>(fileContent);
                    FullList = comics.Comics;


                }
            }
        }
    }

    private static List<Comic> fullList;

    public static List<Comic> FullList { get => fullList; set => fullList = value; }

    public static List<Comic> GetComcis()
    {
        return FullList;
    }


}

code is probably quite messy, then again I'm completely new and have been following all kind of information online, also for some reason, the open dialog windows is opening twice I don't understand why. here is the json file I'm using:

{
  "Comics": [
    {
      "Manga": "MangaNumber1",
      "Chapters": 85,
      "isFinished": true,
      "LastReadCH": 85
    },
    {
      "Manga": "MangaNumber2",
      "Chapters": 112,
      "isFinished": true,
      "LastReadCH": 112
    },
    {
      "Manga": "MangaNumber3",
      "Chapters": 117,
      "isFinished": true,
      "LastReadCH": 117
    },
    {
      "Manga": "MangaNumber4",
      "Chapters": 74,
      "isFinished": true,
      "LastReadCH": 74
    }
  ]
}

I've tried pretty much anyone with my "expertise" could, changing all kind of variable names and so on, would really appreciate some help.

here's a screenshot of the problem: as you can see only the boolean values are actually correct, otherwise they'd be false, all other values though...

EDIT: the result im hopping for is to populate the listbox with the manga names, (thank you @beeker for that property thing) and once i select said manga then i want to create some other objects such as labels and text boxes to view and edit the values of the chapters etc, also i would like to be able to see what is being parsed by the json file how ever when i do this:

                        label1.Text = Convert.ToString(comics);

i get the label with the text "Manga_Organizer_2.ComicList"

By the way when i say im new, i mean i only ever did stuff with console apps using only "if" functions this whole parsing json, openfiledialog, and even classes usage is completly new. I also have no background in programing with any other language c# is the first and im loving it even though having terrible difficulties :)

Answer/Update: All is good now :) all i had to do was set

                        DisplayComic.FullList = comics.Comics;

in the openfiledialog right before setting the datasource for the listbox. In the end it looks like this:


                        string fileContent = sr.ReadToEnd();
                        ComicList comics = JsonConvert.DeserializeObject<ComicList>(fileContent);
                        DisplayComic.FullList = comics.Comics;
                        Manga_listBox.DataSource = DisplayComic.FullList;
                        Manga_listBox.DisplayMember = "manga";

also removed the encapsulations alltogether in the comic class, in the end it looks like this:

    public class Comic
    {
        public string Manga { get; set; }
        public double Chapters { get; set; }
        public bool IsFinished { get; set; }
        public double LastReadCH { get; set; }
        public string StartedOn { get; set; }
        public string FinishedOn { get; set; }

    }

and also the displaycomic class looks like this:

    public class DisplayComic
    {
        public static List<Comic> FullList { get; set; }


        public static List<Comic> GetComcis()
        {
            return FullList;
        }
    }

Also, after all this trouble came even more, i could deserialize the json, parse it to a string and then to a list, load, save and edit it and i also managed to serialize it back together, and with a savefiledialog create a file for it, however when i did, i would be unable to re-deserialize it once again, something about the json had changed (from object to array or vice versa), aside from that i also had problems with datetime stuff, couldn't load it correctly from a string and so on, anyway after a bunch of mishaps, and litteraly 17hours of looking at code with a puzzled face i finnaly finished my app :D! It does all i want it to, load a json, save and edit it, put it back together, add and remove from it, and i learned a bunch from all this trouble, thank you all for helping, thanks to you guys learned how to set stuff to display on listboxes, and also very importantly "somewhat" learned how to debug code. Thanks.

Rui Paulo
  • 9
  • 4
  • _"...I'm completely new to programming..."_ so an important skill to learn is [how to debug](https://stackoverflow.com/q/25385173/5528593) your code to find out step by step what goes on and where it goes wrong. For example you could check what was actually deserialized. which values your `Comic` instances have been populated with etc.. – René Vogt Mar 16 '20 at 16:21
  • Your Comic class constructor looks wrong - you're not populating your properties from the asd parameter that's being passed in. – auburg Mar 16 '20 at 16:26
  • hi! welcome! I agree with @RenéVogt on this one, learning how to debug will be one of your top priorities in the process of learning how to process. in the mean time, maybe expand your post with some expected output and current output. also ask yourself these questions: what do my models look like after I've parsed the JSON, and are these the values what I expect or is there something wrong. this should narrow the issue down from the whole app to either the display part or the data part. keep doing this until you find the root cause – Grey Mar 16 '20 at 16:29
  • @auburg that was just recently added i wasn't even using it before kinda desperate here and thought that would help, the code outputs the same result even without that whole snipet of code. – Rui Paulo Mar 16 '20 at 16:51
  • @RenéVogt not trying to be repetitive but again, completly new, im using visual studio 2017 i see the debug drop down up there but i've got no idea how to use it, btw sorry for my bad english it's not my native language. – Rui Paulo Mar 16 '20 at 16:56
  • @grey im going to expand the post now thank you for the sugestions. – Rui Paulo Mar 16 '20 at 16:56
  • @RenéVogt okay so i think i got how to debug, i think... hum so i put a breakpoint in the `ComicList comics = JsonConvert.DeserializeObject(fileContent);` an i noticed that "fileContent" does indeed have all the text from the json, however "comics" isn't getting those values, the only value being correctly parsed is the isFinished property all other at at null/0 – Rui Paulo Mar 16 '20 at 17:14

2 Answers2

0

Try setting the listbox "DisplayMember" property so that the control knows which property of the class you want to see. Something like this...

Manga_listBox.DataSource = DisplayComic.FullList;
Manga_listBox.DisplayMember = "Manga";

Ref: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listcontrol.displaymember?view=netframework-4.8

beeker
  • 780
  • 4
  • 17
  • setting that property did nothing however, when i set it as "Manga1" the result was a complete empty listbox, which i believe is okay since the "Manga" property was returning an empty string anyway, this is nice to show only the names once i get everything working, thanks – Rui Paulo Mar 16 '20 at 16:58
0

Answer: i removed that whole openfiledialog in the display comic class, renamed the encapsulations in the Comic class and done :D, now the openfiledialog only opens once and the values from the json are being correctly parsed to the comics list, thus enabling me to use them however i want, thank you, you pushed me in the right direction with the idea to learn debug stuff :D. Now all that's left is learn how to create objects by selecting the different lines from the listbox, another challenge awaits this newcomer.

Rui Paulo
  • 9
  • 4
  • glad it's solved! could you provide the solution in code as well? this will greatly help other new people who run into similar issues like you. remember to never stop learning ;) – Grey Mar 17 '20 at 12:41
  • sure thing, will include now in the post. – Rui Paulo Mar 18 '20 at 19:40