locked
Getting a value back from a class RRS feed

  • Question

  • Hi

    I am having a problem getting the results back to my main program from a class.

    I can call a class and get it to produce a result but I dont know how to give the result back to the program that called it in the first place.

    I have a text box and a button on a form

    when I click the buton I want to call a class in the class I want it to set a value eg "From class" and return this value to the text box on the form.

    I have read many things on the net only to be come more confused.

    Please help

    Graham

    Here is the code I have so far

    code for form

     

    using System;

     

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

        namespace WindowsFormsApplication1

            {

            public partial class Form1 : Form
                {

                public string tb1 ="";

                    public Form1()

                    {

                        InitializeComponent();

                     }

                    private void button1_Click(object sender, EventArgs e)

                        {

                        Class1 test = new Class1();

                        tb1 = Class1.sTexta;

                        textBox1.Text = tb1;

                }

            }

        }

     

    }

    code for the class

    using System;

     

    using System.Collections.Generic;

     

    using System.Linq;

     

    using System.Text;

     

    namespace WindowsFormsApplication1

    {

    public class Class1

     

    {

    public string sTexta ="";

    public Class1()

    {

    string stexta = "From class";

    sTexta = stexta;

    }

    }

    }

    Thursday, October 9, 2008 8:30 AM

Answers

  • You've created an instance of Class1 - test, so you should do like this:

    tb1 = test.sTexta;

    insead of tb1 = Class1.sTexta;

    • Edited by Tyumener Thursday, October 9, 2008 8:42 AM
    • Marked as answer by Graham71 Thursday, October 9, 2008 8:44 AM
    Thursday, October 9, 2008 8:39 AM

All replies

  • You've created an instance of Class1 - test, so you should do like this:

    tb1 = test.sTexta;

    insead of tb1 = Class1.sTexta;

    • Edited by Tyumener Thursday, October 9, 2008 8:42 AM
    • Marked as answer by Graham71 Thursday, October 9, 2008 8:44 AM
    Thursday, October 9, 2008 8:39 AM
  • Thank you

    I have spent two Hours on the net just to find that out

    Graham
    Thursday, October 9, 2008 8:45 AM