C
C#2mo ago
Shovel

help with this code c# windows forms

how do i make it work?
No description
41 Replies
reflectronic
reflectronic2mo ago
you need to convert it to a string, which you can do by doing a.ToString() that is pretty strange though, you are basically just setting textBox1.Text to itself
Shovel
Shovel2mo ago
thanks, i totally forgot about that
Jimmacle
Jimmacle2mo ago
please stop making additional threads for the same problem this is the 3rd one
Shovel
Shovel2mo ago
it's a different one
Jimmacle
Jimmacle2mo ago
it looks like the same thing z showed you an example of in the last thread
Jimmacle
Jimmacle2mo ago
No description
Shovel
Shovel2mo ago
his example didn't apply int a, b, c; if (int.TryParse(textBox1.Text, out a) && int.TryParse(textBox2.Text, out b)) { textBox1.Text = a.ToString(); textBox2.Text = b.ToString(); c = a; textBox1.Text = b.ToString(); textBox2.Text = c.ToString(); } _
Jimmacle
Jimmacle2mo ago
you can say so in that thread
Shovel
Shovel2mo ago
int a = Convert.ToInt32(textBox1.Text); int b = Convert.ToInt32(textBox2.Text); int c; textBox1.Text = a.ToString(); textBox2.Text = b.ToString(); c = a; textBox1.Text = b.ToString(); textBox2.Text = c.ToString();
Jimmacle
Jimmacle2mo ago
making new threads for every small change makes it hard to follow
Shovel
Shovel2mo ago
which is better
Jimmacle
Jimmacle2mo ago
to me, neither i see no reason to convert them to and from integers if you just want to swap the contents
Shovel
Shovel2mo ago
does it go up?
Jimmacle
Jimmacle2mo ago
?
Shovel
Shovel2mo ago
my post
Jimmacle
Jimmacle2mo ago
the same people that responded will still be there leaving them hanging to make a new post just makes it slower for you to get answers
Shovel
Shovel2mo ago
what would be more professional?
Jimmacle
Jimmacle2mo ago
remove all the unnecessary conversions you can swap 2 variables in 3 lines, or even make it a single line if you use a trick with tuples
Shovel
Shovel2mo ago
No description
Jimmacle
Jimmacle2mo ago
yes, this is a picture of code
Shovel
Shovel2mo ago
there's an error..
Jimmacle
Jimmacle2mo ago
there is indeed
Shovel
Shovel2mo ago
i removed the conversion as you said and got an error
Jimmacle
Jimmacle2mo ago
writing code involves understanding the code you're writing do you know what that error means?
Shovel
Shovel2mo ago
no
Jimmacle
Jimmacle2mo ago
you declared a variable but never assigned it a value in this code a, b, and c are all uninitialized you probably don't want ints either, because you're working with text
Shovel
Shovel2mo ago
what value should I give it to I'm swapping numbers
Jimmacle
Jimmacle2mo ago
values of text boxes aren't numbers, they're strings unless you need to do math with them there's no reason to parse them to actual numbers
Shovel
Shovel2mo ago
they can be used for numbers
Jimmacle
Jimmacle2mo ago
to solve the "swap 2 numbers" problem you don't actually need to make them numbers, because the problem is really "swap 2 variables" you only need a single extra variable to solve this problem
Shovel
Shovel2mo ago
which is?
Jimmacle
Jimmacle2mo ago
...a single extra variable it wasn't a trick question
Shovel
Shovel2mo ago
for some reason you make me feel stupid lol u don't give direct answers
Jimmacle
Jimmacle2mo ago
correct
Shovel
Shovel2mo ago
I want to get spoonfed
Jimmacle
Jimmacle2mo ago
most people here don't do that
Shovel
Shovel2mo ago
idc about c# I'm learning something else but I have to cuz of uni
Jimmacle
Jimmacle2mo ago
unfortunately that attitude won't get you answers here better off googling problems like this if you just want to be given the answer
Shovel
Shovel2mo ago
I already solved it, just wanted to do it more professionally but whatever
Jimmacle
Jimmacle2mo ago
fwiw,
var temp = textBox1.Text;
textBox1.Text = textBox2.Text;
textBox2.Text = temp;
var temp = textBox1.Text;
textBox1.Text = textBox2.Text;
textBox2.Text = temp;
is the answer i was trying to lead you to or even shorter
(textBox1.Text, textBox2.Text) = (textBox2.Text, textBox1.Text);
(textBox1.Text, textBox2.Text) = (textBox2.Text, textBox1.Text);
Shovel
Shovel2mo ago
wow that's much simpler text boxes almost always get converted so I'm used to it