Sunday, January 17, 2010

COM & .NET

ACTIVE X DLL PROJECT MeanCaslculator2 having Class MeanCalc2:

Notes:1)Build DLL, Register it.

Option Explicit
Dim mintValue As Integer
Dim mdblValues() As Double
Dim mdblMean As Double

Public Sub Class_Initialize()
Reset
End Sub

Public Sub AddInput(InputValue As Double)
mintValue = mintValue + 1
ReDim Preserve mdblValues(mintValue)
mdblValues(mintValue) = InputValue
End Sub

Public Sub DoCalculation()
Dim iValue As Integer
mdblMean = 0#
If (mintValue = 0) Then Exit Sub

For iValue = 1 To mintValue
mdblMean = mdblMean + mdblValues(iValue)
Next iValue

mdblMean = mdblMean / mintValue

End Sub

Public Function GetOutput() As Double
GetOutput = mdblMean
End Function

Public Sub Reset()
mintValue = 0
End Sub


********************************

Using ACTIVEX DLL CREATED FROM VB 6.0 in VB.NET (Early Binding):

Notes:
1)Add reference to ActiveX DLL MeanCalc2 having Class MeanCalc2

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 TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents CmdADD As System.Windows.Forms.Button
Friend WithEvents CmdReset As System.Windows.Forms.Button
Friend WithEvents CmdCalculate As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.CmdADD = New System.Windows.Forms.Button()
Me.CmdReset = New System.Windows.Forms.Button()
Me.CmdCalculate = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'CmdADD
'
Me.CmdADD.Location = New System.Drawing.Point(216, 24)
Me.CmdADD.Name = "CmdADD"
Me.CmdADD.TabIndex = 0
Me.CmdADD.Text = "ADD"
'
'CmdReset
'
Me.CmdReset.Location = New System.Drawing.Point(72, 56)
Me.CmdReset.Name = "CmdReset"
Me.CmdReset.TabIndex = 1
Me.CmdReset.Text = "RESET"
'
'CmdCalculate
'
Me.CmdCalculate.Location = New System.Drawing.Point(216, 88)
Me.CmdCalculate.Name = "CmdCalculate"
Me.CmdCalculate.Size = New System.Drawing.Size(80, 23)
Me.CmdCalculate.TabIndex = 2
Me.CmdCalculate.Text = "CALCULATE"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(32, 24)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(168, 20)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(32, 88)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(168, 20)
Me.TextBox2.TabIndex = 4
Me.TextBox2.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(376, 266)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox2, Me.TextBox1, Me.CmdCalculate, Me.CmdReset, Me.CmdADD})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
Dim myObj As New MeanCalculatorNet.MeanCalc2()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub CmdADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdADD.Click
myObj.AddInput(Val(TextBox1.Text))
End Sub

Private Sub CmdReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdReset.Click
myObj.Reset()
End Sub

Private Sub CmdCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdCalculate.Click
myObj.DoCalculation()
TextBox2.Text = myObj.GetOutput()
End Sub
End Class

No comments:

Post a Comment