Triangle Property (class)

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Triangle Property (class)

1. Registered User
Join Date
Nov 2006
Posts
29

## Triangle Property (class)

I have a program that takes user input from a textbox. Based on those 3 numbers it will tell them whether it is a right triangle, equilateral triangle or not a triangle. I've written 3 properties (get/set) to take the lengths. Now I need two readonly (get only) properties s to determine whether something is right or equilateral. I'm really not sure how I would write the formula in vb. I understand a right triangle is a2 + b2 = c2 but i'm not sure how I could process a math formula inside a property. I guess I am also trying to learn how these two new properties would interact with the 3 already written? Here is what I have so far. I do not yet have anything for the right triangle. Does this make sense and anybody know how to easily calculate a right triangle. The formulas online haven't really made sense to me in order to write it out in vb.

Code:
```Public Class Triangle

'declare integers for 3 triangle sides
Dim m_intSide1 As Integer
Dim m_intSide2 As Integer
Dim m_intSide3 As Integer

'declare integers for triangle type
Dim m_intRight As Integer
Dim m_intEquilateral As Integer

'Triangle constructor, sides supplied
Public Sub New(ByVal side1Value As Integer, ByVal side2Value As Integer, _
ByVal side3Value As Integer, ByVal RightTriangleValue As Integer, _
ByVal EquilateralValue As Integer)

m_intSide1 = side1Value
m_intSide2 = side2Value
m_intSide3 = side3Value
m_intRight = RightTriangleValue
m_intEquilateral = EquilateralValue

End Sub 'New

'property side1
Public Property Side1() As Integer

'return m_intSide1 value
Get
Return m_intSide1
End Get 'end of Get accessor

'set m_intside1 value
Set(ByVal Value As Integer)
'if side 1 value entered is valid
If (Value > 0) Then
m_intSide1 = Value
Else
m_intSide1 = 0  'set invalid input to 0
End If
End Set 'end of set accessor
End Property

'property side2
Public Property Side2() As Integer

'return m_intSide2 value
Get
Return m_intSide2
End Get 'end of Get accessor

'set m_intside2 value
Set(ByVal Value As Integer)
'if side 2 value entered is valid
If (Value > 0) Then
m_intSide2 = Value
Else
m_intSide2 = 0  'set invalid input to 0
End If
End Set 'end of set accessor
End Property

'property side3
Public Property Side3() As Integer

'return m_intSide3 value
Get
Return m_intSide3
End Get 'end of Get accessor

'set m_intside3 value
Set(ByVal Value As Integer)
'if side 3 value entered is valid
If (Value > 0) Then
m_intSide3 = Value
Else
m_intSide3 = 0  'set invalid input to 0
End If
End Set 'end of set accessor
End Property

'property IsRight (copied from below, work in progress)
Public ReadOnly Property IsRight() As Integer

'return m_intRightTriangle value
Get
If (Side1 = Side2 AndAlso Side2 = Side3) Then
Return m_intRight
End If

End Get 'end of Get accessor

End Property

'property IsEquilateral
Public ReadOnly Property IsEquilateral() As Integer

'return m_intEquilateral value
Get
If (Side1 = Side2 AndAlso Side2 = Side3) Then
Return m_intEquilateral
End If

End Get 'end of Get accessor

End Property

End Class   'Triangle```

2. Registered User
Join Date
Dec 2004
Posts
163
geo039,

I would consider making IsRight and IsEquilateral methods (public functions) that return boolean values.

Kerry Moorman

3. Registered User
Join Date
Nov 2006
Posts
29
That's also how I approached it and since this is a hw assignment there are requirements and it has to be in a property but due to the timeframe I have normally at this point I just try to get it to work and take whatever credit I can get, so I may end up just putting it in a boolean function.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source