C
C#10mo ago
Cam

✅ Help with Simplifying Method further

Anyway to make this code smaller so that i dont have to write "new List<Avalonia.Point>" and "new Avalonia.Point(x,y)" so many times? Preferably wanting a function where i can parse as many points into the method as i need. I have already added a method to create the line but it still contains too much rubbish repetitive code
c#
public partial class Newmarket : UserControl
{
public List<Polyline> newmarketTurnouts = new List<Polyline>();
public bool normal = true;
public Newmarket()
{
InitializeComponent();
buildPanel();
PanelGrid panelGrid = new PanelGrid(NewmarketPanel);
}

public void buildPanel() // Build Dynamic Points to Panel
{
newmarketTurnouts.Add(drawToPanel(new List<Avalonia.Point> { new Avalonia.Point(100, 480), new Avalonia.Point(112, 480) })); // 251B - Down Main
newmarketTurnouts.Add(drawToPanel(new List<Avalonia.Point> { new Avalonia.Point(138, 420), new Avalonia.Point(150, 420) })); // 251A - Up Main
newmarketTurnouts.Add(drawToPanel(new List<Avalonia.Point> { new Avalonia.Point(150, 430), new Avalonia.Point(140, 430), new Avalonia.Point(110, 470), new Avalonia.Point(100, 470) })); // 251 Crossover


NewmarketPanel.Children.Add(newmarketTurnouts);
}
public Polyline drawToPanel(List<Avalonia.Point> points) // Create new Polyline
{
Polyline newObject = new Polyline
{
Stroke = Avalonia.Media.Brushes.Red,
StrokeThickness = 2
};
var n = points.Count;
for (int i = 0; i < n; i++)
{
newObject.Points.Add(points[i]);
}
return newObject;
}
}
c#
public partial class Newmarket : UserControl
{
public List<Polyline> newmarketTurnouts = new List<Polyline>();
public bool normal = true;
public Newmarket()
{
InitializeComponent();
buildPanel();
PanelGrid panelGrid = new PanelGrid(NewmarketPanel);
}

public void buildPanel() // Build Dynamic Points to Panel
{
newmarketTurnouts.Add(drawToPanel(new List<Avalonia.Point> { new Avalonia.Point(100, 480), new Avalonia.Point(112, 480) })); // 251B - Down Main
newmarketTurnouts.Add(drawToPanel(new List<Avalonia.Point> { new Avalonia.Point(138, 420), new Avalonia.Point(150, 420) })); // 251A - Up Main
newmarketTurnouts.Add(drawToPanel(new List<Avalonia.Point> { new Avalonia.Point(150, 430), new Avalonia.Point(140, 430), new Avalonia.Point(110, 470), new Avalonia.Point(100, 470) })); // 251 Crossover


NewmarketPanel.Children.Add(newmarketTurnouts);
}
public Polyline drawToPanel(List<Avalonia.Point> points) // Create new Polyline
{
Polyline newObject = new Polyline
{
Stroke = Avalonia.Media.Brushes.Red,
StrokeThickness = 2
};
var n = points.Count;
for (int i = 0; i < n; i++)
{
newObject.Points.Add(points[i]);
}
return newObject;
}
}
5 Replies
Jimmacle
Jimmacle10mo ago
1. use a collection initializer instead of new List<T> 2. use target-typed new for the elements? 3. write a helper method that takes in a collection of tuples and returns a list of points? 3 like List<Avalonia.Point> FromTuples(params (int, int)[] tuples) looking closely it doesn't look like the list is necessary at all, you're just using that as a way to get a collection of points into the other method so you could do 3 by just putting that conversion logic in that method public Polyline drawToPanel(params (int, int)[] points)
JochCool
JochCool10mo ago
If you are using Visual Studio, it should already be giving you suggestions for simplifying the code that creates the list
Cam
CamOP10mo ago
I do get the “Can be simplified” stuff but this one didn’t have that. It just looks too ugly having new this new that all over the place
Angius
Angius10mo ago
Instead of looping over points to add them to newObject's collection, use the initializer and LINQ Assuming LINQ is even necessary here
Cam
CamOP10mo ago
I’m fairly new to Avalonia and even list creation in general so it was honestly just a working method at the time I see what you mean. Putting a list into another list can be done inside the method rather than returning it.
Want results from more Discord servers?
Add your server