473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I use this Class I made?

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?

Jan 22 '07 #1
3 2034
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



"Ron" <pt*****@yahoo.comschreef in bericht
news:11**********************@m58g2000cwm.googlegr oups.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.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?

Jan 22 '07 #2
"Ron" <pt*****@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.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

Jan 22 '07 #3
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.EventArgs) 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.EventArgs )
)
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.Target = Me.lblFace

End Sub

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

Just something to ponder ...

HTH,
Phill W.
Jan 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Tim Henderson | last post by:
Hi i have some errors in my code that i can't find the reason for here is the error i have 3 classes: -Song -Album -Artist Song has 3 pieces of data with getter and setter meathods: -Name...
4
by: BH | last post by:
I'm looking at the source code of the ASP.NET forum sample application. It has the "code-behind" classes compiled into a separate DLL, totally separated from the aspx/ascx files. Adding the class...
4
by: Brian Shannon | last post by:
I am playing around with class libraries trying to understand how they work. I created a class library, library.vb. I placed the library.dll into the bin directory and set my reference. If I...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
1
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
6
by: Gaijinco | last post by:
I have always felt that there are a lot of topics that you learned the facts but you only grasp the matter sometime down the road. For me, two of those topics are inner classes and anonymous...
5
by: Amal P | last post by:
Dears, I have a question. class Data { }; class DisableDerive:virtual protected Data // A base class { private:
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
1
by: djss900 | last post by:
I've done some searching but haven't had much luck figuring this one out - hopefully someone here has some insight as to what I might be doing wrong. Assuming we have a regular C# driven asp.net...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.