Resolved Help Need to Select Correct file based on serial number

servox

Member
Joined
Jun 30, 2022
Messages
15
Programming Experience
Beginner
Hi All

I'm currently using this method to select the file based on the serial number, but ideally would like to find a better way minimize the code

Each file contains 5000 lines per file and I have currently have over 100+ files and growing as more data is added, so that's a lot of coding

vb .net:
Select Case txtGetSerialNumber.text

            Case 1 To 5000
                strCurrentFileToLoad = "c:\temp\Data1.dat"
            Case 5001 To 10000
                strCurrentFileToLoad = "c:\temp\Data2.dat"
            Case 10001 To 15000
                strCurrentFileToLoad = "c:\temp\Data3.dat"
            Case 15001 To 20000
                strCurrentFileToLoad = "c:\temp\Data4.dat"


        End Select

the serial length is 6 digits, so could go to 999999

any help would be appreciated
 
Solution
As for your question, it looks as though each file corresponds to a multiple of 5000, so all you need to do is divide your number by 5000 and that's basically your file number. Here's one option:
VB.NET:
Dim serialNumber As Integer

If Integer.TryParse(serialNumberTextBox.Text, serialNumber) Then
    Dim fileNumber = ((serialNumber - 1) \ 5000) + 1
    Dim filePath = $"C:\temp\Data{fileNumber}.dat"
   
    '...
Else
    'Input invalid. Act accordingly.
End If
That calculation may seem a bit odd but let's look at it step by step. It starts by subtracting 1 from the serial number. That means that numbers in the range 1-5000 map to 0-4999, 5001-10000 to 5000-9999 and so on. The next step is to perform integer division by 5000. That will...
First things first, the Text of a control is a String but you are comparing it to numbers. That's bad. It will often work as expected but there are times that it won't. Do you know when it won't? If you don't then you have no idea whether the code you're using will work or not. If you respect data types and be explicit, rather then expecting the system to read your mind, then you'll get it right every time.

To that end, you should turn Option Strict On in the project properties. That will enforce strict typing rules and code like yours will fail to compile. That may seem inconvenient but it will force you to write better code and actually think about data types and what you should be using. In this case, you should explicitly convert the user input to a number, then use that number.

The thing is, if that data is free text entered by the user, how do you even know it's a number? By accident or on purpose, the user could enter anything, you ought to validate the data first and then convert it if it's valid. This step may be overlooked in many begiiner projects for simplicity but is crucial in any real software, to avoid crashes when users inevitably enter the wrong thing.

Note that you should also set Option Strict On in the VS options, so it is On by default in all future projects.
 
As for your question, it looks as though each file corresponds to a multiple of 5000, so all you need to do is divide your number by 5000 and that's basically your file number. Here's one option:
VB.NET:
Dim serialNumber As Integer

If Integer.TryParse(serialNumberTextBox.Text, serialNumber) Then
    Dim fileNumber = ((serialNumber - 1) \ 5000) + 1
    Dim filePath = $"C:\temp\Data{fileNumber}.dat"
    
    '...
Else
    'Input invalid. Act accordingly.
End If
That calculation may seem a bit odd but let's look at it step by step. It starts by subtracting 1 from the serial number. That means that numbers in the range 1-5000 map to 0-4999, 5001-10000 to 5000-9999 and so on. The next step is to perform integer division by 5000. That will give the whole number of times that 5000 divides into the prior result. 0-4999 maps to 0, 5000-9999 to 1 and so on. The last step is to add 1 to get the file number.

Note that I have used some more appropriate variable names here. What you do is up to you but names like txtGetSerialNumber are bad. Hungarian Notation (use of prefixes like "txt" to denote data type) have long been recommended against. They are a holdover from the days when data types were few and code was written in text editors. If you're using Visual Studio to write code that might use hundreds or even thousands of different data types, it's just not practical. I can't count the number of people I've seen who end up prefixing 95% of their variables with "obj". Also, the variable should indicate what the variable holds. Names like "GetSerialNumber" do not belong in variables. That might be the name of a method that actually gets a serial number but a variable that stores a serial number isn't doing any getting. Variable names should be noun-based and method names should be verb-based because variables are data and methods are behaviour.
 
First things first, the Text of a control is a String but you are comparing it to numbers. That's bad. It will often work as expected but there are times that it won't. Do you know when it won't? If you don't then you have no idea whether the code you're using will work or not. If you respect data types and be explicit, rather then expecting the system to read your mind, then you'll get it right every time.

To that end, you should turn Option Strict On in the project properties. That will enforce strict typing rules and code like yours will fail to compile. That may seem inconvenient but it will force you to write better code and actually think about data types and what you should be using. In this case, you should explicitly convert the user input to a number, then use that number.

The thing is, if that data is free text entered by the user, how do you even know it's a number? By accident or on purpose, the user could enter anything, you ought to validate the data first and then convert it if it's valid. This step may be overlooked in many begiiner projects for simplicity but is crucial in any real software, to avoid crashes when users inevitably enter the wrong thing.

