Hi!
I have done mostly C# and now I am programming VB.NET and I have a few questions on how to optimize my code:
First
This feels kind of bloated, is there better syntax to handle this? The scenario is in the repository where an enum is converted to a number which is the type expected in the db, and sadly the db is not based on binary representation so I have to manually convert the value before saving the performanceTest object with Entity FrameworkCode:If performanceTest.TestResultUnits = 4 Then performanceTest.TestResultUnits = 12 End If If performanceTest.FlowScaleUnits = 4 Then performanceTest.FlowScaleUnits = 12 End If If performanceTest.HeadScaleUnits = 4 Then performanceTest.HeadScaleUnits = 12 End If If performanceTest.PowerScaleUnits = 4 Then performanceTest.PowerScaleUnits = 12 End If
Next
The above code is used to set headers for a datagrid in WPF. The headertext is bound to FlowLabel, HeadLabel and PowerLabel. The code works but also feels bloated with too many line sof code for something seamingly trivial.Code:Private Sub SetHeaders(selectedUnit As DisplayUnits) Dim flow As String = String.Empty Dim head As String = String.Empty Dim power As String = String.Empty If selectedUnit = DisplayUnits.Metric Then flow = "l/s" head = "m" power = "kW" ElseIf selectedUnit = DisplayUnits.Us Then flow = "USGPM" head = "ft" power = "kW" End If FlowLabel = String.Format(My.Resources.FlowLabel, flow) HeadLabel = String.Format(My.Resources.HeadLabel, head) PowerLabel = String.Format(My.Resources.PowerLabel, power) End Sub
Finally...
Two properties are used for each unit to determine if it is visible in a plot component. To set this I have to check the model object for each property. It is very teadious and looks like a lot of repeated code, or un-clever syntax.Code:ShowQAxis1 = PerformanceTest.FlowScaleUnits <> DisplayUnits.Us ShowQAxis2 = PerformanceTest.FlowScaleUnits <> DisplayUnits.Metric ShowPAxis1 = PerformanceTest.PowerScaleUnits <> DisplayUnits.Us ShowPAxis2 = PerformanceTest.PowerScaleUnits <> DisplayUnits.Metric ShowHAxis1 = PerformanceTest.HeadScaleUnits <> DisplayUnits.Us ShowHAxis2 = PerformanceTest.HeadScaleUnits <> DisplayUnits.Metric
Maybe the above code is fine and optimized, but I can't help but thinking that is a better way to solve it...
/H




Reply With Quote