Results 1 to 9 of 9

Thread: How can i make this class work?

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Question

    So far i've created a project to try and learn how to use classes. I've just made it add two numbers. I have it check in the propery let statment if its a numer else set it to 1. When i type a number in it does not fire the code to check it. Here it is if you can help thanks

    'on class module

    Dim output As Object
    Private C_num1 As Integer
    Private C_num2 As Integer
    Dim thenumber1 As Integer
    Dim thenumber2 As Integer
    Public Property Get number() As Integer
    number1 = C_num1
    End Property

    Public Property Let number1(number1 As Integer)
    If IsNumeric(number) Then
    number1 = C_num1
    Else
    C_num1 = 1
    End If
    End Property
    Public Property Get number2() As Integer
    number2 = C_num2
    End Property

    Public Property Let number2(number2 As Integer)
    If IsNumeric(number2) Then
    number = C_num2
    Else
    C_num2 = 1
    End If
    End Property

    Property Set outputdev(indev As Object)
    Set output = indev
    End Property

    Public Sub additup(number1 As Integer, number2 As Integer)
    output = number1 + number2
    End Sub

    Private Sub Class_Initialize()
    C_num1 = 0
    C_num2 = 0
    End Sub

    'on the form
    Dim myclass As New Class1

    Private Sub Command1_Click()
    Set myclass.outputdev = lblanswer
    thenumber1 = txtnum1.Text
    thenumber2 = txtnum2.Text
    myclass.additup txtnum1, txtnum2
    End Sub
    Matt

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'I used your code for the class module
    'I changed the name of the class module to clsNumber
    'I changed the property of the class module from Project1 to Number
    'I saved the clsNumber as clsNumber and saved the project as Number
    '
    Option Explicit
    
    Dim output As Object
    Private C_num1 As Integer
    Private C_num2 As Integer
    Dim thenumber1 As Integer
    Dim thenumber2 As Integer
    Public Property Get number() As Integer
    number1 = C_num1
    End Property
    
    Public Property Let number1(number1 As Integer)
    If IsNumeric(number) Then
    number1 = C_num1
    Else
    C_num1 = 1
    End If
    End Property
    Public Property Get number2() As Integer
    number2 = C_num2
    End Property
    
    Public Property Let number2(number2 As Integer)
    If IsNumeric(number2) Then
    number = C_num2
    Else
    C_num2 = 1
    End If
    End Property
    
    Property Set outputdev(indev As Object)
    Set output = indev
    End Property
    
    Public Sub additup(number1 As Integer, number2 As Integer)
    output = number1 + number2
    End Sub
    
    Private Sub Class_Initialize()
    C_num1 = 0
    C_num2 = 0
    End Sub
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'I opened a new standard project
    'I went to Project/Add Class Module/ and added clsNumber
    'I changed your code a little here and there.
    'Presto...lblAnswer produces 26
    'All's well that ends well
    
    
    Option Explicit
    
    'on the form
    Dim clsnumber As New clsnumber
    
    
    Private Sub Command1_Click()
    
        Dim theNumber1 As String
        Dim theNumber2 As String
        txtNum1 = 12
        txtNum2 = 14
        
        Set clsnumber.outputdev = lblAnswer
        theNumber1 = txtNum1.Text
        theNumber2 = txtNum2.Text
        clsnumber.additup CInt(txtNum1), CInt(txtNum2)
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Guest
    Well, the main problem is that the first Property Get is

    Code:
    Public Property Get number() As Integer 
    number1 = C_num1 
    End Property
    instead of

    Code:
    Public Property Get number1() As Integer 
    number1 = C_num1 
    End Property
    which means you've forgot the 1 behind "Number".

    Basic syntax error.

  4. #4
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Here is the redone code:

    Code:
    'MyClass.cls
    
    Private output As Object      'Changed from Dim to Private
    Private C_num1 As Integer
    Private C_num2 As Integer
    Private thenumber1 As Integer 'Changed from Dim to Private
    Private thenumber2 As Integer 'Changed from Dim to Private
    
    
    'Set Your properties to variants as they're the only ones _
    that can take any variable type.
    'You also had a miss-naming error with number1, fixed it.
    Public Property Get number1() As Variant
       number1 = C_num1
    End Property
    
    'You were assinging C_num1 to Number1, this was backwards, _
    so I set it straight.
    Public Property Let number1(number1 As Variant)
       If IsNumeric(number1) Then
          C_num1 = number1        'Changed this around
          Else
          C_num1 = 1
       End If
    End Property
    
    'The same for these properties, set them to variant and _
    swapped the variables around so it functions properly.
    Public Property Get number2() As Variant
       number2 = C_num2
    End Property
    
    Public Property Let number2(number2 As Variant)
       If IsNumeric(number2) Then
          C_num2 = number2
          Else
          C_num2 = 1
       End If
    End Property
    
    Property Set outputdev(indev As Object)
       Set output = indev
    End Property
    
    Public Sub additup(number1 As Integer, number2 As Integer)
       output = number1 + number2
    End Sub
    
    Private Sub Class_Initialize()
       C_num1 = 0
       C_num2 = 0
    End Sub
    
    'MyForm1.frm
    Dim myclass As New Class1
    
    Private Sub Command1_Click()
       Set myclass.outputdev = lblanswer
    
       'Don't quite know where you got 'thenumber1' or 2, but _ 
       this is how it should be, referencing the numbers in _
       MyClass ...
       myclass.number1 = txtnum1.Text
       myclass.number2 = txtnum2.Text
    
       'set this up so that it was referencing the numbers in your class
       myclass.additup myclass.number1, myclass.number2
    End Sub
    I hope that helps you out.
    -Excalibur

  5. #5

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Smile

    I got it to work ok, but it seems now that if i try and enter a letter I get a type mismatch. I have the property let statements to check for that but they don't fire. What can i do to make it check? Thanks for all your help
    Matt

  6. #6
    Guest
    Code:
    Private output As object
    Varible OUTPUT is defined as OBJECT

    Code:
    output = number1 + number2
    Trying to use OUTPUT as a Integer/Variant.

    You misused OUTPUT there. You can either:
    Code:
    output.property = number1 + number2
    OR
    Code:
    Code:
    Private output as ...
    Private outputnum as Integer
    ...
    outputnum = number1 + number2

  7. #7
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    The code you see in my post takes care of that by accepting Variants. I tested the code before I posted it, give it a shot.
    -Excalibur

  8. #8

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    Your right excaliberzone it worked. I jus did not understand why at the moment. Thanks everyone
    Matt

  9. #9
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908

    Smile

    Glad to be of help.
    -Excalibur

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width