[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......
}
}
}
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...
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.
Re: Visual c# - same code four times, one time is enough?
Here's a wild guess....
Make var1 and var2 class fields?
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.
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.
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.
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
Re: Visual c# - same code four times, one time is enough?
Quote:
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.
Re: Visual c# - same code four times, one time is enough?
Quote:
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. :)
Re: Visual c# - same code four times, one time is enough?
Quote:
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
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...
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. :)
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?
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.