Area

Using two methods in one class file...

Page 1 of 1

9 Replies - 2192 Views - Last Post: 29 July 2008 - 11:26 AM Rate Topic: -----

#1 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Area

Posted 28 July 2008 - 10:05 AM

This is suppose to have a Overloads method with in the file, and modify the calc button event. Here's the codes I have right now.

Option Explicit On
Option Strict On


Public Class Square
	Private _side As Integer
	Private _length As Integer
	Private _width As Integer
	

   Public Property Side() As Integer
		Get
			Return _side
		End Get
		Set(ByVal value As Integer)
			If value > 0 Then
				_side = value

			Else
				_side = 0

			End If
		End Set
	End Property

	Public Property Length() As Integer
		Get
			Return _length
		End Get
		Set(ByVal value As Integer)
			If value > 0 Then
				_length = value

			Else
				_length = 0

			End If
		End Set
	End Property

	Public Property Width() As Integer
		Get
			Return _width
		End Get
		Set(ByVal value As Integer)
			If value > 0 Then
				_width = value

			Else
				_width = 0

			End If
		End Set
	End Property
   
	Public Sub New()	' default constructor
		_side = 0

	End Sub

	Public Function CalculateArea() As Integer<--error here says 'has multiple definitions with identical signatures'
		Return _side * _side

	End Function

	Public Overloads Function CalculateArea() As Integer
		Return _length * _width

	End Function
End Class



Calc button code:


  Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
		'calculates and displays the area of a square

		Dim mySquare As New Square
		Dim area As Integer

		'assign side measurement to the
		'Square object's property

		Integer.TryParse(Me.xSideTextBox.Text, mySquare.Side)

		'calculate and display the area
		area = mySquare.CalculateArea()
		Me.xAreaLabel.Text = area.ToString

		Me.xSideTextBox.Focus()

	End Sub



No errors here so far..

Is This A Good Question/Topic? 0
  • +

Replies To: Area

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Area

Posted 28 July 2008 - 11:31 AM

To create an overloaded function each will have the same name, as you already have done. However, each method will have a different signature. By this I mean, it will have a different type or number of arguments that are being passed to the function/method. Each one must be different, no two can be the same.

Example:
	'calculates the area of a rectangle
	Public Overloads Function CalculateArea() As Integer
		Return _length * _width
	End Function

	'calculates the area of a rectangle
	Public Overloads Function CalculateArea(ByVal width As Integer, ByVal length As Integer) As Integer
		Return length * width
	End Function

	'calculates the area of a 3-D rectangle
	Public Overloads Function CalculateArea(ByVal width As Integer, ByVal length As Integer, ByVal depth As Integer) As Integer
		Return length * width * depth
	End Function

	'calculates the area of a circle
	Public Overloads Function CalculateArea(ByVal radius AS Integer) As Integer
		Return ((radius * radius) * 3.14)
	End Function


Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Area

Posted 28 July 2008 - 12:40 PM

I made the following change,but nothing changed. This is the area of a square also, I forgot to mention it earlier, sorry.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Area

Posted 28 July 2008 - 01:20 PM

Can you post the your current code?
Was This Post Helpful? 0
  • +
  • -

#5 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Area

Posted 28 July 2008 - 01:58 PM

opps sorry :wub: trying to do three at a time...I forgot Here's the code:


Option Explicit On
Option Strict On


Public Class Square
	Private _side As Integer
	Private _length As Integer
	Private _width As Integer
	

   Public Property Side() As Integer
		Get
			Return _side
		End Get
		Set(ByVal value As Integer)
			If value > 0 Then
				_side = value

			Else
				_side = 0

			End If
		End Set
	End Property

	Public Property Length() As Integer
		Get
			Return _length
		End Get
		Set(ByVal value As Integer)
			If value > 0 Then
				_length = value

			Else
				_length = 0

			End If
		End Set
	End Property

	Public Property Width() As Integer
		Get
			Return _width
		End Get
		Set(ByVal value As Integer)
			If value > 0 Then
				_width = value

			Else
				_width = 0

			End If
		End Set
	End Property

   
	Public Sub New()	' default constructor
		_side = 0

	End Sub

	Public Function CalculateArea() As Integer
		Return _side * _side

	End Function

	Public Overloads Function CalculateArea(ByVal width As Integer, ByVal length As Integer) As Integer
		Return length * width

	End Function


Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Area

