vb.net - is there a better way to do this (code)?
vb.net - is there a better way to do this (code)?
Author
Discussion

TonyRPH

Original Poster:

13,560 posts

200 months

Wednesday 31st January 2018
quotequote all
I'm sending data (frequencies) to an oscilloscope, and I need to change the timebase every so often.

So I came up with this code: (just used to display the output at the moment)

In the snippet below, the variable index contains the frequency. So I'm looking at setting the timebase for a given range of frequencies.

Is there a better way than "If index >= 9 And index <= 41 Then" ?

This seems a bit cumbersome.


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim freq2 As Decimal
Dim period As Decimal
While index <= 1050000
freq2 = Math.Round(index, 2)
ListBox1.Items.Add(freq2)
freqs.Add(index)
index = index * 2 ^ (1 / 3)
' ****** the code below is that which I'm looking to improve *******
If index >= 9 And index <= 41 Then
ListBox4.Items.Add("50mS" & " " & freq2)
End If
If index >= 49 And index <= 161 Then
ListBox4.Items.Add("5mS" & " " & freq2)
End If
If index >= 162 And index <= 321 Then
ListBox4.Items.Add("2mS" & " " & freq2)
End If
If index >= 322 And index <= 1600 Then
ListBox4.Items.Add("0.5mS" & " " & freq2)
End If
If index >= 1800 And index <= 4001 Then
ListBox4.Items.Add("0.2mS" & " " & freq2)
End If
If index >= 4002 And index <= 16200 Then
ListBox4.Items.Add("50uS" & " " & freq2)
End If
If index >= 16300 And index <= 32100 Then
ListBox4.Items.Add("20uS" & " " & freq2)
End If
If index >= 39000 And index <= 81000 Then
ListBox4.Items.Add("10uS" & " " & freq2)
End If
If index >= 82000 And index <= 1000000 Then
ListBox4.Items.Add("5uS" & " " & freq2)
End If
' **********************************************************************************
End While
End Sub

fido

18,941 posts

287 months

Wednesday 31st January 2018
quotequote all
So you're basically adding some text to a ListBox depending on which range a value falls into?

I would separate the code that:-

a) works out the text e.g. text = "5uS" & " " & freq2 , and possibly a Boolean to indicate that text is to be added
b) ListBox4.Items.Add(text) \\ do this at the end

BryanR

73 posts

219 months

Wednesday 31st January 2018
quotequote all
You cut down the code a bit with use of the Case Select.


Dim number As Integer = 8
Select Case number
Case 1 To 5
Debug.WriteLine("Between 1 and 5, inclusive")
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8
Debug.WriteLine("Between 6 and 8, inclusive")
Case 9 To 10
Debug.WriteLine("Equal to 9 or 10")
Case Else
Debug.WriteLine("Not between 1 and 10, inclusive")
End Select


andycambo

1,077 posts

206 months

Wednesday 31st January 2018
quotequote all
You could tidy it up a little (using something like below) or use a case as an alternative


Private Sub WithinBounds(min, max, value)
return value >= min && value <=max
End Sub

**CHANGE EXISITING CODE TO SOMETHINGLIKE**
Dim result As String
If WithinBounds(9, 41, index) Then
result = 50mS
End If
If WithinBounds(49, 161, index) Then
result = 5mS
End If
etc.

ListBox4.Items.Add(result & " " & freq2)

toon10

7,160 posts

189 months

Wednesday 31st January 2018
quotequote all
BryanR said:
You cut down the code a bit with use of the Case Select.


Dim number As Integer = 8
Select Case number
Case 1 To 5
Debug.WriteLine("Between 1 and 5, inclusive")
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8
Debug.WriteLine("Between 6 and 8, inclusive")
Case 9 To 10
Debug.WriteLine("Equal to 9 or 10")
Case Else
Debug.WriteLine("Not between 1 and 10, inclusive")
End Select
Beat me to it. Much more efficient to use CASE and not have to reconcile each IF statement one after the other.

maffski

1,905 posts

191 months

Wednesday 31st January 2018
quotequote all
Any reason for the gaps (e.g. if index = 42?) Assuming not I would go for...


if index <= 41 then
out = "50ms"
elseif index <= 161 then
out = "5ms"
elseif ... etc...

end if
ListBox4IsAnAwfulName.items.add(out & " " & freq2)





TonyRPH

Original Poster:

13,560 posts

200 months

Wednesday 31st January 2018
quotequote all
BryanR said:
Helpful stuff
Thanks, that seems much cleaner and easier to understand.

Also learnt something new (DebugWriteLine) - much easier than writing things out to listboxes and the like!!

Every day a school day and all that!

andycambo said:
More helpful stuff.
Thanks, that's a useful idea as well.

I chose to go with the case statement idea - I've used something very similar in PHP as well.





TonyRPH

Original Poster:

13,560 posts

200 months

Wednesday 31st January 2018
quotequote all
maffski said:
Any reason for the gaps (e.g. if index = 42?) Assuming not I would go for...

<snip>
ListBox4IsAnAwfulName.items.add(out & " " & freq2)
<snip>
Isn't it just!!! ^^^^^^^

There are breaks because the frequency rises in 3rd octave steps (and will potentially be 1/12 and 1/24 octave steps as well).

I have searched to see if the Timebase for the 'scope can be derived directly from the frequency, but there doesn't appear to be any magic formula for that.

Timebase (time) = (wave form) Period and Period = 1/frequency - however I only have a limited range of timebase settings, so I can't calculate the timebase mathematically - so have to create static entries for each given range of frequencies.



