?
Solved

Class project - Need help understanding shared methods and how overloading is used

Posted on 2005-05-12
11
Medium Priority
?
183 Views
Last Modified: 2010-04-23
I am attending a VB.NET class and am working on my final project.  I need to finish it today .. but more important, I need to understand what I am being told to do.  The following text describes a part of the final project where I need to build a class file.

The following defines the private data structure of the Circle class:
· A PointD member called _center
· A Double member called _radius
The following defines the public interface to the class:
· A Read/Write Property for each of the above-mentioned private variables
o Note: Radius can only be a positive number – throw an exception otherwise
· A Read Only Property called Diameter that returns the diameter of the circle
· A function called Area that returns the area of the defined circle
· A function called Circumference that returns the circumference of the defined circle
· A constructor that accepts both a center and radius value and sets the private variables (hint:
use the above-defined properties inside the constructor for the setting to leverage the existing
code)

· A second constructor that accepts only the radius value

The following defines the shared method of the class (Remember, shared methods do not require an
instantiated class object to be used!)

· A function called Area that accepts a double “radius” parameter and returns the area of the
circle defined by that radius
(HINT: Use overloading or it will not work!)

· A function called Circumference that accepts a double “radius” parameter and returns the
circumference of the circle defined by that radius  
(HINT: Use overloading or it will not work!)

Mathematical Functions Helper:
· Area = Math.PI * (Radius ^ 2)
· Circumference = 2 * Math.PI * Radius
· Diameter = 2 * Radius

I'm getting stuck on understanding what needs to be done with the "Shared Method" and how it needs to be built.  I believe that the shared Method accesses the public functions area and circumference, but I'm not sure.

So, what am I looking for?  I'm sure there are many of you that can whip out the above code in no time at all.  This is ok, I would appreciate this.  But more important, I need to understand this.  I'm not taking the class to just get a good grade, I'm taking the class to get a better understanding of VB.NET.

Because we may need to go back and forth a few times until I understand this, and because it would be helpful to have this part of the project developed (as long as I understand it), I'm assigning 400 points for this solution.  Thanks in advance for your assistance.

Frank
0
Comment
Question by:FrankG77
  • 7
  • 4
11 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13990035
Public Class Circle
     
...
Public Shared Function Area(ByVal radius as Double) As Double
     Return Math.PI * radius * radius
End Function
...
End Class

Basically the Shared modifier means that I don't need to declare a Circle object to use the Area method of the Circle class.  Shared methods can only access shared members and other shared methods.

usage example 1: (Area method is shared)

messagebox.show(Circle.Area(2).tostring)

usage example 2: (If area method was not declared as shared)

dim c as new Circle
messagebox.show(Circle.Area(2).tostring)

The idea is that why should I have to declare a circle just to figure out the area of one?
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13990113
As for the overloading...

Public Function Area() As Double
     Return Math.PI * me._radius * me._radius
End Function

The overloading will be handled automatically but may be specified by adding the Overloads keyword

"The Overloads keyword is optional when overloading, but if any overloaded member uses the Overloads keyword, then all other overloaded members with the same name must also specify this keyword"

A function/procedure signature is made up of its name and parameter list so that you can have many functions within the same namespace with the same name as long as they have different parameter lists.
0
 

Author Comment

by:FrankG77
ID: 13990305
Here is my code.  I'm sort of getting what you are saying, but not completely.  Also, to tell you a little more about this, I'm building an assembly which contains this class, a rectangle class, and a Triangle class.  Once this is complete, I'm building a windows application and a console application to access the assembly. I have 4 1/2 hours to complete this ...lol  Please take a look at the code below.  See the "??" .. these are areas I'm struggling with.  Also what about the other sections of the code, do they look alright, or am I missing anything else?  Please compare the code below to the class instructions above.  I'm hoping once you "fill in the blanks", I'll understand better.  The next two classes will be similar, so once I understand how this one works, the other two should be easier.  Thanks!

Public Class Class1
    Public Structure PointD
        Public x As Double
        Public y As Double
    End Structure
    Public Sub New(ByVal _x As Double, ByVal _y As Double)
        x = _x : y = _y
    End Sub

    Class Circle
        Private _center As PointD
        Private _radius As Double
        Public Property Center() As PointD
            Get
                Return _center
            End Get
            Set(ByVal Value As PointD)
                _center = Value
            End Set
        End Property
        Public Property Radius() As Double
            Get
                Return _radius
            End Get
            Set(ByVal Value As Double)
                ' need to throw an exception here if not positive
                _radius = Value
            End Set
        End Property
        Public ReadOnly Property Diameter() As Double
            Get
                Return (_diameter)
            End Get
        End Property
        Public Function Area(ByVal _area As Double)
            Area = Math.PI * (Radius ^ 2)
        End Function

        Public Function Circumference(ByVal _circ As Double)
            Circumference = 2 * Math.PI * Radius
        End Function
        Private Sub New(ByVal _center As PointD, ByVal _radius As Double)
            ???
        End Sub

        Public Sub New(ByVal _radius As Double)
            ???
        End Sub

        Public Shared Function Area(ByVal ?? As Double)
            ???
        End Function

        Public Shared Function Circumference(ByVal ?? As Double)
            ??
        End Function

    End Class

    Class Rectangle

    End Class

    Class Triangle

    End Class

End Class
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:FrankG77
ID: 13991064
Your veriabge concerning overloading helped me a lot - thanks!  I've been working on this more and I think I'm getting closer, but I'm still not understanding serveral things.  I'd appreciate hearing back from you.  I'm really interested in what the code will look like when you've completed it.
Thanks
0
 

Author Comment

by:FrankG77
ID: 13991170
BriCrowe ... you still with me???
0
 

Author Comment

by:FrankG77
ID: 13991319
Can someone else help me with this?   Thanks!!  I'm playing "Beat the clock .. your assistance is great appreciated"  I've increased the points to 500!
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13991485
Sorry had some work to do :-)

        Private Sub New(ByVal _center As PointD, ByVal _radius As Double)
            ' I'm not sure what a PointD is...is that like a PointF (x,y coordinates with floating point values)?
        End Sub

        Public Sub New(ByVal radius As Double)
            'might want to do some error checking here to make sure the radius is > 0
            me.radius = radius 'using property
            'I'm not sure what's to be done about the center here? default to 0,0?
        End Sub

        Public Shared Function Area(ByVal radius As Double)
            return Math.pi * radius * radius
        End Function

        Public Shared Function Circumference(ByVal radius As Double)
            return math.pi * 2 * radius
        End Function

hope this helps...
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 1500 total points
ID: 13991490
correction...forgot the return types on each of the last 2 functions

 Public Shared Function Area(ByVal radius As Double) As Double

 Public Shared Function Circumference(ByVal radius As Double) As Double
0
 

Author Comment

by:FrankG77
ID: 13991581
ok .. If we are calling the Public Shared Functions ... what is the purpose of the public functions?  Are they even getting accessed?  Also, I can't figure out what x and y are for.  Do you know?   Thanks.
0
 

Author Comment

by:FrankG77
ID: 13991673
I beleive that somehow the shared function needs to access the non shared function and that the non shared function is where the work is to be done.  Something like this .. only this does not work ...  It ends up calling its slef ... which isn't a good thing. lol  Ideas?

 Public Function Area() As Double
            Return Math.PI * (Me._radius ^ 2)
        End Function

Public Shared Function Area(ByVal Radius As Double) As Double
            Return Circle.Area(Radius)
        End Function

0
 

Author Comment

by:FrankG77
ID: 14023939
Well .. I didn't get out of this all that I wanted, but you were helpful.  Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question