Passing Classes

R

RBCC

Public Class fraction
Dim m_numerator As Int16
Dim m_denominator As Int16

Public Event zerodenom()


Public Property numerator() As Int16
Get
Return m_numerator
End Get
Set(ByVal Value As Int16)
Value = m_numerator
End Set
End Property
Public Property denominator() As Int16
Get
Return m_denominator
End Get
Set(ByVal Value As Int16)
Value = m_denominator
End Set
End Property

Public Function reduce(ByVal numerator As Int16, ByVal denominator
As Int16) As Int16
Dim temp As Int16

Do While denominator <> 0
temp = denominator
denominator = numerator Mod denominator
numerator = temp
Loop
Return numerator
End Function
Public Function calculate_common_denominator(ByVal denominator As
Int16, ByVal denominator1 As Int16)
Dim common As Int16
common = denominator * denominator1

Return common
End Function

Public Function add(ByVal frac1 As fraction, ByVal frac2 As
fraction)

How do I pass a class so I can use its members in a function? In here I
need to use frac1.numerator & Denominator frac2.numerator and
denominator how is this done by passing the objects?
John


End Function

End Class

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents btnAdd As System.Windows.Forms.Button
Friend WithEvents txtdenom1 As System.Windows.Forms.TextBox
Friend WithEvents txtnum1 As System.Windows.Forms.TextBox
Friend WithEvents txtdenom2 As System.Windows.Forms.TextBox
Friend WithEvents txtsumnum As System.Windows.Forms.TextBox
Friend WithEvents txtsumdenom As System.Windows.Forms.TextBox
Friend WithEvents txtnum2 As System.Windows.Forms.TextBox
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
Friend WithEvents ListBox3 As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.txtdenom1 = New System.Windows.Forms.TextBox
Me.txtnum1 = New System.Windows.Forms.TextBox
Me.txtnum2 = New System.Windows.Forms.TextBox
Me.txtdenom2 = New System.Windows.Forms.TextBox
Me.txtsumnum = New System.Windows.Forms.TextBox
Me.txtsumdenom = New System.Windows.Forms.TextBox
Me.btnAdd = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.ListBox2 = New System.Windows.Forms.ListBox
Me.ListBox3 = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'txtdenom1
'
Me.txtdenom1.Location = New System.Drawing.Point(8, 112)
Me.txtdenom1.Name = "txtdenom1"
Me.txtdenom1.Size = New System.Drawing.Size(64, 20)
Me.txtdenom1.TabIndex = 0
Me.txtdenom1.Text = ""
'
'txtnum1
'
Me.txtnum1.Location = New System.Drawing.Point(8, 80)
Me.txtnum1.Name = "txtnum1"
Me.txtnum1.Size = New System.Drawing.Size(64, 20)
Me.txtnum1.TabIndex = 1
Me.txtnum1.Text = ""
'
'txtnum2
'
Me.txtnum2.Location = New System.Drawing.Point(144, 80)
Me.txtnum2.Name = "txtnum2"
Me.txtnum2.Size = New System.Drawing.Size(64, 20)
Me.txtnum2.TabIndex = 2
Me.txtnum2.Text = ""
'
'txtdenom2
'
Me.txtdenom2.Location = New System.Drawing.Point(144, 120)
Me.txtdenom2.Name = "txtdenom2"
Me.txtdenom2.Size = New System.Drawing.Size(64, 20)
Me.txtdenom2.TabIndex = 3
Me.txtdenom2.Text = ""
'
'txtsumnum
'
Me.txtsumnum.Location = New System.Drawing.Point(376, 80)
Me.txtsumnum.Name = "txtsumnum"
Me.txtsumnum.Size = New System.Drawing.Size(64, 20)
Me.txtsumnum.TabIndex = 4
Me.txtsumnum.Text = ""
'
'txtsumdenom
'
Me.txtsumdenom.Location = New System.Drawing.Point(376, 112)
Me.txtsumdenom.Name = "txtsumdenom"
Me.txtsumdenom.Size = New System.Drawing.Size(64, 20)
Me.txtsumdenom.TabIndex = 5
Me.txtsumdenom.Text = ""
'
'btnAdd
'
Me.btnAdd.Font = New System.Drawing.Font("Microsoft Sans Serif",
14.25!, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnAdd.Location = New System.Drawing.Point(80, 96)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(48, 32)
Me.btnAdd.TabIndex = 6
Me.btnAdd.Text = "+"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif",
15.75!, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(248, 96)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(32, 24)
Me.Label1.TabIndex = 7
Me.Label1.Text = "="
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point(368, 104)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(80, 4)
Me.ListBox1.TabIndex = 8
'
'ListBox2
'
Me.ListBox2.Location = New System.Drawing.Point(144, 112)
Me.ListBox2.Name = "ListBox2"
Me.ListBox2.Size = New System.Drawing.Size(72, 4)
Me.ListBox2.TabIndex = 9
'
'ListBox3
'
Me.ListBox3.Location = New System.Drawing.Point(8, 104)
Me.ListBox3.Name = "ListBox3"
Me.ListBox3.Size = New System.Drawing.Size(64, 4)
Me.ListBox3.TabIndex = 10
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(472, 266)
Me.Controls.Add(Me.ListBox3)
Me.Controls.Add(Me.ListBox2)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.txtsumdenom)
Me.Controls.Add(Me.txtsumnum)
Me.Controls.Add(Me.txtdenom2)
Me.Controls.Add(Me.txtnum2)
Me.Controls.Add(Me.txtnum1)
Me.Controls.Add(Me.txtdenom1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
Dim WithEvents clsfraction As New fraction
Dim WithEvents clsfraction1 As New fraction

Dim denominator, denominator1 As Int16







Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim common, numer1, numer2, denom, total As Int16
Dim gcd As Int16 'holds value of gcd



numer1 = Val(txtnum1.Text)
numer2 = Val(txtnum2.Text)
denominator = Val(txtdenom1.Text)
denominator1 = Val(txtdenom2.Text)

common = clsfraction.calculate_common_denominator(denominator,
denominator1)

total = clsfraction.add(clsfraction, clsfraction1)
gcd = clsfraction.reduce(total, common)

common = common / gcd
total = total / gcd

txtsumnum.Text = total
txtsumdenom.Text = common


End Sub



Private Sub clsfraction1_zerodenom() Handles clsfraction1.zerodenom
MessageBox.Show("Error-Zero in the denominator")

End Sub


End Class
 
R

Richard T. Edwards

Dim newClass as new fraction

RBCC said:
Public Class fraction
Dim m_numerator As Int16
Dim m_denominator As Int16

Public Event zerodenom()


Public Property numerator() As Int16
Get
Return m_numerator
End Get
Set(ByVal Value As Int16)
Value = m_numerator
End Set
End Property
Public Property denominator() As Int16
Get
Return m_denominator
End Get
Set(ByVal Value As Int16)
Value = m_denominator
End Set
End Property

Public Function reduce(ByVal numerator As Int16, ByVal denominator
As Int16) As Int16
Dim temp As Int16

Do While denominator <> 0
temp = denominator
denominator = numerator Mod denominator
numerator = temp
Loop
Return numerator
End Function
Public Function calculate_common_denominator(ByVal denominator As
Int16, ByVal denominator1 As Int16)
Dim common As Int16
common = denominator * denominator1

Return common
End Function

Public Function add(ByVal frac1 As fraction, ByVal frac2 As
fraction)

How do I pass a class so I can use its members in a function? In here I
need to use frac1.numerator & Denominator frac2.numerator and
denominator how is this done by passing the objects?
John


End Function

End Class

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents btnAdd As System.Windows.Forms.Button
Friend WithEvents txtdenom1 As System.Windows.Forms.TextBox
Friend WithEvents txtnum1 As System.Windows.Forms.TextBox
Friend WithEvents txtdenom2 As System.Windows.Forms.TextBox
Friend WithEvents txtsumnum As System.Windows.Forms.TextBox
Friend WithEvents txtsumdenom As System.Windows.Forms.TextBox
Friend WithEvents txtnum2 As System.Windows.Forms.TextBox
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
Friend WithEvents ListBox3 As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.txtdenom1 = New System.Windows.Forms.TextBox
Me.txtnum1 = New System.Windows.Forms.TextBox
Me.txtnum2 = New System.Windows.Forms.TextBox
Me.txtdenom2 = New System.Windows.Forms.TextBox
Me.txtsumnum = New System.Windows.Forms.TextBox
Me.txtsumdenom = New System.Windows.Forms.TextBox
Me.btnAdd = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.ListBox2 = New System.Windows.Forms.ListBox
Me.ListBox3 = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'txtdenom1
'
Me.txtdenom1.Location = New System.Drawing.Point(8, 112)
Me.txtdenom1.Name = "txtdenom1"
Me.txtdenom1.Size = New System.Drawing.Size(64, 20)
Me.txtdenom1.TabIndex = 0
Me.txtdenom1.Text = ""
'
'txtnum1
'
Me.txtnum1.Location = New System.Drawing.Point(8, 80)
Me.txtnum1.Name = "txtnum1"
Me.txtnum1.Size = New System.Drawing.Size(64, 20)
Me.txtnum1.TabIndex = 1
Me.txtnum1.Text = ""
'
'txtnum2
'
Me.txtnum2.Location = New System.Drawing.Point(144, 80)
Me.txtnum2.Name = "txtnum2"
Me.txtnum2.Size = New System.Drawing.Size(64, 20)
Me.txtnum2.TabIndex = 2
Me.txtnum2.Text = ""
'
'txtdenom2
'
Me.txtdenom2.Location = New System.Drawing.Point(144, 120)
Me.txtdenom2.Name = "txtdenom2"
Me.txtdenom2.Size = New System.Drawing.Size(64, 20)
Me.txtdenom2.TabIndex = 3
Me.txtdenom2.Text = ""
'
'txtsumnum
'
Me.txtsumnum.Location = New System.Drawing.Point(376, 80)
Me.txtsumnum.Name = "txtsumnum"
Me.txtsumnum.Size = New System.Drawing.Size(64, 20)
Me.txtsumnum.TabIndex = 4
Me.txtsumnum.Text = ""
'
'txtsumdenom
'
Me.txtsumdenom.Location = New System.Drawing.Point(376, 112)
Me.txtsumdenom.Name = "txtsumdenom"
Me.txtsumdenom.Size = New System.Drawing.Size(64, 20)
Me.txtsumdenom.TabIndex = 5
Me.txtsumdenom.Text = ""
'
'btnAdd
'
Me.btnAdd.Font = New System.Drawing.Font("Microsoft Sans Serif",
14.25!, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnAdd.Location = New System.Drawing.Point(80, 96)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(48, 32)
Me.btnAdd.TabIndex = 6
Me.btnAdd.Text = "+"
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif",
15.75!, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(248, 96)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(32, 24)
Me.Label1.TabIndex = 7
Me.Label1.Text = "="
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point(368, 104)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(80, 4)
Me.ListBox1.TabIndex = 8
'
'ListBox2
'
Me.ListBox2.Location = New System.Drawing.Point(144, 112)
Me.ListBox2.Name = "ListBox2"
Me.ListBox2.Size = New System.Drawing.Size(72, 4)
Me.ListBox2.TabIndex = 9
'
'ListBox3
'
Me.ListBox3.Location = New System.Drawing.Point(8, 104)
Me.ListBox3.Name = "ListBox3"
Me.ListBox3.Size = New System.Drawing.Size(64, 4)
Me.ListBox3.TabIndex = 10
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(472, 266)
Me.Controls.Add(Me.ListBox3)
Me.Controls.Add(Me.ListBox2)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.txtsumdenom)
Me.Controls.Add(Me.txtsumnum)
Me.Controls.Add(Me.txtdenom2)
Me.Controls.Add(Me.txtnum2)
Me.Controls.Add(Me.txtnum1)
Me.Controls.Add(Me.txtdenom1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
Dim WithEvents clsfraction As New fraction
Dim WithEvents clsfraction1 As New fraction

Dim denominator, denominator1 As Int16







Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim common, numer1, numer2, denom, total As Int16
Dim gcd As Int16 'holds value of gcd



numer1 = Val(txtnum1.Text)
numer2 = Val(txtnum2.Text)
denominator = Val(txtdenom1.Text)
denominator1 = Val(txtdenom2.Text)

common = clsfraction.calculate_common_denominator(denominator,
denominator1)

total = clsfraction.add(clsfraction, clsfraction1)
gcd = clsfraction.reduce(total, common)

common = common / gcd
total = total / gcd

txtsumnum.Text = total
txtsumdenom.Text = common


End Sub



Private Sub clsfraction1_zerodenom() Handles clsfraction1.zerodenom
MessageBox.Show("Error-Zero in the denominator")

End Sub


End Class
 
J

John Marshall

So where do I put this? In the top of the Form code or the the class
code?
Dim newClass as new fraction?
 
G

Guest

...

Dim Object as myClas

Object = new myClas

SomeFunction(Object

...

Public Function SomeFunction(ByVal Object as myClass) as boolea

' Then you just call the properties or functio
Object.MyFunction(
Object.Name = "This is my object

..

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top