Hi everybody please help me on how to code this in C#.

I have an ASP.Net Web project and needs some C# code behind.
Working some variables:

In my VB.Net it looks like this:

Dim str1 as String

Private Sub Button1_Click(...)
 str1 = "some text" 'This will be assigned to str1 
End Sub

Private Sub Button2_Click(...)
'I will use the value of str1 here Ex.

Messagebox.Show("" & str1 &"")

End Sub

The string is declared outside the Sub so it can be accessed anywhere within the form.
how to do like that in C#?

public partial class _Default : System.Web.UI.Page 
{
    string str;

    protected void Button1_Click(object sender, EventArgs e)
    {
        str = "Hello World";

    }
}

That won't work

public partial class _Default : System.Web.UI.Page
{
  string str;
  protected void Button1_Click(object sender, EventArgs e)
   {
        str = "Hello World";
   }
    protected void Button2_Click(object sender, EventArgs e)
   {
        Messagebox.Show("" + str +"");

      //the box returns empty
   }
}

This should work... I'm assuming you're working a windows forms app and not a web app based on your use of MessageBox.

As long as the first line is in the initial declarations of your code you should be able to access it from any function within.

If you need to access it from outside the class entirely (ie from another class or in the case of a web app from the asp code itself) then you'll need to add 'public' in front of the declaration for outside code to be able to see the variable.

As I look at it after the fact it's almost identical to the code provided by the previous person so I'm not sure why it wouldn't be working for you.

string str1 = "";

protected void Button1_Click(object sender, EventArgs e)
{
    str1 = "some text";
}

protected void Button2_Click(object sender, EventArgs e)
{
    MessageBox.Show(str1);
}
string str1;

    
    private void Button1_Click(void ...) {
        str1 = "some text";
    }
    
    void Button1_Click(void ...) {
        str1 = "some text";
    }
    
    private void Button2_Click(void ...) {
        // I will use the value of str1 here Ex.
        Messagebox.Show(("" 
                        + (str1 + "")));
    }

It's a Web from.... it's there on my first post. tnx

This should work... I'm assuming you're working a windows forms app and not a web app based on your use of MessageBox.

As long as the first line is in the initial declarations of your code you should be able to access it from any function within.

If you need to access it from outside the class entirely (ie from another class or in the case of a web app from the asp code itself) then you'll need to add 'public' in front of the declaration for outside code to be able to see the variable.

As I look at it after the fact it's almost identical to the code provided by the previous person so I'm not sure why it wouldn't be working for you.

string str1 = "";

protected void Button1_Click(object sender, EventArgs e)
{
    str1 = "some text";
}

protected void Button2_Click(object sender, EventArgs e)
{
    MessageBox.Show(str1);
}

It's a Web from.... it's there on my first post. tnx

I must be missing something then because I've never seen MessageBox utilized in a web form. Though I have seen similar effect with the use of JavaScript.

In either event, the declaration/usage is correct for the variable in the 2-3 examples given but not seeing the actual physical use the variable is going to be given is throwing me off.

If all you want is to display the results of a passed variable, however, you can always use <ASP:Label> or textbox for output onto the screen or I believe that an Alert tag will give the same effect as a MessageBox as such (just format the alert tag into the button2_click process on the front end and have it utilize a value of a hidden label for it's input):

<script type="text/javascript">
<!--

alert ("This is a JavaScript ALERT box.")

// -->
</script>

Like the other posters, I'm not sure what you're doing with a Messagebox in a webform. But if it really is working, then declaring str1 as static, like so, should sort it out I think...

protected static string str1;

protected void Button1_Click(object sender, EventArgs e)
{
    str1 = "some text";
}

protected void Button2_Click(object sender, EventArgs e)
{
    MessageBox.Show(str1);
}

c# coding....

partial class Form1 

String str1 ;

Private Sub Button1_Click(...)
 str1 = "some text";
End Sub

Private Sub Button2_Click(...)

Messagebox.Show(str1);

End Sub
commented: Put correct code here. -2

c# coding....

partial class Form1

String str1 ;

Private Sub Button1_Click(...)
str1 = "some text";
End Sub

Private Sub Button2_Click(...)

Messagebox.Show(str1);

End Sub

That certainly looks like VB coding and looks almost identical to the original post. *shrug* I still say the culprit is using Messagebox in a webapp but what do I know ;) it's 3:30am here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.