views:

48

answers:

0

Hey guys, I have a listbox which the user inputs data into. I then can display the output with a line like this:

dest.Text = (string)listBox.Items[0];

Basically, it outputs the the listBox item at location 0 to a textbox called dest. Problem is that I want to be able to do this from a different class so I instantiate an object like so:

    MainPage myObj = new MainPage();

and then use the same code but as an object like:

    myObj.dest.Text = (string)listBox.Items[0];

It doesnt work though... The first line works and displays the item, but if I instantiate an object and then try to display the item, it just remains blank.

Original Code:

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void addNameButton_Click(object sender, RoutedEventArgs e)
        {
            listBox.Items.Add(addNameBox.Text);
            dothis();
        }
        public int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        public void dothis()
        {
            MainPage myObj = new MainPage();
            myObj.dest.Text = (string)listBox.Items[RandomNumber(0,listBox.Items.Count)]; //Throws error
        }

        private void update_Click(object sender, RoutedEventArgs e)
        {
            dothis();
        }


    }
}

Thanks!