none
Why strValue in public void PassValue(string strValue ) does not exist in private void Form1_Load(object sender, EventArgs e) RRS feed

  • Question

  • Hello everyone,

    I need some help. There is a text "Hello" (strvalue = "Hello") in public void PassValue(string strValue), why I can't use this text in private void Form1_Load(object sender, EventArgs e). Error will occur: "The name 'strValue' does not exist in the current content".

    Expected result that I looking for is strValue can be read in private void Form1_Load(object sender, EventArgs e).

    Codes:

    private void Form1_Load(object sender, EventArgs e)
       
    {

          label1
    .text = strValue;
         
    //strValue does not exist
       
    }

        
    public void PassValue(string strValue )
       
    {

           strValue
    = "Hello";
       
    }

    Thursday, April 18, 2019 8:16 AM

Answers

  • Hi

    Thank you for posting here.

    For you question, you want to know why the string strvalue in passvalue method cannot be accessed in the Form1_Load event.

    You could try to add out keyword to solve it.

    private void Form1_Load(object sender, EventArgs e)
            {
                string text = "";
                PassValue(out text);
                label1.Text = text;
            }
            public void PassValue(out string str)
            {
                str = "Hello";
            }
    
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Pugita Thursday, April 18, 2019 9:26 AM
    Thursday, April 18, 2019 8:40 AM
    Moderator

All replies

  • Hi

    Thank you for posting here.

    For you question, you want to know why the string strvalue in passvalue method cannot be accessed in the Form1_Load event.

    You could try to add out keyword to solve it.

    private void Form1_Load(object sender, EventArgs e)
            {
                string text = "";
                PassValue(out text);
                label1.Text = text;
            }
            public void PassValue(out string str)
            {
                str = "Hello";
            }
    
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Pugita Thursday, April 18, 2019 9:26 AM
    Thursday, April 18, 2019 8:40 AM
    Moderator
  • Thank you Mr. Jack.

    Best regards,

    Pugita

    Thursday, April 18, 2019 9:26 AM