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

Show parent comments

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)