r/visualbasic • • Jan 12 '20

Need a bit help

So ive done a program and my teacher just casually said its spagheti code and i need to somehow fix it but im totally out of idea how to do that so i would appreciate all help i can get from here!
Here is the code:
https://pastebin.com/qzPbMXh8

6 Upvotes

33 comments sorted by

3

u/LondonPilot Jan 12 '20 edited Jan 12 '20

It’s not really “spaghetti code” as such - that would suggest the flow of the program is hard to follow.

The flow of this program is simple - but it’s hard to read.

So:

  • Move the “numbers” and “letters” variables into a different module (or class, if you know about classes yet)

  • For the “letters” variable, put each string onto a different line, and indent them so that it’s immediately clear that they’re showing the letters (I think Visual Studio will let you do this, I can’t remember and I’m on my phone so I can’t test it)

  • Take that massive list of if statements, and at the very least change it to a select/case. Even better, if you can figure out how to turn this pseudo-code into code:

If between 0-9, Subtract ASCII code for 0

Else if between A-Z, Subtract ASCII code for A then Add 10

1

u/Dennis13631 Jan 12 '20

Thanks! Will try to make it like u suggest when i go home.

1

u/Dennis13631 Jan 12 '20

Btw by different modules u mean different sub or im just not getting it?

2

u/LondonPilot Jan 12 '20

No, I mean different modules - which basically means a different file.

Again, I’m not at my computer, so this may not be exactly right, but if you right click on the project in solution explorer, then choose Add, you should get an option to add a module.

The module has a name. Your module that you already have is called Module1 - it says that on line 2 of your code. You can access things in other modules from your main module.

There’s a guide to modules here which looks quite good - it’s based on Windows Forms but should still tell you everything you need to know.

1

u/Dennis13631 Jan 12 '20

I gotcha what u mean with the new module so i now just transfered the numbers and letters to new module but when i try to go back to the original module and write the new sub in the new module to the For i as Integer = 0 to Values.numbers(index) im getting Expression does not produce a value on the Value sub. To be honest im lost

1

u/Dennis13631 Jan 12 '20

Update : https://imgur.com/a/GZx8SKG this is whats happening now and its on the second module too at the End Function

2

u/LondonPilot Jan 12 '20

Ok, so although I can't really see much of what you've done from that screenshot, I'm in front of the computer for a bit, so I was able to try some stuff out myself and put together an example for you. Here's my example code:

Program.vb

Imports System

Module Program
    Sub Main(args As String())
        Console.WriteLine(Test.numbers)
    End Sub
End Module

Test.vb

Module Test
    Public numbers(25) As Array

    Sub New() ' Static constructor
        Console.WriteLine("Initialising the array")
        numbers(0) = {1, 2, 3}
    End Sub
End Module

Note that, to be able to access numbers from the Program module, I had to:

  • Mark it as "Public" in Test.vb, using the word Public instead of Dim

  • Prefix it with the name of the module that contained it when I wanted to access it in a different module. Note that I wrote Test.numbers, and not just numbers.

Also:

  • I created a static constructor in the new module, which is the most convenient place to initialise the array. (You can't just put the initialisation code below the declaration, because it's not in a method.)

Hope that with those examples you're able to get back on track! If you're stuck again, though, please post both files, not just one.

1

u/Dennis13631 Jan 12 '20

Okay ive managed to do that and i used 3 modules.
Here are the pastebins if u wanna check them:
https://pastebin.com/5H91abHG - 1st module
https://pastebin.com/1ykqbwWB - 2nd module
https://pastebin.com/esnHD0U5 - 3rd module
And all thats left is to make it so the person who use the program can print more than 1 char and be on the same row so hope thats more easy than all that atleast!

1

u/LondonPilot Jan 12 '20

Excellent.

If I was sitting down with you, there’s a lot more we could do to bring it towards being professional standard, but for a beginner I’d say that’s not a bad start. It’s 100 times easier to read than it was before, and one of the most important things to learn if you want to improve is that your code must be easy to read. Professional programmers spend 80% of their time changing code, and only 20% writing new code, so making code easy to read is vital. Well done - I hope your teacher agrees!

1

u/Dennis13631 Jan 12 '20