Posted 28 July 2008 - 03:41 PM

Are you getting any errors or is it just not working?

Can you post the code where you instantiate an object of this class and call its methods?
Was This Post Helpful? 0
  • +
  • -

#7 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Area

Posted 29 July 2008 - 01:09 AM

Here's the code for the file:


Option Explicit On
Option Strict On


Public Class Square
	Private _side As Integer
	Private _length As Integer
	Private _width As Integer
	

   Public Property Side() As Integer
		Get
			Return _side
		End Get
		Set(ByVal value As Integer)
			If value > 0 Then
				_side = value

			Else
				_side = 0

			End If
		End Set
	End Property

	Public Property Length() As Integer
		Get
			Return _length
		End Get
		Set(ByVal value As Integer)
			If value > 0 Then
				_length = value

			Else
				_length = 0

			End If
		End Set
	End Property

	Public Property Width() As Integer
		Get
			Return _width
		End Get
		Set(ByVal value As Integer)
			If value > 0 Then
				_width = value

			Else
				_width = 0

			End If
		End Set
	End Property

   
	Public Sub New()	' default constructor
		_side = 0

	End Sub

	Public Function CalculateArea() As Integer<--error here says 'function "CalculateArea" must be declared "Overloads" because another "CalculateArea" is declared "Overloads" or "Overrides".'
		Return _side * _side

	End Function

	Public Overloads Function CalculateArea(ByVal width As Integer, ByVal length As Integer) As Integer
		Return length * width

	End Function
End Class



This post has been edited by LadyWolf: 29 July 2008 - 01:13 AM

Was This Post Helpful? 0
  • +
  • -

#8 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Area

Posted 29 July 2008 - 01:16 AM

Here's the code for the form, as of yet there's no errors here:


Option Explicit On
Option Strict On

Public Class MainForm

	Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
		Me.Close()
	End Sub

	Private Sub xSideTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xSideTextBox.Enter
		Me.xSideTextBox.SelectAll()
	End Sub

	Private Sub xSideTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles xSideTextBox.KeyPress
		' accept only numbers, the period, and the Backspace

		If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
			AndAlso e.KeyChar <> "." _
			AndAlso e.KeyChar <> ControlChars.Back Then
			e.Handled = True
		End If
	End Sub

	Private Sub xSideTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xSideTextBox.TextChanged
		Me.xAreaLabel.Text = String.Empty
	End Sub

	Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
		'calculates and displays the area of a square

		Dim mySquare As New Square
		Dim area As Integer

		'assign side measurement to the
		'Square object's property

		Integer.TryParse(Me.xSideTextBox.Text, mySquare.Side)

		'calculate and display the area
		area = mySquare.CalculateArea()
		Me.xAreaLabel.Text = area.ToString

		Me.xSideTextBox.Focus()

	End Sub
End Class


This post has been edited by LadyWolf: 29 July 2008 - 01:18 AM

Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Area

Posted 29 July 2008 - 09:26 AM

You need to include the keyword 'overloads' in each of the functions definitions. If you look at the example I posted previously, you will see what I mean.
Was This Post Helpful? 1
  • +
  • -

#10 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Area

Posted 29 July 2008 - 11:26 AM

View Postjayman9, on 29 Jul, 2008 - 12:26 PM, said:

You need to include the keyword 'overloads' in each of the functions definitions. If you look at the example I posted previously, you will see what I mean.


ahhh okay I see now! Thank you for the help, I really appreciate it :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1