dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: I can't string from somewhere else

  1. #1
    Join Date
    Oct 2010
    Location
    Net 4.0 / VS 2010
    Posts
    43

    Angry I can't string from somewhere else

    So what I'm saying is I can't use the same string when it is in another clicked event. Why?

  2. #2
    Join Date
    Oct 2010
    Location
    Net 4.0 / VS 2010
    Posts
    43

    Unhappy Re: I can't string from somewhere else

    Like... if in the first Button Clicked event, I say:

    string LOL = "LOL";

    and then in the second button2_clicked event I can't use LOL.

    What's up with that?

  3. #3
    Join Date
    Oct 2010
    Posts
    3

    Re: I can't string from somewhere else

    Because LOL is declared inside the First Button event its scope is local. If you need to use it elsewhere in the code declare it global.

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    string LOL2 = string.Empty;//accessible because it is declared globally

    private void button1_Click(object sender, EventArgs e)
    {
    string LOL = "LOL";//only accessible inside this event/method
    LOL2 = "LOL2";
    }

    private void button2_Click(object sender, EventArgs e)
    {
    //LOL does not exist
    LOL2 = "Hello World";
    }

  4. #4
    Join Date
    Oct 2010
    Location
    Net 4.0 / VS 2010
    Posts
    43

    Re: I can't string from somewhere else

    And how do I do that? I'm saying I made everything public, but it still doesn't work. Because it's like this:

    string.text = textbox1.text

    Even if I declare it at the top of the script, it still won't have the same value.

  5. #5
    Join Date
    Oct 2010
    Location
    Net 4.0 / VS 2010
    Posts
    43

    Thumbs down Re: I can't string from somewhere else

    Uh... HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!

  6. #6
    Join Date
    Oct 2010
    Posts
    3

    Re: I can't string from somewhere else

    If you declare at the top of your code behind(make sure you are not inside any methods/events)
    ,you can retireve or set the value of SomeString inside any method or event in the code behind.

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    //declare SomeString globally and set its intial value to ""
    public string SomeString = "";


    private void button1_Click(object sender, EventArgs e)
    {
    SomeString = textbox1.text;
    }

    //If you click button2 before you click button1 SomeString will be set to ""
    //because its value will not have been set yet.
    private void button2_Click(object sender, EventArgs e)
    {
    textbox2.text = SomeString;
    }

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,227

    Re: I can't string from somewhere else

    Quote Originally Posted by Megaleo234 View Post
    Uh... HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Uh... take some time and work through an online C# tutorial. Within working through a few chapters, you'll have an understanding of the difference between the scope of a class field and local variable.

  8. #8
    Join Date
    Oct 2010
    Posts
    12

    Re: I can't string from somewhere else

    You should familiarise yourself with the idea of Scope of variables. It will save you some major headaches down the road.

    The long and short of it is:

    Anything within the proceeding curly brackets is within the scope until it is closed.

    for example:


    public partial class Form1 : Form
    {

    string some_Text_thatIsAGlobalVariable = "oh bugger";

    private void test(int local_scope_Variable)
    {
    //within these squigly brackets you could use local_scope_variable OR some_Text_ThatIsAGlobalVariable.
    // BUT outside of these squiggly brackets you could not use "local_scope_Variable". It exists ONLY within its brackets. This is a very rudimentary example but should serve.
    } //this is the end of test function/method or whatever you care to call it.


    } //this is the end of the partial class

    I hope this helps.

    Regards.

  9. #9
    Join Date
    Oct 2010
    Location
    Net 4.0 / VS 2010
    Posts
    43

    Re: I can't string from somewhere else

    I figured out sorry, I should have made it resolved... How DO I make it resolved anyways?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)