IM a noob.... visual basic help please

Soldato
Joined
4 Nov 2004
Posts
14,451
Location
Beds
Right below is some visual basic proram that i need for our group project. It's supposed to calculate three dimensional spin characteristics of a golf ball fired out of our cannon and upon hitting an angled plate.

When i try and debug the thing in the excel editor it highlights the red bit and says 'invalid outside procedure'.

I don't no much at all about BASIC and am wondering if anyone would be kind enough to explain whats going on, but most importantly get the bad boy working.

Also, does that data stuff need to be at the end, or is that where i need to input my own data ???

cheers :D :D :D



Rem**Y,Z is the principle point
Rem**Y0(1),Z0(1) is the centre of the first image
Rem**Y(1,1),Z(1,1) is the first node of the first image
Rem**Y(2,1),Z(2,1) is the second node of the first image
Rem**The coordinates for the successive images aresimilar but with the second array
Rem**variable as 2,3 and 4.

Rem**Dimension the arrays
Dim Y0(4), Z0(4), Y(2, 4), Z(2, 4)

Rem**Read in the data
For J% = 1 To 4
READ Y0(J%), Z0(J%)
For I% = 1 To 2
READ Y(I%, J%), Z(I%, J%)
Next I%
Next J%
READ Y, Z, F

Rem**Adjsut the data to allow for the relief displacement
For I% = 1 To 2
For J% = 1 To 4
If 4.536901 - (Y(I%, J%) - Y0(J%) ^ 2 - (Z(I%, J%) - Z0(J%)) ^ 2 < 0) Then X(I%, J%) = 0: GoTo 5
X(I%, J%) = Sqr(4.536901 - (Y(I%, J%) - Y0(J%)) ^ 2 - (Z(I%, J%) - Z0(J%)) ^ 2)
5 R(I%, J%) = Sqr((Y(I%, J%) - Y) ^ 2 + (Z(I%, J%) - Z) ^ 2)
D(I%, J%) = X(I%, J%) * R(I%, J%) * 0.0154
YO(J%) = (J%) - Y: Y(I%, J%) = Y(I%, J%) - Y
Z0(J%) = Z0(J%) - Z: Z(I%, J%) = Z(I%, J%) - Z
K(I%, J%) = Y(I%, J%) * K(I%, J%)
Y(I%, J%) = Y(I%, J%) * K(I%, J%)
Z(I%, J%) = Z(I%, J%) * K(I%, J%)
Y(I%, J%) = Y(I%, J%) - Y0(J%): Z(I%, J%) = Z(I%, J%) - Z0(J%)
If 4.536901 - Y(I%, J%) ^ 2 - Z(I%, J%) ^ 2 Then X(I%, J%) = 0: GoTo 6
X(I%, J%) = Sqr(4.536901 - Y(I%, J%) ^ 2 - Z(I%, J%) ^ 2)
6 Next I%
Next J%

Rem**Modify cartesian coordinates to direct cosines
For J% = 1 To 4
For I% = 1 To 2
X(I%, J%) = X(I%, J%) / 2.13
Y(I%, J%) = Y(I%, J%) / 2.13
Z(I%, J%) = Z(I%, J%) / 2.13
Next I%
Next J%

Rem**Calculate spin vector before impact
If Z(2, 1) - Z(2, 2) = 0 Then A = 1E+20: GoTo 10
A = (Z(1, 1) - Z(1, 2)) / (Z(2, 1) - Z(2, 2))
10 If A * (Y(2, 1) - Y(2, 2)) - Y(1, 1) + Y(1, 2) = 0 Then GoTo 20
B = (X(1, 1) - X(1, 2) - A * (X(2, 1) - X(2, 2))) / (A * (Y(2, 1) - Y(2, 2)) - Y(1, 1) + Y(1, 2))
C = 1 + B * B + ((X(1, 1) - X(1, 2) + B * (Y(1, 1) - Y(1, 2))) / (Z(1, 2) - Z(1, 1))) ^ 2: GoTo 30
20 B = 1E+20: C = 1E+20
30 Xb = Sqr(1 / C)
Yb = B * Xb
Zb = Sqr(1 - (1 + B * B) * Xb * Xb)

