How do I use this Class I made?

R

Ron

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.EventArgs) 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?
 
M

Michel Posseth [MCP]

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.EventArgs) Handles btnFrown.Click
dim oFaces as new Faces
lblface.text = ofaces.frown
End Sub


regards

Michel Posseth
 
A

Armin Zingler

Ron said:
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.EventArgs) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top