Thanks man i appreciate all the help u gave me but i still feel like i wont even finish it alone :( This seems way too hard for a beginner or maybe its just me that im bad at this . Ive tried with SetCursorPosition too but it just wont place the next chars on the same row

1

u/LondonPilot Jan 12 '20

Yeah, SetCursorPosition is probably the way to go here.

Let’s try breaking it down. First of all, concentrate on the user typing in just two letters. Decide where the second letter should go on the screen, and use SetCursorPosition to put the second letter in the right place. You will need a variable to keep track of whether it’s the first or second letter. And you can “hard code” the position, so just work out the correct X and Y for each part of the character you’re displaying, and put the numbers in the program code.

The next step is to make it work with more than two letters. Again, use a variable to keep track of how many letters you’ve done so far, but this time try to get the program to calculate the X position using some simple maths (something like characterNumber * 10, if you have a variable called characterNumber and you decide to allow 10 spaces per character).

That will work so long as the user’s text fits on one line. If you have time for a bit more of a challenge, you can work on splitting text over more than one line... you’ll need to calculate the X position (which will be a bit more complex now), and also the Y position, so this really is a bit of a challenge, and definitely not one to worry about until you’re confident with just one line.

1

u/Dennis13631 Jan 12 '20

But to be honest we still havent learned that setcursorposition so not sure how it works completly i just found it while browsing to get my job done :D

→ More replies (0)

2

u/Songg45 Jan 12 '20 edited Jan 12 '20

So I took a look at your code, and had made a few enhancements (note: I did not add LondonPilot's suggestions) :

  1. Changed your if text = 1 or 2 or.... to a Regex function.

  2. Made it into a Do While loop exiting by typing "exit"

  3. The Regex function forces text to have a length of 1.

  4. Changed your 25 if then index statements to a select case

  5. Added a closing statement and error message

https://pastebin.com/18HbDEX0

1

u/Dennis13631 Jan 12 '20

Can i just ask what is this? ("[0-9]|[bcdhnot]|[AEFGHLPUY]")
Never seen something like this whats it supposed to do?

1

u/Songg45 Jan 12 '20

Thats my Regex (or REGular EXpression). It allows me to quickly determine patterns in text. I move text to the pattern search, and tell it to match anything thats 0-9, bcdhnot, or AEFGHLPUY. If you had the full alphabet, it would be ("[0-9][A-Z][a-z]")

If it gets a match, then the function returns true and the program continues. If it doesn't get a match, or text is longer than 1 character, the function returns false and fails.

1

u/Dennis13631 Jan 12 '20

so its not possible to print more than 1 character with this function as i understand? Anyway thats awesome but i cant use it like that i think

1

u/Songg45 Jan 12 '20

Like printing both 1 and Y for example?

With some work, it can, but your original program doesn't either.

1

u/Dennis13631 Jan 12 '20

On the same row yes . Thats the last thing i need to finish this program

1

u/Songg45 Jan 12 '20

Big oof if true

I have to leave so I will be back later... but I'm thinking not on the same line without a major rework.

1

u/Dennis13631 Jan 12 '20

Im sorry u wasted your time like that i should have explained it in the post not the comments :/ feels bad

1

u/Songg45 Jan 12 '20

No what I'm saying is I'm pretty certain what your trying to do is not possible using console.writeline... but I may be wrong.

Do you absolutely HAVE TO use console.writeline?

1

u/Dennis13631 Jan 12 '20 edited Jan 12 '20

Well it has to be similar to that ( its my teacher code) but i started mine before he uploaded his so i was told to somehow fix the spagheti code and upgrade it however i can

→ More replies (0)

1

u/Thunor_SixHammers Jan 12 '20

Could you give some context as to what this is supposed to do?

1

u/Dennis13631 Jan 12 '20

You put one of the available values and it should print them like a digital clock but with stars and i have to somehow fix this spagheti code and also make it so its able to print more than 1 on the same row
Here is how it looks right now when its started : https://imgur.com/a/FpaRR6R

1

u/fuzzfeatures Jan 16 '20 edited Jan 16 '20

Hi,

I thought I'd add my ideas to the mix.

Looking at the code my immediate thought there was a lot of work went into defining each letter, so I set about reducing the amount of work and the chance of making mistakes .. so i pre-defined the strings. This way, intellisense helps you out when it comes to defining each character. Also, if you wanted to change the asterisks to some other character, you only have to make a few changes with less chance of mistakes.

Also the arrays are structured slightly differently so you don't have to mess around with array subscripts .. this makes the code a little simpler and less prone to errors and easier to read. AND it removes the need for nested loops

Finally, your multiple If..Then statements can be reduced to a couple of lines.

All the replacement code does it to create a string of all the valid characters, then the first if statement checks that the user entered character is contained in the string of valid characters. Then, the next line just finds the character's position in that string and uses that position as the index in the array where the appropriate element is. :-)

Have a look and see if it makes sense

Option Explicit On
Module Module1

    Sub Main(args As String())
        Dim numbers(25)() As Integer
        'numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
        numbers(0) = {1, 0, 0, 0, 0, 0, 0}
        numbers(1) = {1, 0, 0, 1, 1, 1, 1}
        numbers(2) = {0, 1, 0, 0, 1, 0, 0}
        numbers(3) = {0, 1, 1, 0, 0, 0, 0}
        numbers(4) = {0, 0, 1, 1, 0, 0, 1}
        numbers(5) = {0, 0, 1, 0, 0, 1, 0}
        numbers(6) = {0, 0, 0, 0, 0, 1, 0}
        numbers(7) = {1, 1, 1, 1, 0, 0, 0}
        numbers(8) = {0, 0, 0, 0, 0, 0, 0}
        numbers(9) = {0, 0, 1, 0, 0, 0, 0}

        'Letters A, b, c, d, E, F, G, H, h, L, n, o, P, t, U, Y
        numbers(10) = {0, 0, 0, 1, 0, 0, 0}
        numbers(11) = {0, 0, 0, 0, 0, 1, 1}
        numbers(12) = {0, 1, 0, 0, 1, 1, 1}
        numbers(13) = {0, 1, 0, 0, 0, 0, 1}
        numbers(14) = {0, 0, 0, 0, 1, 1, 0}
        numbers(15) = {0, 0, 0, 1, 1, 1, 0}
        numbers(16) = {1, 0, 0, 0, 0, 1, 0}
        numbers(17) = {0, 0, 0, 1, 0, 0, 1}
        numbers(18) = {0, 0, 0, 1, 0, 1, 1}
        numbers(19) = {1, 0, 0, 0, 1, 1, 1}
        numbers(20) = {0, 1, 0, 1, 0, 1, 1}
        numbers(21) = {0, 1, 0, 0, 0, 1, 1}
        numbers(22) = {0, 0, 0, 1, 1, 0, 0}
        numbers(23) = {0, 0, 0, 0, 1, 1, 1}
        numbers(24) = {1, 0, 0, 0, 0, 0, 1}
        numbers(25) = {0, 0, 1, 0, 0, 0, 1}

        Dim segH As String = "*******"
        Dim segL As String = "*      "
        Dim segR As String = "      *"
        Dim segLR As String = "*     *"
        Dim segEmpty As String = "       "


        Dim letters(25)() As String
        'numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
        letters(0) = {segH, segLR, segLR, segLR, segLR, segLR, segLR, segLR, segH}
        letters(1) = {segR, segR, segR, segR, segR, segR, segR, segR, segR}
        letters(2) = {segH, segR, segR, segR, segH, segL, segL, segL, segH}
        letters(3) = {segH, segR, segR, segR, segH, segR, segR, segR, segH}
        letters(4) = {segLR, segLR, segLR, segLR, segH, segR, segR, segR, segR}
        letters(5) = {segH, segL, segL, segL, segH, segR, segR, segR, segH}
        letters(6) = {segH, segL, segL, segL, segH, segLR, segLR, segLR, segH}
        letters(7) = {segH, segR, segR, segR, segR, segR, segR, segR, segR}
        letters(8) = {segH, segLR, segLR, segLR, segH, segLR, segLR, segLR, segH}
        letters(9) = {segH, segLR, segLR, segLR, segH, segR, segR, segR, segH}

        'Letters A(10), b(11), c(12), d(13), E(14), F(15), G(16), H(17), h(18), L(19), n(20), o(21), P(22), t(23), U(24), Y(25)
        letters(10) = {segH, segLR, segLR, segLR, segH, segLR, segLR, segLR, segLR}
        letters(11) = {segL, segL, segL, segL, segH, segLR, segLR, segLR, segH}
        letters(12) = {segEmpty, segEmpty, segEmpty, segEmpty, segH, segL, segL, segL, segH}
        letters(13) = {segR, segR, segR, segR, segH, segLR, segLR, segLR, segH}
        letters(14) = {segH, segL, segL, segL, segH, segL, segL, segL, segH}
        letters(15) = {segH, segL, segL, segL, segH, segL, segL, segL, segL}
        letters(16) = {segH, segL, segL, segL, segLR, segLR, segLR, segLR, segH}
        letters(17) = {segLR, segLR, segLR, segLR, segH, segLR, segLR, segLR, segLR}
        letters(18) = {segL, segL, segL, segL, segH, segLR, segLR, segLR, segLR}
        letters(19) = {segL, segL, segL, segL, segL, segL, segL, segL, segH}
        letters(20) = {segEmpty, segEmpty, segEmpty, segEmpty, segH, segLR, segLR, segLR, segLR}
        letters(21) = {segEmpty, segEmpty, segEmpty, segEmpty, segH, segLR, segLR, segLR, segH}
        letters(22) = {segH, segLR, segLR, segLR, segH, segL, segL, segL, segL}
        letters(23) = {segL, segL, segL, segL, segH, segL, segL, segL, segH}
        letters(24) = {segLR, segLR, segLR, segLR, segLR, segLR, segLR, segLR, segH}
        letters(25) = {segLR, segLR, segLR, segLR, segH, segR, segR, segR, segH}
        Console.ForegroundColor = ConsoleColor.Red
        Console.WriteLine("Values : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, b, c, d, E, F, G, H, h, L, n, o, P, t, U,  Y")
        Console.Write("Write a value: ")
        Dim text As String = Console.ReadLine()
        Console.ResetColor()

        Dim validCharacters As String = "0123456789AbcdEFGHhLnoPtUY"
        If validCharacters.Contains(text) Then
            'Replacement for all the If..Then statements
            Dim index As Integer = validCharacters.IndexOf(text)
            'Letters A(10), b(11), c(12), d(13), E(14), F(15), G(16), H(17), h(18), L(19), n(20), o(21), P(22), t(23), U(24), Y(25)


            For i As Integer = 0 To numbers(index).GetUpperBound(0)
                Console.Write(" ")
                Console.Write(numbers(index)(i))
            Next
            Console.WriteLine()

            For j As Integer = 0 To letters(index).GetUpperBound(0)
                Console.WriteLine(letters(index)(j))
            Next

        Else
            Console.WriteLine("wrong value!")
        End If
        Console.ReadKey()
    End Sub
End Module

1

u/fuzzfeatures Jan 16 '20 edited Jan 16 '20

Hi again,

Have a look at my previous post first .. this explains some simplifications to your original code. The code below adds the ability to type in several characters and display them.

Rather than include the whole code here, I've pasted everything after the user input prompt

        Dim validCharacters As String = "0123456789AbcdEFGHhLnoPtUY"
        'Check that the entered text characters are all valid
        Dim allCharactersValid As Boolean = True
        For Each character As String In text
            If Not validCharacters.Contains(character) Then
                allCharactersValid = False
                Exit For
            End If
        Next

        Dim index As Integer
        If allCharactersValid Then
            For Each character As String In text
                index = validCharacters.IndexOf(character)
                For i As Integer = 0 To numbers(index).GetUpperBound(0)
                    Console.Write(numbers(index)(i))
                Next
                Console.Write(" ")
            Next
            Console.WriteLine()
            For j As Integer = 0 To letters(index).GetUpperBound(0)
                For Each character As String In text
                 Console.Write(letters(validCharacters.IndexOf(character))(j) & " ")
                Next
                Console.WriteLine()
            Next
        Else
            Console.WriteLine("wrong value!")
        End If
        Console.ReadKey()
    End Sub