locked
how can used public var that link a control

    Question

  • hi

    i want used this var to many event in my app:

    string st1=textbox.text;

    string st2=textbox2.text

    string st3=textbox3.text

    string st4=textbox4.text

    string st5=textbox5.text

    how can make public them and used st1 , st2 ....st4 for each event?

    tank you


    Amir Ameri


    • Edited by AMIR ameri Saturday, December 13, 2014 5:30 PM
    Saturday, December 13, 2014 5:29 PM

Answers

  • If you don't want to repeat the code you could use the same event handler for all Buttons:

    <Button Content="Add" Click="btnAdd_Click" />
    <Button Content="Button 1" Click="btnAdd_Click" />
    <Button Content="Button 2" Click="btnAdd_Click" />

    If you want to use the same variables in different event handlers, you should define them at class level, i.e. outside the event handler. You will still have to assign them values in the event handler(s) though:

    string Vshape;
                     string Vlength;
                     string Vunitprice;
    ...
    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
                    Vshape = cmbShape.SelectedItem.ToString();
                    Vlength = txbxLenght.Text;
                    Vunitprice = txbxPrice.Text;
      //...
    } 
    

    Please remember to mark helpful posts as answer and/or helpful.

    Sunday, December 14, 2014 12:09 PM

All replies

  • The question is kind of confusing, but if you introduced your string variables within a function they are only visible within that function, moving them out of a function will make them visible from anywhere within your class, further designating those variables as public will make them visible from any instance of your class as well... im not entirely sure what you are asking but heres a simple example..

    public class Test
    {
        public string st1=String.Empty;
        public string st2=String.Empty;
        public string st3=String.Empty;
        public string st4=String.Empty;
        public string st5=String.Empty;
        public Test()
        {
          //intialize your strings here
          //you can call your string variables from any function in your class
        //or from any global instance of your class "in this case Test"
       // IE 
          Test myTestInstance = new Test();
          string s = myTestInstance.st1;// will return str1
        }
    }

    • Marked as answer by AMIR ameri Sunday, December 14, 2014 7:56 AM
    • Unmarked as answer by AMIR ameri Sunday, December 14, 2014 8:37 AM
    Sunday, December 14, 2014 6:54 AM
  • tank you very much Jtmtv.

    my question is :

    i have this var 

        private void btnAdd_Click(object sender, RoutedEventArgs e)

                    string Vshape = cmbShape.SelectedItem.ToString();
                    string Vlength = txbxLenght.Text;
                    string Vunitprice = txbxPrice.Text;
                    string Vunitfee = txbxFee.Text;
                    string Vkgm = txblkg_m.Text;
                    string Vallweigth = txblAllWeigth.Text; //this
                    string Vallmatprice = txblAllPriceMat.Text; //this
                    string Vallfee = txblAllfee.Text; //this
                    string Vtotalrow = txblTotalRow.Text; //this

    i have to repeat all of them to the other button click event.

    my problem is it.

    i dont want repeat this variable for each event.

    what can i do? 


    Amir Ameri


    • Edited by AMIR ameri Sunday, December 14, 2014 8:45 AM
    Sunday, December 14, 2014 7:56 AM
  • If you don't want to repeat the code you could use the same event handler for all Buttons:

    <Button Content="Add" Click="btnAdd_Click" />
    <Button Content="Button 1" Click="btnAdd_Click" />
    <Button Content="Button 2" Click="btnAdd_Click" />

    If you want to use the same variables in different event handlers, you should define them at class level, i.e. outside the event handler. You will still have to assign them values in the event handler(s) though:

    string Vshape;
                     string Vlength;
                     string Vunitprice;
    ...
    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
                    Vshape = cmbShape.SelectedItem.ToString();
                    Vlength = txbxLenght.Text;
                    Vunitprice = txbxPrice.Text;
      //...
    } 
    

    Please remember to mark helpful posts as answer and/or helpful.

    Sunday, December 14, 2014 12:09 PM
  • tank you very much

    it was very  useful .

    Amir Ameri


    • Marked as answer by AMIR ameri Sunday, December 14, 2014 12:25 PM
    • Edited by AMIR ameri Sunday, December 14, 2014 12:35 PM
    • Unmarked as answer by Jamles HezModerator Thursday, December 25, 2014 8:08 AM
    Sunday, December 14, 2014 12:25 PM
  • Why in the world are you copying the code from my post and mark your own one as answer!?

    You should mark posts that answers your question as answer...

    Sunday, December 14, 2014 12:31 PM
  • sorry.i fix it.

    realy sorry.


    Amir Ameri


    • Edited by AMIR ameri Sunday, December 14, 2014 12:37 PM
    Sunday, December 14, 2014 12:36 PM