Rem**Calculate rotation before impact
ANGb = Xb * X(1, 1) + Yb * Y(1, 1) + Zb * Z(1, 1)
ANGb = -Atn(ANGb / Sqr(-ANGb * ANGb + 1)) + 1.570796
R = 4.26 * Sin(ANGb)
D = Sqr((X(1, 1) - X(1, 2)) ^ 2 + (Y(1, 1) - Y(1, 2)) ^ 2 + (Z(1, 1) - Z(1, 2)) ^ 2)
ROTb = D / R
ROTb = 2 * Atn(ROTb / Sqr(-ROTb * ROTb + 1))

Rem**Calculate spin vector after impact
If Z(2, 3) - Z(2, 4) = 0 Then A = 1E+20: GoTo 10
A = (Z(1, 3) - Z(1, 4)) / (Z(2, 3) - Z(2, 4))
10 If A * (Y(2, 3) - Y(2, 4)) - Y(1, 3) + Y(1, 4) = 0 Then GoTo 20
B = (X(1, 3) - X(1, 4) - A * (X(2, 3) - X(2, 4))) / (A * (Y(2, 3) - Y(2, 4)) - Y(1, 3) + Y(1, 4))
C = 1 + B * B + ((X(1, 3) - X(1, 4) + B * (Y(1, 3) - Y(1, 4))) / (Z(1, 4) - Z(1, 3))) ^ 2: GoTo 30
20 B = 1E+20: C = 1E+20
30 Xa = Sqr(1 / C)
Ya = B * Xa
Za = Sqr(1 - (1 + B * B) * Xa * Xa)

Rem**Calculate rotation after impact
ANGa = Xa * X(1, 3) + Ya * Y(1, 3) + Za * Z(1, 3)
ANGa = -Atn(ANGa / Sqr(-ANGa * ANGa + 1)) + 1.570796
R = 4.26 * Sin(ANGa)
D = Sqr((X(1, 3) - X(1, 4)) ^ 2 + (Y(1, 3) - Y(1, 4)) ^ 2 + (Z(1, 3) - Z(1, 4)) ^ 2)
ROTb = D / R
ROTb = 2 * Atn(ROTb / Sqr(-ROTb * ROTb + 1))

Rem**Output data
Print "*****Before Impact*****"
Print "Spin Vector is ";: Print USING; "+#.##,"; Xb; Yb; Zb
Print "ROTx = ";: Print USING; "+###.##"; ROTb * Xb * F;: Print " rad/s ROTy = ";: Print USING; "+###.##"; ROTb * Yb * F;: Print " rad/s ROTz = ";: Print USING; "+###.##"; ROTb * Zb * F;: Print " rad/s."
Print "Total rotation is ";: Print USING; "+###.##"; ROTb * F;: Print " rad/s"

Print "*****After impact*****"
Print "Spin Vector is ";: Print USING; "+#/##,"; Xa; Ya; Za
Print "ROTx = ";: Print USING; "+###.##"; ROTa * Xa * F;: Print " rad/s, ROTz = ";: Print USING; "+###.##"; ROTa * Za * F;: Print " rad/s."
Print "Total rotation is ";: Print USING; "+###.##"; ROTa * F;: Print " rad/s"

End
DATA 41.4, 18.1
DATA 41.5, 19#
DATA 42.2, 17.9
DATA 33.7, 9.1
DATA 34.4, 9.9
DATA 34.2, 8.6
DATA 41.3, 18#
DATA 41.5, 19.2
DATA 42.2, 17.9
DATA 33.8, 9.2
DATA 34.4, 9.9
DATA 34.2, 8.6
DATA 31#, 14.5, 250
 
That looks suspiciously like ZX or perhaps C64 Basic, what it definately isn't it Visual Basic.

Where on earth did you get it from? Still, good luck unpicking what it does and getting it to work, I certainly wouldn't like to do it and don't see any value in unpicking 20 year old code.

:)
 
It's nothing really like ZX Basic either - but I'm stumped as to what it could ACTUALLY be.

:)

It's not "nothing like" it anyway, it does have some similarities to both ZX Basic AND C64 basic - REM/DIM/GOTO statements, the concept of using the ; character to continue a printed line, the Data bit at the end being READ in (both C64 and ZX), as is the use of % identifiers on variables (C64 used both % and $ iirc).

I'm a C64 n00b as I wasn't posh enough to be able to afford one at the time, you obviously come from an overprivileged background and I bet you had an Amiga when they first came out too!

;)
 
Mr^B said:
you obviously come from an overprivileged background and I bet you had an Amiga when they first came out too!

;)
nah, i had the "slimline" version of the c64 - the one they released after the amiga was already out (i think). and i went from that to a megadrive. :cool:
 
Mr^B said:
That looks suspiciously like ZX or perhaps C64 Basic, what it definately isn't it Visual Basic.

Where on earth did you get it from? Still, good luck unpicking what it does and getting it to work, I certainly wouldn't like to do it and don't see any value in unpicking 20 year old code.

:)

lol it's from some old paper our tutor gave us.... haha i did't know it was that old...... Damn i knew it was old skool but not that bad..


Christ we are screwed then :rolleyes: . plus our tutor said it was simple.... Silly old fool...


I'm a C64 n00b as I wasn't posh enough to be able to afford one at the time, you obviously come from an overprivileged background and I bet you had an Amiga when they first came out too!

nah screw amiga, atari for the win. Think i still have the 2600 or what ever it was called. stunning console and still works. Sony and their rubbish ps2 eat your heart out. !!!!!

i need a cookie
 
Last edited:
adfinni said:
nah screw amiga, atari for the win. Think i still have the 2600 or what ever it was called. stunning console and still works. Sony and their rubbish ps2 eat your heart out. !!!!!

I had a BBC :(
 
adfinni said:
lol it's from some old paper our tutor gave us.... haha i did't know it was that old...... Damn i knew it was old skool but not that bad..

Christ we are screwed then :rolleyes: . plus our tutor said it was simple.... Silly old fool...

Honestly, it shouldn't be that hard to convert to code that would run in VB...

This variant of BASIC seems very familiar to me, but I can't remember what it's likely to be... Apple ][ BASIC? GWBasic? QBasic?

Maybe I'll have a go at translating it to compilable code for you... :D

Edit: having had hunch it might be QBasic, and having checked a QBasic reference, I believe the code is indeed QBasic... (See e.g. http://www.tedfelix.com/qbasic/ and see e.g. "Chapter 25.9" about DATA and READ which seems a distinct attribute of this code which matches with *** is described.)
 
Last edited:
OK, this code compiles/runs, but must produce bogus results as several variables were/are referenced in the original code for which no values were supplied. Also there were several other minor problems (like ROTa being used in the print statements but not being set to any value and the line numbers being duplicated) that I tried to correct. Note it is *not* the way to naturally express these concepts in VB.Net, but I tried to make the code compile with the minimum amount of changes, not change it to suit VB.Net idioms of expression.

Code:
Option Explicit Off

Module GolfBallSim
    ' Compilable on VS2005 (VB.Net)
    Sub Main()
        REM**Y,Z is the principle point
        REM**Y0(1),Z0(1) is the centre of the first image
        REM**Y(1,1),Z(1,1) is the first node of the first image
        REM**Y(2,1),Z(2,1) is the second node of the first image
        REM**The coordinates for the successive images aresimilar but with the second array
        REM**variable as 2,3 and 4.

        REM**Dimension the arrays
        Dim Y0(4), Z0(4), Y(2, 4), Z(2, 4)

        REM** These were missing from the source..... ***********<<<<<<<<<<<
        Dim X(2, 4), K(2, 4), D(2, 4), R(2, 4)
        F = 0 REM F is used below but likewise not set anywhere...

        REM**Read in the data
        Y0(1) = 41.4
        Z0(1) = 18.1
        Y(1, 1) = 41.5
        Z(1, 1) = 19.0#
        Y(2, 1) = 42.2
        Z(2, 1) = 17.9

        Y0(2) = 33.7
        Z0(2) = 9.1
        Y(1, 2) = 34.4
        Z(1, 2) = 9.9
        Y(2, 2) = 34.2
        Z(2, 2) = 8.6

        Y0(3) = 41.3
        Z0(3) = 18.0#
        Y(1, 3) = 41.5
        Z(1, 3) = 19.2
        Y(2, 3) = 42.2
        Z(2, 3) = 17.9

        Y0(4) = 33.8
        Z0(4) = 9.2
        Y(1, 4) = 34.4
        Z(1, 4) = 9.9
        Y(2, 4) = 34.2
        Z(2, 4) = 8.6

        Y_ = 31.0#
        Z_ = 14.5
        F_ = 250


        REM**Adjsut the data to allow for the relief displacement
        For I% = 1 To 2
            For J% = 1 To 4
                If 4.536901 - (Y(I%, J%) - Y0(J%) ^ 2 - (Z(I%, J%) - Z0(J%)) ^ 2 < 0) Then X(I%, J%) = 0 : GoTo 5
                X(I%, J%) = System.Math.Sqrt(4.536901 - (Y(I%, J%) - Y0(J%)) ^ 2 - (Z(I%, J%) - Z0(J%)) ^ 2)
5:              R(I%, J%) = System.Math.Sqrt((Y(I%, J%) - Y_) ^ 2 + (Z(I%, J%) - Z_) ^ 2)
                D(I%, J%) = X(I%, J%) * R(I%, J%) * 0.0154
                Y0(J%) = (J%) - Y_ : Y(I%, J%) = Y(I%, J%) - Y_
                Z0(J%) = Z0(J%) - Z_ : Z(I%, J%) = Z(I%, J%) - Z_
                K(I%, J%) = Y(I%, J%) * K(I%, J%)
                Y(I%, J%) = Y(I%, J%) * K(I%, J%)
                Z(I%, J%) = Z(I%, J%) * K(I%, J%)
                Y(I%, J%) = Y(I%, J%) - Y0(J%) : Z(I%, J%) = Z(I%, J%) - Z0(J%)
                If 4.536901 - Y(I%, J%) ^ 2 - Z(I%, J%) ^ 2 Then X(I%, J%) = 0 : GoTo 6
                X(I%, J%) = System.Math.Sqrt(4.536901 - Y(I%, J%) ^ 2 - Z(I%, J%) ^ 2)
            Next J%
6:      Next I%

        REM**Modify cartesian coordinates to direct cosines
        For J% = 1 To 4
            For I% = 1 To 2
                X(I%, J%) = X(I%, J%) / 2.13
                Y(I%, J%) = Y(I%, J%) / 2.13
                Z(I%, J%) = Z(I%, J%) / 2.13
            Next I%
        Next J%

        REM**Calculate spin vector before impact
        If Z(2, 1) - Z(2, 2) = 0 Then A = 1.0E+20 : GoTo 10
        A = (Z(1, 1) - Z(1, 2)) / (Z(2, 1) - Z(2, 2))
10:     If A * (Y(2, 1) - Y(2, 2)) - Y(1, 1) + Y(1, 2) = 0 Then GoTo 20
        B = (X(1, 1) - X(1, 2) - A * (X(2, 1) - X(2, 2))) / (A * (Y(2, 1) - Y(2, 2)) - Y(1, 1) + Y(1, 2))
        C = 1 + B * B + ((X(1, 1) - X(1, 2) + B * (Y(1, 1) - Y(1, 2))) / (Z(1, 2) - Z(1, 1))) ^ 2 : GoTo 30
20:     B = 1.0E+20 : C = 1.0E+20
30:     Xb = System.Math.Sqrt(1 / C)
        Yb = B * Xb
        Zb = System.Math.Sqrt(1 - (1 + B * B) * Xb * Xb)

        REM**Calculate rotation before impact
        ANGb = Xb * X(1, 1) + Yb * Y(1, 1) + Zb * Z(1, 1)
        ANGb = -System.Math.Atan(ANGb / System.Math.Sqrt(-ANGb * ANGb + 1)) + 1.570796
        R_ = 4.26 * System.Math.Sin(ANGb)
        D_ = System.Math.Sqrt((X(1, 1) - X(1, 2)) ^ 2 + (Y(1, 1) - Y(1, 2)) ^ 2 + (Z(1, 1) - Z(1, 2)) ^ 2)
        ROTb = D_ / R_
        ROTb = 2 * System.Math.Atan(ROTb / System.Math.Sqrt(-ROTb * ROTb + 1))

        REM**Calculate spin vector after impact
        If Z(2, 3) - Z(2, 4) = 0 Then A = 1.0E+20 : GoTo 40
        A = (Z(1, 3) - Z(1, 4)) / (Z(2, 3) - Z(2, 4))
40:     If A * (Y(2, 3) - Y(2, 4)) - Y(1, 3) + Y(1, 4) = 0 Then GoTo 50
        B = (X(1, 3) - X(1, 4) - A * (X(2, 3) - X(2, 4))) / (A * (Y(2, 3) - Y(2, 4)) - Y(1, 3) + Y(1, 4))
        C = 1 + B * B + ((X(1, 3) - X(1, 4) + B * (Y(1, 3) - Y(1, 4))) / (Z(1, 4) - Z(1, 3))) ^ 2 : GoTo 60
50:     B = 1.0E+20 : C = 1.0E+20
60:     Xa = System.Math.Sqrt(1 / C)
        Ya = B * Xa
        Za = System.Math.Sqrt(1 - (1 + B * B) * Xa * Xa)

        REM**Calculate rotation after impact
        ANGa = Xa * X(1, 3) + Ya * Y(1, 3) + Za * Z(1, 3)
        ANGa = -System.Math.Atan(ANGa / System.Math.Sqrt(-ANGa * ANGa + 1)) + 1.570796
        R_ = 4.26 * System.Math.Sin(ANGa)
        D_ = System.Math.Sqrt((X(1, 3) - X(1, 4)) ^ 2 + (Y(1, 3) - Y(1, 4)) ^ 2 + (Z(1, 3) - Z(1, 4)) ^ 2)
        ROTa = D_ / R_  REM Corrected from ROTb in original
        ROTa = 2 * System.Math.Atan(ROTb / System.Math.Sqrt(-ROTb * ROTb + 1)) REM Corrected from ROTb in original

        REM**Output data
        Console.WriteLine("*****Before Impact*****")
        Console.WriteLine("Spin Vector is {0:N}, {0:N}, {0:N}", Xb, Yb, Zb)
        Console.Write("ROTx = {0:N} rad/s ", ROTb * Xb * F) : Console.Write("ROTy = {0:N} rad/s ", ROTb * Yb * F) : Console.WriteLine("ROTz = {0:N} rad/s", ROTb * Zb * F)
        Console.WriteLine("Total rotation is {0:N} rad/s", ROTb * F)

        Console.WriteLine("*****After impact*****")
        Console.Write("Spin Vector is {0:N}, {0:N}, {0:N}", Xa, Ya, Za)
        Console.Write("ROTx = {0:N} rad/s ", ROTa * Xb * F) : Console.Write("ROTy = {0:N} rad/s ", ROTa * Yb * F) : Console.WriteLine("ROTz = {0:N} rad/s", ROTa * Zb * F)
        Console.Write("Total rotation is {0:N} rad/s", ROTa * F)
        'End

        Console.ReadLine()
    End Sub
End Module
 
sos for not replying, been away for a few days.

Thanks a lot byte, much appreciated. Meeting with our tutor tomorrow so il show him what we have so far :D :D :D

Programming something like this for noobs really is hard. I know loads about PC hardware and a little about software, but programming is soo hard. I remember doing some C++ in my first year, and 90% of the 200 coursework exercises were copied off about 5 people who could do it. We are engineers not programers. LOL.

Ad
 
Last edited:
O ya and i think it was BASIC for apple.

Tutor lookedover it today and said it was some VERRRRY old program that obviously couldn't have been written excel as that was around all those years ago. So he said that the printing parts near the end cannot obviously be compiled by excel, and other parts of the program are not need/ and or wrong.

He didn't tell me any of this when he gave me the program and probably assumed i was some wizzkid :rolleyes: lol
 
Are you trying to make this work in Excel VBA (Excel Visual Basic for Applicaitons) then? What version of Excel are you using? (Just keep asking questions, it'll become easier, promise! :) )
 
hmm thought id replied, obviously not.

Using excel XP and just go to the macro part of the tools and select *** visual basic option in there.

My tutor explained a little of it too me, but ive sent it to him so hopefully he'll be able to bring it up to date in excel.
 
Back
Top Bottom