How to reduce the number of if-else statements (e.g. avoid hardcoding values)?

61 views Asked by At

How to do this without hardcoding every Jar version? So, in every ElseIf, it checks the version, if it's (for example 1.2.5), then download the software accordingly, to the path selected by the user + \ + type + _ + version + .jar. The problem is that there are more versions (more than 30 I think) and I don't want to hardcode all of them.

If JarVer = "1.2.1" Or JarVer = "1.2.2" Or JarVer = "1.2.3" Or JarVer = "1.2.4" Then
                My.Computer.Network.DownloadFile(
                "https://assets.minecraft.net/1_2/minecraft_server.jar",
                Path + "\" + SW + "_" + JarVer + ".jar")
            ElseIf JarVer = "1.2.5" Then
                My.Computer.Network.DownloadFile(
                "https://assets.minecraft.net/1_2_5/minecraft_server.jar",
                Path + "\" + SW + "_" + JarVer + ".jar")
            ElseIf JarVer = "1.3.1" Then
                My.Computer.Network.DownloadFile(
                "https://launcher.mojang.com/v1/objects/d8321edc9470e56b8ad5c67bbd16beba25843336/server.jar",
                Path + "\" + SW + "_" + JarVer + ".jar")
            ElseIf JarVer = "1.3.2" Then
                My.Computer.Network.DownloadFile(
                "https://launcher.mojang.com/v1/objects/82563ce498bfc1fc8a2cb5bf236f7da86a390646/server.jar",
                Path + "\" + SW + "_" + JarVer + ".jar")
            ElseIf JarVer = "1.4.2" Then
                My.Computer.Network.DownloadFile(
                "https://launcher.mojang.com/v1/objects/3de2ae6c488135596e073a9589842800c9f53bfe/server.jar",
                Path + "\" + SW + "_" + JarVer + ".jar")
            ...
1

There are 1 answers

0
OwlsSleeping On

They have to be hardcoded somewhere unless you have a consistent pattern.

I would consider putting the list in a data table or dictionary, then looking up the url from the version.