Note that you should also set Option Strict On in the VS options, so it is On by default in all future projects.

Thank you for taking the time to share your knowledge and explaining about the dangers of mixing integers and strings.

The TextBox will only allow numbers so there no chance of character being entered

VB.NET:
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Back) Then

        Else
            Dim ch As Char = e.KeyChar
            If Not Char.IsDigit(ch) Then
                e.Handled = True
            End If
        End If

            If txtGetSerialNumber.TextLength > 0 Then

Carry on with code


        End If
 
As for your question, it looks as though each file corresponds to a multiple of 5000, so all you need to do is divide your number by 5000 and that's basically your file number. Here's one option:
VB.NET:
Dim serialNumber As Integer

If Integer.TryParse(serialNumberTextBox.Text, serialNumber) Then
    Dim fileNumber = ((serialNumber - 1) \ 5000) + 1
    Dim filePath = $"C:\temp\Data{fileNumber}.dat"
   
    '...
Else
    'Input invalid. Act accordingly.
End If
That calculation may seem a bit odd but let's look at it step by step. It starts by subtracting 1 from the serial number. That means that numbers in the range 1-5000 map to 0-4999, 5001-10000 to 5000-9999 and so on. The next step is to perform integer division by 5000. That will give the whole number of times that 5000 divides into the prior result. 0-4999 maps to 0, 5000-9999 to 1 and so on. The last step is to add 1 to get the file number.

Note that I have used some more appropriate variable names here. What you do is up to you but names like txtGetSerialNumber are bad. Hungarian Notation (use of prefixes like "txt" to denote data type) have long been recommended against. They are a holdover from the days when data types were few and code was written in text editors. If you're using Visual Studio to write code that might use hundreds or even thousands of different data types, it's just not practical. I can't count the number of people I've seen who end up prefixing 95% of their variables with "obj". Also, the variable should indicate what the variable holds. Names like "GetSerialNumber" do not belong in variables. That might be the name of a method that actually gets a serial number but a variable that stores a serial number isn't doing any getting. Variable names should be noun-based and method names should be verb-based because variables are data and methods are behaviour.


I’ve test the code and I’m getting an error on the file path with the $
Saying invalid character
 
Solution
As for your question, it looks as though each file corresponds to a multiple of 5000, so all you need to do is divide your number by 5000 and that's basically your file number. Here's one option:
VB.NET:
Dim serialNumber As Integer

If Integer.TryParse(serialNumberTextBox.Text, serialNumber) Then
    Dim fileNumber = ((serialNumber - 1) \ 5000) + 1
    Dim filePath = $"C:\temp\Data{fileNumber}.dat"
   
    '...
Else
    'Input invalid. Act accordingly.
End If
That calculation may seem a bit odd but let's look at it step by step. It starts by subtracting 1 from the serial number. That means that numbers in the range 1-5000 map to 0-4999, 5001-10000 to 5000-9999 and so on. The next step is to perform integer division by 5000. That will give the whole number of times that 5000 divides into the prior result. 0-4999 maps to 0, 5000-9999 to 1 and so on. The last step is to add 1 to get the file number.

Note that I have used some more appropriate variable names here. What you do is up to you but names like txtGetSerialNumber are bad. Hungarian Notation (use of prefixes like "txt" to denote data type) have long been recommended against. They are a holdover from the days when data types were few and code was written in text editors. If you're using Visual Studio to write code that might use hundreds or even thousands of different data types, it's just not practical. I can't count the number of people I've seen who end up prefixing 95% of their variables with "obj". Also, the variable should indicate what the variable holds. Names like "GetSerialNumber" do not belong in variables. That might be the name of a method that actually gets a serial number but a variable that stores a serial number isn't doing any getting. Variable names should be noun-based and method names should be verb-based because variables are data and methods are behaviour.


Many thanks for your code, it works perfectly. Thank you
I just had to change the filePath section to


VB.NET:
Dim filepath = String.Format("c:\temp\Data{0}.dat", filenumber)

I'm still new to programming and will take on board what you said and try to improve my code when dealing with integers and strings etc.
 
If you had to use String.Format instead of string interpolation then you must be using a fairly old version of VB. I would suggest updating to the latest version if you can.
 
If you had to use String.Format instead of string interpolation then you must be using a fairly old version of VB. I would suggest updating to the latest version if you can.

I’m currently using VS2010, but have now installled VS community 2022 and switch the option strict to on and had quite and few errors. I have convert them to
VB.NET:
If intSerial = Convert.ToInt32(serialtextbox.text) = 300 then
‘Continue
else
exit sub
end if

would you say that’s correct?

I must say that I love the way it’s show the dotted lines for a if statement so that you can easily see where the statement ends.

Thanks again for the advice
 
Back
Top