CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Feb 2010
    Posts
    13

    [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):
    Code:
    var1;
    var2;
    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......
    
            }
        }
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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...

  3. #3
    Join Date
    Apr 2010
    Location
    Dayton, OH
    Posts
    16

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Visual c# - same code four times, one time is enough?

    Here's a wild guess....


    Make var1 and var2 class fields?

  5. #5
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  6. #6
    Join Date
    Dec 2008
    Posts
    144

    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  8. #8
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    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

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Visual c# - same code four times, one time is enough?

    Quote Originally Posted by vcdebugger View Post
    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.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Visual c# - same code four times, one time is enough?

    Quote Originally Posted by BigEd781 View Post
    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.

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    Talking Re: Visual c# - same code four times, one time is enough?

    Quote Originally Posted by Arjay View Post
    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

  12. #12
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  13. #13
    Join Date
    Feb 2010
    Posts
    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.

  14. #14
    Join Date
    May 2007
    Posts
    1,546

    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.

  15. #15
    Join Date
    Feb 2010
    Posts
    13

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured