How do I use this Class I made?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron

    #1

    How do I use this Class I made?

    I am writing a very simple program, three buttons NORMAL, SMILE, FROWN.
    When the appropriate button is clicked a label either pops up a smile,
    frown or a normal face.

    I wrote a class to do this. Using the Windings font and the letts J K
    L you can get faces.

    How do I then use this class for my button click events?

    Here is my class:
    Public Class Faces
    Private s As String = "J"
    Private n As String = "K"
    Private f As String = "L"

    Public ReadOnly Property smile() As String
    Get
    Return s
    End Get

    End Property
    Public ReadOnly Property normal() As String
    Get
    Return n
    End Get
    End Property
    Public ReadOnly Property frown() As String
    Get
    Return f
    End Get
    End Property
    End Class

    NOW lets say I want to use that class on this frown button click event?
    How would the code look? I was thinking :
    Public Class Form1
    Private Sub btnFrown_Click( ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles btnFrown.Click

    lblface.text = faces.f

    End Sub

    BUT This does not work. Anyone can tell me how to get this to work?
    Do I need to do something before my class will work?

  • Michel Posseth [MCP]

    #2
    Re: How do I use this Class I made?

    You must first create an instance of a class before you can access it`s
    property`s or methods ( unless it is declared shared but that`s another
    chapter )

    so the syntax for your example would be



    Private Sub btnFrown_Click( ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles btnFrown.Click
    dim oFaces as new Faces
    lblface.text = ofaces.frown
    End Sub


    regards

    Michel Posseth







    "Ron" <pts4560@yahoo. comschreef in bericht
    news:1169493809 .373755.235910@ m58g2000cwm.goo glegroups.com.. .
    >I am writing a very simple program, three buttons NORMAL, SMILE, FROWN.
    When the appropriate button is clicked a label either pops up a smile,
    frown or a normal face.
    >
    I wrote a class to do this. Using the Windings font and the letts J K
    L you can get faces.
    >
    How do I then use this class for my button click events?
    >
    Here is my class:
    Public Class Faces
    Private s As String = "J"
    Private n As String = "K"
    Private f As String = "L"
    >
    Public ReadOnly Property smile() As String
    Get
    Return s
    End Get
    >
    End Property
    Public ReadOnly Property normal() As String
    Get
    Return n
    End Get
    End Property
    Public ReadOnly Property frown() As String
    Get
    Return f
    End Get
    End Property
    End Class
    >
    NOW lets say I want to use that class on this frown button click event?
    How would the code look? I was thinking :
    Public Class Form1
    Private Sub btnFrown_Click( ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles btnFrown.Click
    >
    lblface.text = faces.f
    >
    End Sub
    >
    BUT This does not work. Anyone can tell me how to get this to work?
    Do I need to do something before my class will work?
    >

    Comment

    • Armin Zingler

      #3
      Re: How do I use this Class I made?

      "Ron" <pts4560@yahoo. comschrieb
      I am writing a very simple program, three buttons NORMAL, SMILE,
      FROWN. When the appropriate button is clicked a label either pops up
      a smile, frown or a normal face.
      >
      I wrote a class to do this. Using the Windings font and the letts J
      K L you can get faces.
      >
      How do I then use this class for my button click events?
      >
      Here is my class:
      Public Class Faces
      Private s As String = "J"
      Private n As String = "K"
      Private f As String = "L"
      >
      Public ReadOnly Property smile() As String
      Get
      Return s
      End Get
      >
      End Property
      Public ReadOnly Property normal() As String
      Get
      Return n
      End Get
      End Property
      Public ReadOnly Property frown() As String
      Get
      Return f
      End Get
      End Property
      End Class
      >
      NOW lets say I want to use that class on this frown button click
      event? How would the code look? I was thinking :
      Public Class Form1
      Private Sub btnFrown_Click( ByVal sender As System.Object, ByVal e
      As System.EventArg s) Handles btnFrown.Click
      >
      lblface.text = faces.f
      Where did you declare a variable called "faces"? Did you create an instance
      of the class? The fields (variables s, n, f) are instance members, thus you
      need an instance.

      End Sub
      >
      BUT This does not work. Anyone can tell me how to get this to work?
      Do I need to do something before my class will work?

      There are several ways to do this. Shortest:

      Public Class Faces
      public const s As String = "J"
      public const n As String = "K"
      public const f As String = "L"
      End Class


      If you want to stay with properties, you can declare s,n,f and all
      properties as shared. (private shared..., public shared readonly...)


      Armin

      Comment

      • Phill W.

        #4
        Re: How do I use this Class I made?

        Ron wrote:
        NOW lets say I want to use that class on this frown button click event?
        How would the code look? I was thinking :
        Public Class Form1
        Private Sub btnFrown_Click( ByVal sender As System.Object, ByVal e
        As System.EventArg s) Handles btnFrown.Click
        >
        lblface.text = faces.f
        >
        End Sub
        You've created a Class called Faces. So far, so good.
        To use the methods it provides, you must create an Instance of that
        class (an Object of Type Faces), something like

        Dim f as New Faces
        lblFace.Text = f.frown ' public property, not private variable


        Alternatively, you could make your properties (and private variables)
        "Shared", in which case you /don't/ need to use an instance variable, as in

        Public Class Faces2
        Public Shared ReadOnly Property frown
        Get
        Return _f
        End Get
        End Property
        Private Shared _f As String = "L"
        End Class

        ....then ...

        lblFace.Text = Faces2.frown


        If you /really/ want to go the whole hog, (and this /may/ go just a
        /little bit/ further than your "very simple program" might want, but...)

        Instead of using a helper class (faces) to connect your buttons to the
        label and having to "wire-up" all the button events yourself (since all
        the "face" buttons work the same way), you could create your /own/
        Button class[es], something like this:

        Public Class FaceButton
        Inherits Button

        Public Sub New()
        MyBase.New()
        End Sub

        Public Property Caption() as String
        Get
        Return _caption
        End Get
        Set( value as String)
        _caption = value
        End Set
        End Property

        Public Property Target() as Label
        Get
        Return _target
        End Get
        Set( value as Label)
        _target = value
        End Set
        End Property

        Protected Overrides Sub OnClick( _
        ByVal e as System.EventArg s )
        )
        If Not ( _target Is Nothing ) Then
        _target.Text = _caption
        End if

        MyBase.OnClick( e)

        End Sub

        Private _target As Label
        Private _caption As String = "L"

        End Class

        Class SmileyButton
        Inherits FaceButton

        Public Sub New()
        MyBase.New()

        Me.Caption = "J"
        End Sub

        End Class

        Replace the standard buttons with the above class (edit the code VB
        tells you never to edit) and initialise them:

        [Designer Code]
        Private WithEvents smiley As SmileyButton
        ....
        smiley = New SmileyButton
        ....

        Sub Form_Load( ...

        Me.smiley.Targe t = Me.lblFace

        End Sub

        Now, whenever you click on the smiley button, you update the target label.

        Just something to ponder ...

        HTH,
        Phill W.

        Comment

        Working...