A noobs question :)

+1 Kerem Etes · January 5, 2015
Hi , just wanted to ask a real quick question about C#

I'm using Microsoft Visual C# 2010 Express and on a form I have a textbox and a button. I just simply want to assign a variable to the text box's user input so that the variable will change depending on what I type in the text box. I couldn't figure out what the correct syntax is. Hope you can help 


I basically want to type a name in the text box then press the button and depending on what the text box input is the message will print out a message but as a variable.

Just declaring a variable then trying to assign it to the text box didn't work.

/images/forum/upload/2015-01-05/acabf4c447d0b6097e048311aabcb96f.jpg

Post a Reply

Replies

Oldest  Newest  Rating
0 Gabriel Felipe · January 6, 2015
Just double-click the button and type: 


            string msg;
            msg = textBox1.Text;
            MessageBox.Show(msg);



and if you want to use this variable outside of the button, just declare it directly in the main class as public  like that: 


public partial class Form1 : Form
    {
        public string msg;
        


Hope i helped ;D
0 Kerem Etes · January 6, 2015
Thanks a lot, Yes it helped :) I was going crazy for such a simple thing :).
0 Kerem Etes · January 10, 2015
Ok here is my second noob's question :). I have two instances of a code and I know for a fact the second code method is applied wrong (by me ). I want to know the reason why I cant  double the intFromString by 2 in the second screenshot I added. Is it because my intFromString's value is actually referred and not changed  ? Hope someone can enlighten me :) 

Thanks .

CORRECT VERSION
/images/forum/upload/2015-01-10/e6dffd6026f4b6a21fee28c12f716996.jpg



BUGGED VERSION




/images/forum/upload/2015-01-10/c4c876e8f26a0732c5985e8143488ae0.jpg
0 Eugene Botma · January 11, 2015
Here it would be very beneficial to read the error message. It should say something like " 'projectname.output(string)' has some invalid arguments", then below that: "Argument 1: cannot convert from int to string".

This means that the Output method requires a string as an argument, but you're giving it an int. Simply convert or parse the int to a string.

Output((intFromString * 2).ToString());

In visual studio, if you type the name of the method and then enter the opening bracket, a popup shows that should explain what the method does (if available) and what the arguments should be. It's always a good idea to look at the information when using a method.
0 Kerem Etes · January 11, 2015
Thanks Eugene. 

Sometimes I go blind to see the simplest things :) . You're right I should have added .ToString().
-1 Daniel Nachum · January 15, 2015
you could also do this:

Output(""+intFromString *2);
  • 1

C#

107,169 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators