-
April 12th, 2010, 10:10 AM
#1
[RESOLVED] Visual c# - same code four times, one time is enough?
Hello,
I have a visual c# problem, with four buttons that execute different things. I also have two variables that is the same in all four buttons.
For every button I declare those two variables(float var1, float var2), so I do the same code four times, I feel that one time should be enough somehow.
I have this four times in the code( I want to have it just one time):
An ideas?
All code here
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace try18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var1;
var2;
code.... }
private void button3_Click(object sender, EventArgs e)
{
var1;
var2;
code....
}
private void button4_Click(object sender, EventArgs e)
{
var1;
var2;
code....
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void sub_Click(object sender, EventArgs e)
{
var1;
var2;
code......
}
}
}
-
April 12th, 2010, 11:44 AM
#2
Re: Visual c# - same code four times, one time is enough?
It's hard to say without any clue as to what the code is actually doing...
-
April 12th, 2010, 12:01 PM
#3
Re: Visual c# - same code four times, one time is enough?
Sorry, I misread your post. You can't really encapsulate the two variables on their own. As BigEd said, without more information about what your code does, you're stuck with declaring the two variables in all four routines.
-
April 12th, 2010, 05:57 PM
#4
Re: Visual c# - same code four times, one time is enough?
Here's a wild guess....
Make var1 and var2 class fields?
-
April 12th, 2010, 10:21 PM
#5
Re: Visual c# - same code four times, one time is enough?
Wild guess #2.
Put the code in a seperate function and call it from each of the button handlers, passing values for var1 and var2 as parameters to the function.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
April 12th, 2010, 11:12 PM
#6
Re: Visual c# - same code four times, one time is enough?
Wild guess #3. ))
If your button handlers "execute different things", the fact you have variables with the same types and names in each of them is not a sufficient reason to make these variables common.
-
April 13th, 2010, 01:08 AM
#7
Re: Visual c# - same code four times, one time is enough?
Yes, declaring local variables is not something you encapsulate unless you are performing some common operation on them.
-
April 13th, 2010, 05:30 AM
#8
Re: Visual c# - same code four times, one time is enough?
declare those variables as global variables outside your functions one time... and use them in your function
-
April 13th, 2010, 11:52 AM
#9
Re: Visual c# - same code four times, one time is enough?
Originally Posted by vcdebugger
declare those variables as global variables outside your functions one time... and use them in your function
There may be no reason to do this, and then you will just have the added problem of managing global state. We need more info from the OP.
-
April 13th, 2010, 11:56 AM
#10
Re: Visual c# - same code four times, one time is enough?
Originally Posted by BigEd781
There may be no reason to do this, and then you will just have the added problem of managing global state. We need more info from the OP.
Technically, it wouldn't be global state - it would be class instance state.
-
April 13th, 2010, 12:11 PM
#11
Re: Visual c# - same code four times, one time is enough?
Originally Posted by Arjay
Technically, it wouldn't be global state - it would be class instance state.
You know, I was going to come back and edit that out and then work got in the way =P
-
April 13th, 2010, 06:10 PM
#12
Re: Visual c# - same code four times, one time is enough?
This is becoming a 'virtual discussion'. As we still don't have all of the facts from the originator of this thread...
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
April 14th, 2010, 08:31 AM
#13
Re: Visual c# - same code four times, one time is enough?
Hello, me again, starter of this thread.
I did write wrong(sorry about that) in my orginal posting, the correct code is
int var1;
int var2;
Anyway, depending on which button is pressed, different things(like a calculator, +,-,*,/) is happening and the result presented. But I am not looking for the code for that stuff, I am just looking for a good way to avoid to write the same code over and over(next time it's maybe much more variables).
Maybe I am trying to be to clever.
-
April 14th, 2010, 09:26 AM
#14
Re: Visual c# - same code four times, one time is enough?
We still don't know what your repetitive code is. Can you show us exactly what you mean with your actual code?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
April 14th, 2010, 09:54 AM
#15
Re: Visual c# - same code four times, one time is enough?
Hello
My repetitive code is:
int var1;
int var2;
I have that code four times.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|