0

I'm making a 7-segment clock in C# and I've come across a problem. I usually code in PHP and this problem is confusing to me.

Let's say I'm trying to assign locations to panels in C# So first use this code:

Panel a = new Panel();
Panel b = new Panel();
Panel c = new Panel();
Panel d = new Panel();
Panel e = new Panel();
Panel f = new Panel();
Panel g = new Panel();

I have an array:

int[,] locationArray = new int[,] {
    {10,20},
    {90,30},
    {90,125},
    {10,210},
    {5,125},
    {5,30},
    {10,115}
};

Than I loop through that array:

for (int d = 0; d < locationArray.GetLength(0); d++)
{
    for (int j = 0; j < locationArray.GetLength(1); j++)
    {
        int locationY = locationArray[i, j];
        int locationX = locationArray[i, 0];

        /* a has to be variable so that I could use one of the panels from a to g. */
        a.Location = new Point(locationX, locationY);
    }
}

So is it possible to make a.Location a variable from a to g without making it complicated/messy like a switch case or if statement.

I wanted to just iterate through an array above the first array to choose the letter. But it doesn't seem to work that way.

2
  • 1
    You could put them in an array: var panels = new[] { a, b, c, ... };. Then do panels[i].Location = new Point.... You also may not need to assign variables to them, so you could just do var panels = new[] { new Panel(), new Panel(), ..., new Panel() }; Commented Aug 26, 2015 at 8:53
  • Or maybe simpler... var panels = Enumerable.Range(1,7).Select(e => new Panel()).ToList(); Commented Aug 26, 2015 at 8:54

3 Answers 3

4

You can add each panel to an array:

Panel[] panels = new [] { a, b, c, d, e, f, g };

Therefore, you can access a panel by its index inside your loops:

panels[d].Location = new Point(locationX, locationY);
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of having separate variables a,b,c,d,e,f,g, try maintaining an array or dictionary of panels, and then everything becomes much simpler:

var panels = new Panel[] { ... }

or

var panel = new Dictionary<int, Panel> { ... }

or

var panel = new Dictionary<string, Panel> { ... }

Another tip that will make your code more readable: replace the two-dimensional array int[,] with an array of location objects:

var locations = new Location[] { ... }

2 Comments

Thanks for your answer, But how do I add values to them? With the array it is: { {} , {} } how would I do it for the Location?
var locations = new [] { new Location(10,20), new Location(90,30) }; and you just need to add a constructor to the Location class that accepts two arguments and assigns them to fields. Take a look at the Point class which is part of the framework, it might suit your needs.
1

You can use a list:

Panel a = new Panel();
Panel b = new Panel();
Panel c = new Panel();
Panel d = new Panel();
Panel e = new Panel();
Panel f = new Panel();
Panel g = new Panel();


List<Panel> listOfPanels = new List<Panel>(){a, b, c, d, e, f, g};

foreach (Panel panel in listOfPanels)
{
    // Your stuff goes here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.