Results 1 to 10 of 10

Thread: [RESOLVED] Problem in class defination

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Resolved [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?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Re: Problem in class defination

    Quote Originally Posted by OptionBase1 View Post
    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.

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Problem in class defination

    Show your code from that attempt. Did you change it for both length and width?

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Re: Problem in class defination

    Quote Originally Posted by OptionBase1 View Post
    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

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Re: Problem in class defination

    Quote Originally Posted by techgnome View Post
    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.

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Problem in class defination

    Quote Originally Posted by kareninstructor View Post
    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
  •  



Click Here to Expand Forum to Full Width