-5

I have this working code:

public sealed partial class MainPage : Page
{

    public MainPage()
    {
        this.InitializeComponent();

        string name = tbxName.Text;
        string age = tbxAge.Text;
        string dinner = tbxDinner.Text;
    }



    private void OutputNewValues()
    {
        string answer = "Hello, " + tbxName.Text + " you are " + tbxAge.Text + " years old. " + tbxDinner.Text + " sounds yummy for dinner!";
        finalOutput.Text = answer;
    }
}

Now, in this part:

private void OutputNewValues()
    {
        string answer = "Hello, " + tbxName.Text + " you are " + tbxAge.Text + " years old. " + tbxDinner.Text + " sounds yummy for dinner!";
        finalOutput.Text = answer;
    }

I want to use the strings I declared earlier (name, age, dinner) instead of doing "tbxName.Text". But it will give me an error, because it doesn't exist in the current context.

What do I do/where do I put the string part to in order to use it anywhere?

HelpPls4112
  • 11
  • 1
  • 3
  • 2
    Add some arguments to the method use them in method. Then when calling the method, pass `TextBox`values to the method. – Reza Aghaei Aug 12 '16 at 00:20
  • Sorry, would you be able to show me that in code? I find it hard to follow without seeing it. Beginner here. Appreciate your input! – HelpPls4112 Aug 12 '16 at 00:23
  • 2
    You should probably read a beginner book on programming, this is a professional programming Q & A website. I'd suggest you add the Name, Age **variables** as **arguments** in OutputNewValues **parameter**. eg `private void OutputNewValues(string name, string age)` or you make these **member variables** of the **class**, by **declaring the variables** outside of the **constructor** method,. If you have difficulty understanding the words in bold, grab a book so we can communicate and you dont find it hard to follow without being spoon fed. – Jeremy Thompson Aug 12 '16 at 00:27
  • You can create a method with this signature: `string OutputNewValues(string name, string age, string dinner)` which returns `string.Format("Hello, {0} you are {1} years old. {2} sounds yummy for dinner!", name, age, dinner)`. Then you can use it this way: `finalOutput.Text = OutputNewValues(tbxName.Text, tbxAge.Text, tbxDinner.Text);` – Reza Aghaei Aug 12 '16 at 00:32
  • Even just reading through this tutorial will be immensely helpful: http://csharp.net-tutorials.com/basics/hello-world/ I saw your other question 5 hours ago and its pretty much asking the same thing... trust me you dont want to keep asking the same question on here in different ways as you will get downvoted and eventually sin binned or blocked from asking. – Jeremy Thompson Aug 12 '16 at 00:34
  • Thanks for the resources. I did not intent on being spoon fed, but was encouraged to use actual code, hence the "request" for seeing code to understand it better. Again, new to this and fine with learning from books - this is just a dive in-thing to get my hands dirty. Thank you for your responses. – HelpPls4112 Aug 12 '16 at 00:36

1 Answers1

0

Fine, here you go.

Passing the textbox values as arguments in the parameter.

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        txtName.Text = "Your name";
        tbxAge.Text = "Your age";
        tbxDinner.Text = "Your dinner";
        OutputNewValues(txtName.Text, tbxAge.Text, tbxDinner.Text);
    }

    private void OutputNewValues(string name, string age, string dinner)
    {
        string answer = "Hello, " + name + " you are " + age + " years old. " + dinner + " sounds yummy for dinner!";
        finalOutput.Text = answer;
    }
}

or using member variables:

public sealed partial class MainPage : Page
{
    private string name = "Your name";
    private string age = "Your age";
    private string dinner = "Your dinner";
    public MainPage()
    {
        this.InitializeComponent();
        OutputNewValues();
    }

    private void OutputNewValues()
    {
        string answer = "Hello, " + name + " you are " + age + " years old. " + dinner + " sounds yummy for dinner!";
        finalOutput.Text = answer;
    }
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thanks, Jeremy. Hmm, for some reason it always spits out the empty string when calling OutputNewValues(); Used your member variables code. – HelpPls4112 Aug 12 '16 at 01:14
  • In design-time add (drag/drop) a button onto the MainPage (double clicking it should add the Click event code) and put code in its Click event to call the method `OutputNewValues()`. Then in run-time after filling out the 3 textboxes clicking the button should populate finalOutput textbox. Once you get this working, then proceed to save the info when the user closes the app, I suggest following this guys answer to the letter: http://stackoverflow.com/a/38905445/495455 – Jeremy Thompson Aug 12 '16 at 01:41