0

I've been reading for a while now how to do this, but i never find the awnser i understand or works.

This is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Utilities;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;

namespace SongTunes
{
public partial class Form1 : Form
{
    Object Songs;

    public class Song
    {
        public int song_id { get { return song_id; } set { song_id = value; } }
        public string sorting_number { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string lyrics { get; set; }
        public string purchase_link { get; set; }
        public string youtube_id { get; set; }
        public string has_music_video { get; set; }
        public string allow_downloads { get; set; }
        public string raw_artist_id { get; set; }
        public string artist_id { get; set; }
        public string album_artist_id { get; set; }
        public string release_date { get; set; }
        public string artist_name { get; set; }
        public string album_artist_name { get; set; }
        public string album { get; set; }
        public string bitrate { get; set; }
        public string duration { get; set; }
        public string filesize { get; set; }
        public string path { get; set; }
        public string is_part_of_compilation { get; set; }
        public string visible { get; set; }
        public string track_number { get; set; }
        public string is_explicit { get; set; }
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        button1.Text = "Loading...";
        backgroundWorker1.RunWorkerAsync();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void backgroundWorker1_Done(object sender, RunWorkerCompletedEventArgs e)
    {
        button1.Enabled = true;
        button1.Text = "reload";
    }


    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string address = "";
        System.Net.WebClient webclient = new WebClient();
        string json = webclient.DownloadString(address);
        Songs = JsonConvert.DeserializeObject<List<Song>>(json);
    }

    private void SongList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
}

It just crashes on the first

public int song_id { get { return song_id; } set { song_id = value; } }

with "An unhandled exception of type 'System.StackOverflowException' occurred in SongTunes.exe"

5
  • 1
    Of course it will. It will cause a classic StackoverflowException. Rewrite it like public Int32 song_id { get; set; } Commented Dec 7, 2014 at 10:39
  • 3
    Each time you are referring to that property, it recursively calls itself again and again causing the stack to be overflew =) Commented Dec 7, 2014 at 10:40
  • Or if your intention is to explicitly indicate the body use a field private Int32 _song_id; public Int32 song_id { get { return _song_id;} set { _song_id = value } } Commented Dec 7, 2014 at 10:41
  • And one more last advice, don't use this kind of property names. It is not trueъ. The proper naming conventions for .NET for properties is a PascalCase. There should be data annotations or something for mapping this kind of stuff. Commented Dec 7, 2014 at 10:44
  • Oww! Thanks man, i knew it was something this easy Commented Dec 7, 2014 at 10:46

1 Answer 1

4
 public int song_id { get { return song_id; } set { song_id = value; } }

The get and refers to it self, this will result in a stack overflow when you calling it. If you want to save the value in a field try:

 private int _song_id;
 public int song_id { get { return _song_id; } set { _song_id = value; } }

or just change it into:

 public int song_id { get; set; }

like your other properties

Sign up to request clarification or add additional context in comments.

3 Comments

And now we're here, how do i get the value of song_id in something like a MessageBox? MessageBox.Show(Song[100].song_id);?
((Songs as Song[])[100]).song_id, your Songs object is from type object. Make it type Song[] or List<Song>.
I now have List<Song> Songs; but when i do MessageBox.Show(((Songs as List<Song>)[100]).song_id); i get Additional information: Exception has been thrown by the target of an invocation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.