Note that there are some explanatory texts on larger screens.

plurals
  1. POImprove this ugly piece of code
    primarykey
    data
    text
    <p>I'm writing a QR code generator in VB.net</p> <p>First I check what value (of the QR code version) was chosen by the user.</p> <p>Each version has fixed count of bits per mode (0001, 0010, 0100, 1000).</p> <p>Basically what I got now is the following:</p> <pre><code>Private Function get_number_of_bits() As Integer Dim bits As Integer If listVersion.Value &lt; 10 Then If get_binary_mode(listMode.SelectedItem) = "0001" Then bits = 10 End If If get_binary_mode(listMode.SelectedItem) = "0010" Then bits = 9 End If If get_binary_mode(listMode.SelectedItem) = "0100" Or _ get_binary_mode(listMode.SelectedItem) = "1000" Then bits = 8 End If ElseIf listVersion.Value &lt; 27 Then If get_binary_mode(listMode.SelectedItem) = "0001" Then bits = 12 End If If get_binary_mode(listMode.SelectedItem) = "0010" Then bits = 11 End If If get_binary_mode(listMode.SelectedItem) = "0100" Then bits = 16 End If If get_binary_mode(listMode.SelectedItem) = "1000" Then bits = 10 End If Else If get_binary_mode(listMode.SelectedItem) = "0001" Then bits = 14 End If If get_binary_mode(listMode.SelectedItem) = "0010" Then bits = 13 End If If get_binary_mode(listMode.SelectedItem) = "0100" Then bits = 16 End If If get_binary_mode(listMode.SelectedItem) = "1000" Then bits = 12 End If End If Return bits End Function </code></pre> <p>Which works but of course it is an ugly piece of ..... code :)</p> <p>What would be a better way to write this?</p> <p><strong>EDIT</strong></p> <p>As requested.</p> <p>listMode is a combobox which is filled with:</p> <pre><code>Private Function get_encoding_modes() As Dictionary(Of String, String) Dim modes As New Dictionary(Of String, String) modes.Add("0000", "&lt;Auto select&gt;") modes.Add("0001", "Numeric (max. 7089 chars)") modes.Add("0010", "Alphanumeric (max. 4296 chars)") modes.Add("0100", "Binary [8 bits] (max. 2953 chars)") modes.Add("1000", "Kanji/Kana (max. 1817 chars)") Return modes End Function </code></pre> <p>Code for get_binarymode</p> <pre><code>Private Function get_binary_mode(ByVal mode As String) As String Dim modes As New Dictionary(Of String, String) modes = get_encoding_modes() Dim result As String = "" Dim pair As KeyValuePair(Of String, String) For Each pair In modes If pair.Value = mode Then result = pair.Key End If Next Return result End Function </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload