VB and functions

Status
Not open for further replies.

LimpBagel

Ars Legatus Legionis
11,778
Ok I'm writing a triangle app and I need to pass coordinates from one function to another. Here is a sample of the code so far:

Private Sub cmdArea_Click()

Dim Area As Double

Call Area(X1, X2, X3, Y1, Y2, Y3, Area)

lblArea = Area
End Sub
------------------------------------------------
Private Sub cmdSides_Click()

'User entered inputs
Dim X1, X2, X3, Y1, Y2, Y3 As Double

'Length of each sides
Dim A, B, C As Double

X1 = Val(txtX1.Text)
X2 = Val(txtX2.Text)
X3 = Val(txtX3.Text)
Y1 = Val(txtY1.Text)
Y2 = Val(txtY2.Text)
Y3 = Val(txtY3.Text)

Call Sides(X1, Y1, X2, Y2, A)
lblA = A

Call Sides(X2, Y2, X3, Y3, B)
lblB = B

Call Sides(X3, Y3, X1, Y1, C)
lblC = C


End Sub
-------------------------------------------
Private Sub Sides(X1, Y1, X2, Y2, S)
S = Sqr((X1 - X2) ^ 2 + (Y1 - Y2) ^ 2)

End Sub
-----------------------------
Private Sub Area(X1, X2, X3, Y1, Y2, Y3, Area)

'Semi Perimeter
Dim SP As Double

Call cmdSides_Click(A, B, C)

SP = (A + B + C) / 2
Area = Sqr(SP(SP - A) * (SP - B) * (SP - C))
End Sub
--------------------

As you can probably see, the Area command is trying to pass coordinates that don't reside in it! For some reason I can't get the concept of passing variables to work in my head and it bothers me a lot. So the question is, when the user hits the Area button, how can I get it to send coordinates to the Area function? I need to keep from making things global. Thanks
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
You're not using functions, you're using subs. This is bad.

Code:
Private Sub cmdArea_Click()<P>    Dim X1 As Double, X2 As Double, X3 As Double, Y1 As Double, Y2 As Double, Y3 As Double<P>'Length of each sides<P>    X1 = Val(txtX1.Text)
    X2 = Val(txtX2.Text)
    X3 = Val(txtX3.Text)
    Y1 = Val(txtY1.Text)
    Y2 = Val(txtY2.Text)
    Y3 = Val(txtY3.Text)<P>    lblArea.Caption = Area(X1, X2, X3, Y1, Y2, Y3)<P>End Sub<P>Private Sub cmdSides_Click()<P>'User entered inputs
    Dim X1 As Double, X2 As Double, X3 As Double, Y1 As Double, Y2 As Double, Y3 As Double<P>'Length of each sides<P>    X1 = Val(txtX1.Text)
    X2 = Val(txtX2.Text)
    X3 = Val(txtX3.Text)
    Y1 = Val(txtY1.Text)
    Y2 = Val(txtY2.Text)
    Y3 = Val(txtY3.Text)<P>    lblA.Caption =  Sides(X1, Y1, X2, Y2)
    lblB.Caption = Sides(X2, Y2, X3, Y3)
    lblC.Caption =  Sides(X3, Y3, X1, Y1)<P>End Sub
-------------------------------------------
Private Function Sides(X1 As Double, Y1 As Double, X2 AS Double, Y2 As Double) As Double<P>    Sides = Sqr((X1 - X2) ^ 2 + (Y1 - Y2) ^ 2)<P>End Function
-----------------------------
Private Function Area(X1 As Double, X2 As Double, X3 As Double, Y1 As Double, Y2 As Double, Y3 As Double) As Double<P>    Dim SP As Double, A AS Double, B as Double, C As Double<P>    A =  Sides(X1, Y1, X2, Y2)
    B = Sides(X2, Y2, X3, Y3)
    C =  Sides(X3, Y3, X1, Y1)<P>    SP = (A + B + C) / 2
    Area = Sqr(SP(SP - A) * (SP - B) * (SP - C))<P>End Function

should work, although a number of the things you are doing here don't seem to make sense to me, such as why a user would want to have to click twice, once to find the sides and once to find the area.
 
Status
Not open for further replies.