-
Aug 11th, 2020, 01:03 PM
#1
Thread Starter
Member
[RESOLVED] Problem in class defination
Hi,
For learning purpose, I tried to write a simple code for calculating rectangle area in Console Application.
For this, I tried to use Class (I want to learn classes). I learned from a tutorial which say we need to use private declaration in the class:
Code:
Public Class cRectArea
Private _lenght As Double
Private _width As Double
Function GetArea() As Double
GetArea = _lenght * _width
End Function
Public Property Lenght As Double
Set(ByVal value As Double)
If value > 0 Then
value = _lenght
End If
End Set
Get
Return Lenght
End Get
End Property
Public Property Width As Double
Set(ByVal value As Double)
If value > 0 Then
_width = value
End If
End Set
Get
Return Width
End Get
End Property
End Class
When I run the code, the answer is 0.
Code:
Module Module1
Sub Main()
Dim Area As New cRectArea
Area.Lenght = 4
Area.Width = 6
Console.WriteLine("Area is: " & Area.GetArea())
Console.ReadLine()
End Sub
End Module
What is the problem?
-
Aug 11th, 2020, 01:10 PM
#2
Re: Problem in class defination
Your return statements should return the associated private variable, not the Property name.
Also note that you have spelled length wrong everywhere, but that isn't causing your problem.
-
Aug 11th, 2020, 01:18 PM
#3
Thread Starter
Member
Re: Problem in class defination
Originally Posted by OptionBase1
Your return statements should return the associated private variable, not the Property name.
Also note that you have spelled length wrong everywhere, but that isn't causing your problem.
I tried to change the return statement to associated private variable, but it still gives 0 as the answer.
-
Aug 11th, 2020, 01:21 PM
#4
Re: Problem in class defination
Show your code from that attempt. Did you change it for both length and width?
-
Aug 11th, 2020, 01:22 PM
#5
Thread Starter
Member
Re: Problem in class defination
Originally Posted by OptionBase1
Show your code from that attempt. Did you change it for both length and width?
Code:
Public Class cRectArea
Private _length As Double
Private _width As Double
Function GetArea() As Double
GetArea = _length * _width
End Function
Public Property Lenght As Double
Set(ByVal value As Double)
If value > 0 Then
value = _length
End If
End Set
Get
Return _length
End Get
End Property
Public Property Width As Double
Set(ByVal value As Double)
If value > 0 Then
_width = value
End If
End Set
Get
Return _width
End Get
End Property
End Class
-
Aug 11th, 2020, 01:28 PM
#6
Re: Problem in class defination
When something doesn't work right, it's time to dig into the code... set some breakpoints, specifically in the GetArea function... as that seems to be the point of contention. Then when you get to the breka point, check the value of everything... make sure they are what you think they should be. If they are, then it's something in the funciton itself, if they aren't then you can start looking into why they aren't. Also, your function should RETURN somehting... again, your're uysing the older pre-.NET Classic VB style ... which may or may not be the issue.
-tg
-
Aug 11th, 2020, 01:34 PM
#7
Thread Starter
Member
Re: Problem in class defination
Originally Posted by techgnome
When something doesn't work right, it's time to dig into the code... set some breakpoints, specifically in the GetArea function... as that seems to be the point of contention. Then when you get to the breka point, check the value of everything... make sure they are what you think they should be. If they are, then it's something in the funciton itself, if they aren't then you can start looking into why they aren't. Also, your function should RETURN somehting... again, your're uysing the older pre-.NET Classic VB style ... which may or may not be the issue.
-tg
Do you have any tutorial link (from youtube) which can teach the modern and correct way of using classes?
I wrote this code using a YouTube tutorial.
-
Aug 11th, 2020, 02:30 PM
#8
Re: Problem in class defination
All you need is the following
Code:
Public Class RectArea
Public ReadOnly Property Area() As Double
get
Return Length * Width
End get
End Property
Public Property Length As Double
Public Property Width As Double
End Class
Then
Code:
Dim rectArea as New RectArea With{.Width = 6, .Length = 4}
MessageBox.Show($"Area: {rectArea.Area}")
Or
Code:
Dim rectArea As New RectArea
rectArea.Width = 6
rectArea.Length = 4
MessageBox.Show($"Area: {rectArea.Area}")
Tip, don't prefix a class name with c or cls. Place classes into a sub folder in your project and use a namespace e.g. if the project namespace is SomeProject then classes are in SomeProject.Classes
-
Aug 11th, 2020, 07:53 PM
#9
Re: Problem in class defination
You didn't read your code very well, which is exactly why you need to debug code and not just read it. The issue would then have been obvious:
Code:
Public Class cRectArea
Private _length As Double
Private _width As Double
Function GetArea() As Double
GetArea = _length * _width
End Function
Public Property Lenght As Double
Set(ByVal value As Double)
If value > 0 Then
value = _length
End If
End Set
Get
Return _length
End Get
End Property
Public Property Width As Double
Set(ByVal value As Double)
If value > 0 Then
_width = value
End If
End Set
Get
Return _width
End Get
End Property
End Class
-
Aug 11th, 2020, 07:54 PM
#10
Re: Problem in class defination
Originally Posted by kareninstructor
All you need is the following
Code:
Public Class RectArea
Public ReadOnly Property Area() As Double
get
Return Length * Width
End get
End Property
Public Property Length As Double
Public Property Width As Double
End Class
That would allow setting the properties to negative values, which the intended code appears to disallow.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|