0

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#?

6
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by Lusiphur
0
public partial class _Default : System.Web.UI.Page 
{
    string str;

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

    }
}
0

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
   }
}
0

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);
}

Edited by Lusiphur: n/a

0
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 + "")));
    }
0

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);
}

Edited by c0deFr3aK: n/a

0

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>
0

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);
}
-1

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

Edited by __avd: Added [code] tags.

Votes + Comments
Put correct code here.
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.