|
-
October 3rd, 2001, 01:02 AM
#1
can some show me how to add 2 numbers using classes
well the subject says it all doesnt it 
im learning classes and just cant get the grasp of it ive got examples, but most of them are complicated as hell, if someone can just show me how to add 2 numbers with let and get statements i will happy 
thanx alot
--==|3Lad3==--
-
October 3rd, 2001, 01:55 AM
#2
Re: can some show me how to add 2 numbers using classes
'A standard Exe, one Class module
'(with default names)
'Form1 code
'two textboxes and a commandbutton
option Explicit
private myTotalClass as Class1
private Sub Command1_Click()
With myTotalClass
.FirstNumber = Text1.Text 'you do not need to check
'here for numeric values: calss will do it
.SecondNumber = Text2.Text
MsgBox "total= " & .Total
End With
End Sub
private Sub Form_Load()
set myTotalClass = new Class1
End Sub
private Sub Form_Unload(Cancel as Integer)
set myTotalClass = nothing
End Sub
'----------------------------------------------------
'Class1 code
option Explicit
private m_FirstNumber as Long
private m_SecondNumber as Long
public property get FirstNumber() as Variant
'if asked, say to client (=the form) this is first number value
FirstNumber = m_FirstNumber
End property
public property let FirstNumber(byval vNewValue as Variant)
'when form try to set this value, check if it is acceptable
If IsNumeric(vNewValue) then
'you may want to test also for an overflow...
m_FirstNumber = vNewValue
else
'set a default value if non numeric entry
m_FirstNumber = 0
End If
End property
public property get SecondNumber() as Variant
'if asked, say to client (=the form) this is second number value)
SecondNumber = m_SecondNumber
End property
public property let SecondNumber(byval vNewValue as Variant)
If IsNumeric(vNewValue) then
'you may want to test also for an overflow...
m_SecondNumber = vNewValue
else
m_SecondNumber = 0
End If
End property
public property get Total() as Variant
'readonly property
Total = m_FirstNumber + m_SecondNumber
End property
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
October 3rd, 2001, 02:35 AM
#3
Re: can some show me how to add 2 numbers using classes
Nice, illustrates the use of properties, but why are you using variants?
Another way to do it is like this, without the properties, which in this case makes a nicer code:
' *****************
' This is the class
' *****************
public Function Add(A as Double, B as Double) as Double
Add = A + B
End Function
'
' ****************
' This is the Form
' ****************
private Sub Command1_Click()
Dim myClass as new Class1
' Should show 3 
Msgbox myClass.Add(1 , 2)
End Sub
Tom Cannaerts
[email protected]
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
-
October 3rd, 2001, 03:00 AM
#4
Re:sure nicer...
Sure, but request was to do it with properties...
;-)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
October 3rd, 2001, 03:01 AM
#5
Re: can some show me how to add 2 numbers using classes
Open a standard exe project & add a class in to it
In the class:
Define 2 private variables to hold the data (the 2 numbers to be input)
Write 2 public "let properties" to read data. (u can live with these only or u can define 2 private or public "get properties" to get the values). The values read r to be stored in the 2 private variables.
Write a Function (a method) to add two numbers. The functionality may be adding the 2 private variables and returning the added value.
put a command button to the form.
In the click event , write the code to instantiate the class
Using the instance's properties give the 2 numbers u want to add (u can make this more generalised by putting 2 text boxes & getting the value of the numbers put in to those & assigning to the properties.)
Then call the Adding Method of the class in a Msgbox function
See the result.
If working correctly, remove the class from the project. Open a ActiveX DLL project & get that class added in to it. Compile.
From the earlier project reference it. Then run the project.
That's it
If anything is unclear pls let me know
Srinika
If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it 
-
October 3rd, 2001, 03:02 AM
#6
Re: About variants
>but why are you using variants?
To show validation code in class:
you can pass what you want, Class will take care of its data.
;-)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
October 3rd, 2001, 03:21 AM
#7
Re:sure nicer...
Oops, my mistake, didn't see the Get and Let part
Tom Cannaerts
[email protected]
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
-
October 3rd, 2001, 07:18 AM
#8
Re:no matter, friend...
...as long as you rate me...
;-)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|