Home > Software engineering >  For vba shameless!!!!!
For vba shameless!!!!!

Time:10-25

I write my own period of vba code, as follows:
Class:
Option Explicit
Private real_num As Double
'the real component
Private imaginary_num As Double
'imaginary part
Public Property Get real () As a Double
Real=real_num
End Property

Public Property Let real (ByVal re As Double)
Real_num=re
End Property

Public Property Get imaginary () As a Double
Imaginary=imaginary_num
End Property

Public Property Let imaginary (ByVal imag As Double)
Imaginary_num=imag
End Property

As Complex_numble Public Sub sum (a, b As Complex_numble)
Real=(+ b.r a.r eal eal)
Imaginary=(Anderson maginary + b.i maginary)
End Sub
Plural addition
'
As Complex_numble Public Sub subtract (a, b As Complex_numble)
Real=(a.r eal - b.r eal)
Imaginary=(Anderson maginary - b.i maginary)
End Sub
Plural subtraction
'
As Complex_numble Public Sub multiply (a, b As Complex_numble)
Real=(* b.r a.r eal eal - Anderson, maginary * b.i maginary)
Imaginary=(a.r eal * b.i maginary + Anderson b.r maginary * eal)
End Sub
'complex multiplication

As Complex_numble Public Sub divide (a, b As Complex_numble)
Real=((* b.r a.r eal eal + Anderson maginary * b.i maginary)/(* b.r b.r eal eal + b.i maginary * b.i maginary))
Imaginary=((b.r eal * Anderson maginary - a.r eal * b.i maginary)/(* b.r b.r eal eal + b.i maginary * b.i maginary))
End Sub
'
plural division
Public Sub sin (a As Complex_numble)
Sin. Real=((Math. Sin (a.r eal) * (Math. J Exp (Anderson maginary) - Math. J Exp (- Anderson, maginary)))/2)
Sin. The imaginary=((Math. Cos (a.r eal) * (Math. J Exp (Anderson maginary) - math.h Exp (- Anderson, maginary)))/2)
End Sub
Sine
'Public Sub cos (a As Complex_numble)
Real=((Math. Cos (a.r eal) * (Math. J Exp (Anderson maginary) + math.h Exp (- Anderson, maginary)))/2)
Imaginary=((Math. Sin (a.r eal) * (Math. J Exp (- Anderson, maginary) - Math. J Exp (Anderson maginary)))/2)
End Sub
'the cosine
Private Sub Class_Initialize ()
Real=1
Imaginary=1
End Sub
Module:
The Attribute VB_Name="module 2"
Public Sub text1 ()
Dim As a New Complex_numble
Dim b As New Complex_numble
A.r eal=CDbl (Range (" a2 "))
Anderson maginary=CDbl (Range (" b2 "))
B.r eal=CDbl (Range (" a3 "))
B.i maginary=CDbl (Range (" b3 "))
b.s in (a)
Range (" c2 ")=b.r eal
Range (" d2 ")=b.i maginary
End Sub
Where the standard red run not bottom go to, please advice where is wrong

CodePudding user response:

CodePudding user response:

To change to: Call b.s in (a)
Or: b.s in a
(that is, remove the parenthesis)

In addition, your Public Sub sin (a As Complex_numble), the two sine. Have to take out,

CodePudding user response:

Thank you for your bright moon, problem has been solved!

CodePudding user response:

Asking if I want to have all the mathematics methods in the class to function function, how to realize the external call?

CodePudding user response:

This is very simple ah,
If change to function calls, this is not necessary to use class module,
Put all your Sub just moved to a standard module,
And then you reference this generation to do:
 Option Explicit 

'
define complex data typeThe Public Type Complex_numble
'if you want to use in other modules of this type, don't use Private!
'in the standard module code, the Public is the default, can not write,
Real As Double
Imaginary As Double
End Type

'here are some of the realization of the "operation" function...

Plural addition
'As Complex_numble Public Function sum (a, b As Complex_numble) As Complex_numble
Sum. Real=+ b.r a.r eal eal
Sum. The imaginary=Anderson maginary + b.i maginary
End the Function

'* * * * * the other functions, according to you * * * * *
'note: if the "arithmetic" two complex number are needed to do, that you of function parameters, there must be two


Give you a call example:
 Private Sub Test () 
Dim a As Complex_numble
Dim As Complex_numble b
Dim As Complex_numble c

A.r eal=5
Anderson maginary=20
B.r eal=16
B.i maginary=30
-C=sum (a, b)
The Debug. Print c.r eal, c.i maginary
'output:
'21-10
End Sub

CodePudding user response:

Code written to standard module, that a few Public Property Get/Let, don't directly,

CodePudding user response:

Ha ha, I'm complicated
  •  Tags:  
  • VBA
  • Related