Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to REFACTOR this? 3

Status
Not open for further replies.

G00GLER

Instructor
May 17, 2005
57
0
0
US

basically i have 5 stoplights and when user changes the from red to green i clear all images of that stop light with the clearLights function but since I have 5 stoplights i created 5 ClearLights Functions, is there a way to just have one and pass the integer of through so I can change visibility of the correct stoplight?

this code works, i just know there is got to be a better way to do it, combining 5 functions into one OO one
Code:
private void ClearLights0()
{
   GreenLight0.Visible=false;
   YellowLight0.Visible = false;
   RedLight0.Visible = false;
}
private void ClearLights1()
{
   GreenLight1.Visible=false;
   YellowLight1.Visible = false;
   RedLight1.Visible = false;
}
etc...
 
i should clarify GreenLight0 (all the lights) are images on a winform.

I tried
private void ClearLights(int i)
{
("GreenLight"+i).Visible=false;
("YellowLight"+i).Visible=false;
("RedLight"+i).Visible=false;
}

But obviously there is something more to it.
 
You should make the stoplight an object that has a clear stoplight function.

Then you just call the clearlight function on the correct object and it is done. Write it one time.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
As a GUI guy, I would put your stoplight into a UserControl and expose a ClearLight() function - just as Guru7777 said.

You then contain an Arraylist, Hashtable, or ClearLight[] array and index the light using your index (int i in your case)
 
how do i define stoplight as an object?

can you recommend a good book, or code snippet?

thank you both
 
This is just off the top of my head - but I would create a UserControl with 3 buttons on it, btnRed, btnYellow, btnGreen. You would then create your usercontrol on your form.

Then you can use an arraylist, array or hashtable to call your lights.

Here's the stop light class - I just wrote this in the Forum editor so I don't know if it would compile.

Code:
public class StopLight
{
  private Button btnRed;
  private Button btnYellow;
  private Button btnGreen;

  public enum LightColor
   { RED, YELLOW, GREEN }

  public StopLight()
  {
     InitializeComponents();
     TurnOffAllLights();
  }

  private void TurnOffAllLights()
  {
    this.btnRed.BackColor = Color.DarkRed;
    this.btnYellow.BackColor = Color.Gray;
    this.btnGreen.BackColor = Color.DarkGreen;
  }

  public void SetLight(LightColor color)
  {
    TurnOffAllLights();

    switch(color)
    {
        case LightColor.Red:
        {
           this.btnRed.BackColor = Color.Red;
           break;
        }
        case LightColor.Yellow:
        {
           this.btnYellow.BackColor = Color.Yellow;
           break;
        }
        case LightColor.Green:
        {
           this.btnGreen.BackColor = Color.Green;
           break;
        }
        default:
        {
            Console.WriteLine("Problem with lights!");
        }
    }
  }
}


Then your form would use the following:

Code:
using System.Collections;

StopLight[] lights = new StopLight[4];

public Form1()
{
   for (int i = 0; i < lights.Length; i++)
   {
      lights[i] = new StopLight();  
   }

   lights[0].SetLight(StopLight.LightColor.Red);
}

lights[0].SetLight(StopLight.LightColor.Red);
This allows you to set the light color of one of the 4 lights. In this case, the first light in the list.


Hope that gets you started.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top