maffski

1,905 posts

191 months

Wednesday 31st January 2018
quotequote all
TonyRPH said:
There are breaks because the frequency rises in 3rd octave steps (and will potentially be 1/12 and 1/24 octave steps as well).
Does this mean your code will never see an input value between 42 and 48? In which case you don't need the gatekeeper to stop it being treated the same as 49 - which will allow you to simplify the model.




TonyRPH

Original Poster:

13,560 posts

200 months

Wednesday 31st January 2018
quotequote all
maffski said:
Does this mean your code will never see an input value between 42 and 48? In which case you don't need the gatekeeper to stop it being treated the same as 49 - which will allow you to simplify the model.
I'm not sure I understand what you mean?

If you mean what I think you mean then no - the data is 10,12.5,15.8,20, 25.. (see below)

This is a sample of the (frequency) data I'm sending to the scope.

1/3rd oct 1/6th oct 1/12th oct 1/24th oct
10.000 10.000 10.000 10.000
12.599 11.225 10.595 10.29302237
15.874 12.599 11.225 10.59463094
20.000 14.142 11.892 10.90507733
25.198 15.874 12.599 11.22462048
31.748 17.818 13.348 11.55352697
40.000 20.000 14.142 11.89207115
50.397 22.449 14.983 12.24053543
63.496 25.198 15.874 12.5992105
80.000 28.284 16.818 12.96839555
100.794 31.748 17.818 13.34839854
126.992 35.636 18.877 13.73953647
160.000 40.000 20.000 14.14213562
201.587 44.898 21.189 14.55653183
253.984 50.397 22.449 14.98307077
320.000 56.569 23.784 15.42210825
403.175 63.496 25.198 15.87401052
507.968 71.272 26.697 16.33915453
640.000 80.000 28.284 16.81792831
806.349 89.797 29.966 17.31073122
1015.937 100.794 31.748 17.81797436
1280.000 113.137 33.636 18.34008086
1612.699 126.992 35.636 18.87748625
2031.873 142.544 37.755 19.43063882
2560.000 160.000 40.000 20
3225.398 179.594 42.379 20.58604473
4063.747 201.587 44.898 21.18926189
5120.000 226.274 47.568 21.81015465


Edited by TonyRPH on Wednesday 31st January 16:33

andycambo

1,077 posts

206 months

Wednesday 31st January 2018
quotequote all
What about this...

Keep you MAX VALUES in a dictionary. Then add them to a list to loop through as you do check the index value against the list value.

You'll need to add the following as well - Imports System.Collections.Generic

This way all your values are in one place


Dim someDictionaryName As New Dictionary(Of Integer, String)
someDictionaryName.Add(3, "TEST")
someDictionaryName.Add(41, "50ms")
someDictionaryName.Add(161, "5ms")
someDictionaryName.Add(290, "LARGE")
'etc...

'calculate index
Dim index = 162

'create new list and add the dictionary keys
Dim list As New List(Of Integer)
For Each key As String In someDictionaryName.Keys
list.Add(key)
Next

'loop through the new list and check the index value
Dim sKey As Integer
For Each kvp As KeyValuePair(Of Integer, String) In someDictionaryName
sKey = kvp.Key
'Console.WriteLine("KEY VALUES: " & sKey)

If index <= sKey
Console.WriteLine("CLOSEST KEY VALUE: " & sKey)
EXIT FOR
end If

Console.WriteLine("CLOSEST KEY FOUND")
Next

'now get the dictionary value (technically you don't need the if statement here but it is a just in case
If someDictionaryName.ContainsKey(sKey) Then
Console.WriteLine(someDictionaryName.Item(sKey))
End If

andycambo

1,077 posts

206 months

Wednesday 31st January 2018
quotequote all
Btw the if statements are probably quicker to process than what I've said. It might just be a better way to organise your data.

TonyRPH

Original Poster:

13,560 posts

200 months

Wednesday 31st January 2018
quotequote all
@andycambo that's an interesting idea actually, as my frequencies are arrays constructed on the fly, but if I could store those somewhere too..


Timberwolf

5,374 posts

250 months

Wednesday 31st January 2018
quotequote all
After a bit of swearing at VB, I've come up with this which should work if you're on a reasonably recent version (I tested with VS2017 but I think most of this stuff has been around since VS2012 or so):

Do this outside your main loop...

PH doesn't like all those braces said:
Dim rangeBoundaries As New List(Of Array) From {
{{9, 41, "50mS "}},
{{49, 161, "5mS "}},
{{162, 321, "2mS "}},
{{322, 1600, "0.5mS "}},
{{1800, 4001, "0.2mS "}},
{{4002, 16200, "50uS "}},
{{16300, 32100, "20uS "}},
{{39000, 81000, "10uS "}},
{{82000, 1000000, "5uS "}}
}
Then in your main loop:


Dim output As String = (From r In rangeBoundaries
Where index >= r(0) And index <= r(1)
Select r(2) & freq2).FirstOrDefault()

If Not String.IsNullOrEmpty(output) Then ListBox4.Items.Add(output)


Downside is it'll probably be quite a bit slower, although easier to maintain and looks a bit cleaner. Disclaimer: my VB.net code is almost certainly horrible, I can barely write C# these days let alone Visual Basic.

Edited by Timberwolf on Wednesday 31st January 17:42

TonyRPH

Original Poster:

13,560 posts

200 months

Thursday 1st February 2018
quotequote all
Thanks all for the great suggestions - my little app is now well and truly on the way to completion.

beer