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

View all comments

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

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