C
C#7mo ago
Shovel

help with this code c# windows forms

how do i make it work?
No description
41 Replies
reflectronic
reflectronic7mo 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
ShovelOP7mo ago
thanks, i totally forgot about that
Jimmacle
Jimmacle7mo ago
please stop making additional threads for the same problem this is the 3rd one
Shovel
ShovelOP7mo ago
it's a different one
Jimmacle
Jimmacle7mo ago
it looks like the same thing z showed you an example of in the last thread
Jimmacle
Jimmacle7mo ago
No description
Shovel
ShovelOP7mo 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
Jimmacle7mo ago
you can say so in that thread
Shovel
ShovelOP7mo 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
Jimmacle7mo ago
making new threads for every small change makes it hard to follow
Shovel
ShovelOP7mo ago
which is better
Jimmacle
Jimmacle7mo ago
to me, neither i see no reason to convert them to and from integers if you just want to swap the contents
Shovel
ShovelOP7mo ago
does it go up?
Jimmacle
Jimmacle7mo ago
?
Shovel
ShovelOP7mo ago
my post
Jimmacle
Jimmacle7mo 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
ShovelOP7mo ago
what would be more professional?
Jimmacle
Jimmacle7mo 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
ShovelOP7mo ago
No description
Jimmacle
Jimmacle7mo ago
yes, this is a picture of code
Shovel
ShovelOP7mo ago
there's an error..
Jimmacle
Jimmacle7mo ago
there is indeed
Shovel
ShovelOP7mo ago
i removed the conversion as you said and got an error
Jimmacle
Jimmacle7mo ago
writing code involves understanding the code you're writing do you know what that error means?
Shovel
ShovelOP7mo ago
no
Jimmacle
Jimmacle7mo 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
ShovelOP7mo ago
what value should I give it to I'm swapping numbers
Jimmacle
Jimmacle7mo 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
ShovelOP7mo ago
they can be used for numbers
Jimmacle
Jimmacle7mo 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
ShovelOP7mo ago
which is?
Jimmacle
Jimmacle7mo ago
...a single extra variable it wasn't a trick question
Shovel
ShovelOP7mo ago
for some reason you make me feel stupid lol u don't give direct answers
Jimmacle
Jimmacle7mo ago
correct
Shovel
ShovelOP7mo ago
I want to get spoonfed
Jimmacle
Jimmacle7mo ago
most people here don't do that
Shovel
ShovelOP7mo ago
idc about c# I'm learning something else but I have to cuz of uni
Jimmacle
Jimmacle7mo 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
ShovelOP7mo ago
I already solved it, just wanted to do it more professionally but whatever
Jimmacle
Jimmacle7mo 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
ShovelOP7mo ago
wow that's much simpler text boxes almost always get converted so I'm used to it
Want results from more Discord servers?
Add your server