user3795349
user3795349

Reputation: 324

Windows 8 phone System.StackOverflowException while filling a listbox using class system

I have the error System.StackOverflowException, everytime I run my app. This error is located in the last line of my History class. Below:

public class userHistory
{
    public string strTimeDate { get; set; }
    public string strUrl { get; set; }

    public List<userHistory> lstUserHistory { get; set; }

    public userHistory(string timedate, string url)
    {
        lstUserHistory.Add(new userHistory(timedate, url));
    }
}

My MainPage code:

 public List<userHistory> lstUserHistory;
    public userHistory selectedHistory;


public MainPage()
    {
        InitializeComponent();

        listBox.DataContext = lstUserHistory;
    }

private void getHistory(string url)
    {
        string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        userHistory usrHistory = new userHistory(time, url);
        lstUserHistory.Add(usrHistory);

        listBox.DataContext = null;
        listBox.DataContext = lstUserHistory;
    }

    private void listBox_Tap(object sender, GestureEventArgs e)
    {
        selectedHistory = listBox.SelectedValue as userHistory;

        MessageBox.Show(selectedHistory.strUrl);
        browserSearch(selectedHistory.strUrl);
    }

The XAML

  <Grid>
                <ListBox ItemsSource="{Binding}" Foreground="RoyalBlue" Name="listBox" TabIndex="10"  Tap="listBox_Tap">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical" Margin="0,329,0,367" >
                                <TextBlock Text="{Binding strDateTime}" FontSize="15" Margin="51,1,0,1"/>
                                <TextBlock Text="{Binding strUrl}" FontSize="28" Margin="51,1,0,1"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>

I think the error might be because of what happens in getHistory. This method can be called muitply times with the same data. All I would like is for the data to be stored in a List, which can easily add new history records, or remove them. Then be displayed simply in a List box.

Thank you in advance :)

If you need any more details please comment and I will be happy to explain in further detail :)

Upvotes: 0

Views: 136

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503090

Look at this part of userHistory:

public List<userHistory> lstUserHistory { get; set; }

public userHistory(string timedate, string url)
{
    lstUserHistory.Add(new userHistory(timedate, url));
}

There are two bugs here:

  • The constructor calls itself (boom! stack overflow)
  • The constructor calls lstUserHistory.Add when lstUserHistory is definitely null

It's not clear why you've got a List<userHistory> in both your MainPage code and in userHistory. I think you need to think more carefully about the data you're modelling, and what really belongs where.

I suspect your userHistory class should actually look more like this:

public sealed class HistoryEntry
{
    private readonly DateTime timestamp;
    private readonly string url;

    public DateTime Timestamp { get { return timestamp; } }
    public string Url { get { return url; } }

    public HistoryEntry(DateTime timestamp, string url)
    {
        this.timestamp = timestamp;
        this.url = url;
    }
}

Notes:

  • Unless you really need the class to be mutable, make it immutable
  • Follow .NET naming conventions
  • Avoid name prefixes which just specify the type
  • Use the appropriate data type to model what you're interested in - a date/time isn't naturally a string
  • Decide what the role of this class is - is it an entry, or the whole history? With my changes, it's clear that this is just an entry. If you need to model the whole history as a separate class, do that as well, rather than trying to combine the two.

Upvotes: 2

Related Questions