creating a simple class, a cat class?

R

Ron

I want to learn about classes so I would like to know how to create a
simple one say a cat class with sleep, eat, meow properties.

how would I make a class that i could later use proterties of for
example

mycat.eat = true
or mycat.meow=false

is this even how you would do it?

Can anyone provide an example?....or any class in general...a dog, a
fish, anything just want to get the idea of classes.

thanks
 
R

Rory Becker

ok try this....
-------------------------------------------------------------
Public Mustinherit Class LeggedAnimal

Private mLegCount as Integer

Public Readonly Property LegCount as Integer
Get
return mLegCount
End Get

Public Sub New(LegCount as Integer)
if LegCount < 1 then
Throw new ArgumentException("Legged Animal nust have more than 0 legs")
End if
mLegCount = LegCount
End Sub

Public Sub Walk
If mLegCount > 1 then
MsgBox("Animal Walks")
End if
End Sub

End Class

Public Class Dog
Inherits LeggedAnimal

Public Sub WagTail
Msgbox("Dog Wags tail")
End Sub

End Class

Public Module Main
Public sub Main()
Dim Rover as new Dog
Rover.Walk
Rover.Wagtail
End sub
End Module

-------------------------------------------------------------


This proves that the dog can both wag it's tail and use the inherited behaviour
of walking.

Rover is a specific "instance" of the "class" dog.

You can say that "Rover is a Dog" and further that "Rover is an LeggedAnimal"

The Subs (WagTail and Walk) are what we can methods and they are what we
use to do things.
You might think of them as Actions

mLegCount is a Private field. It can only be seen by code that is part of
LeggedAnimal (Not by code that is part of Dog)

LegCount is the public face of mLegCount. It is a readonly property. The
compiler would error if we tried to set it.

We can also have writable and read/writable properties. the main purpose
of a property is to allow us to perform various checks and balances when
either the get or set (TRead and Write aspects of the property) are accessed.

This will act as a brief first example.

Some will find this information too much, others too little.

Please feel free to ask if you have any further questions
 
R

Ron

ok try this....
-------------------------------------------------------------
Public Mustinherit Class LeggedAnimal

Private mLegCount as Integer

Public Readonly Property LegCount as Integer
Get
return mLegCount
End Get

Public Sub New(LegCount as Integer)
if LegCount < 1 then
Throw new ArgumentException("Legged Animal nust have more than 0 legs")
End if
mLegCount = LegCount
End Sub

Public Sub Walk
If mLegCount > 1 then
MsgBox("Animal Walks")
End if
End Sub

End Class

Public Class Dog
Inherits LeggedAnimal

Public Sub WagTail
Msgbox("Dog Wags tail")
End Sub

End Class

Public Module Main
Public sub Main()
Dim Rover as new Dog
Rover.Walk
Rover.Wagtail
End sub
End Module

-------------------------------------------------------------

This proves that the dog can both wag it's tail and use the inherited behaviour
of walking.

Rover is a specific "instance" of the "class" dog.

You can say that "Rover is a Dog" and further that "Rover is an LeggedAnimal"

The Subs (WagTail and Walk) are what we can methods and they are what we
use to do things.
You might think of them as Actions

mLegCount is a Private field. It can only be seen by code that is part of
LeggedAnimal (Not by code that is part of Dog)

LegCount is the public face of mLegCount. It is a readonly property. The
compiler would error if we tried to set it.

We can also have writable and read/writable properties. the main purpose
of a property is to allow us to perform various checks and balances when
either the get or set (TRead and Write aspects of the property) are accessed.

This will act as a brief first example.

Some will find this information too much, others too little.

Please feel free to ask if you have any further questions

OK so If I were making a dog class and I wanted it to have these
properties:(these are user definable through textbox)
Breed
Color
Weight
and these methods:(these are not user definable)
Bark
Sleep
Wakeup

how would I put this into a class

public class mydog
public property
set Bark

public property
set sleep
would it look something like that?

So then if I want to put in a name for breed and a wieght and then I
want to display that in a lable I would just put
mylabel.text ="Your dog, a " & mydog.breed & " wieghs " & mydog.Weight
& "pounds"

Is this right or am I missing the point.
 
R

Rory Becker

OK so If I were making a dog class and I wanted it to have these
properties:(these are user definable through textbox)
Breed
Color
Weight
and these methods:(these are not user definable)
Bark
Sleep
Wakeup
how would I put this into a class


Well your properties would likely be read and write capable. for example...
-------------------------------------------------------------
Private mBreed as String ' Note you could use an Enum here but I'm using
a string for simplicity
Public Property Breed as String
Get
return mBreed
End Get
Set(Value as String)
mBreed = Value
End Set
End Property
-------------------------------------------------------------

So then if I want to put in a name for breed and a wieght and then I
want to display that in a lable I would just put
mylabel.text ="Your dog, a " & mydog.breed & " wieghs " & mydog.Weight
& "pounds"
Is this right or am I missing the point.

In order to say
-------------------------------------------------------------
mylabel.text ="Your dog, a " & mydog.breed & " wieghs " & mydog.Weight &
"pounds"
-------------------------------------------------------------
....which is correct syntax in vb.

You would need to have instantiated (created) 'mydog' and set the properties
appropriately...
-------------------------------------------------------------
Dim mydog as New Dog
mydog.breed = "SomeValue"
mydog.Weight = 45 ' 45 what I don't know :)
-------------------------------------------------------------
public class mydog
public property
set Bark
public property
set sleep
would it look something like that?

Not sure exactly what you're trying to do here, but 'call' is the (optional)
keyword to proceed a call to a sub (method with no return value)

The methods for 'Sleep' etc would be similar, in structure at least, to 'Walk'
and 'WagTail' inmy previous example.

Does this make things any clearer?
 
R

Ron

Well your properties would likely be read and write capable. for example...
-------------------------------------------------------------
Private mBreed as String ' Note you could use an Enum here but I'm using
a string for simplicity
Public Property Breed as String
Get
return mBreed
End Get
Set(Value as String)
mBreed = Value
End Set
End Property
-------------------------------------------------------------


In order to say
-------------------------------------------------------------
mylabel.text ="Your dog, a " & mydog.breed & " wieghs " & mydog.Weight &
"pounds"
-------------------------------------------------------------
...which is correct syntax in vb.

You would need to have instantiated (created) 'mydog' and set the properties
appropriately...
-------------------------------------------------------------
Dim mydog as New Dog
mydog.breed = "SomeValue"
mydog.Weight = 45 ' 45 what I don't know :)
-------------------------------------------------------------


Not sure exactly what you're trying to do here, but 'call' is the (optional)
keyword to proceed a call to a sub (method with no return value)

The methods for 'Sleep' etc would be similar, in structure at least, to 'Walk'
and 'WagTail' inmy previous example.

Does this make things any clearer?

I'm still not understanding how to make the class and set the
properties.
Here are the buttons and the buttons coded for the mydog class that I
would like to make.

Private Sub btnSleep_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSleep.Click
mydog.sleep()
sleepmode()
End Sub

Private Sub btnWakeUp_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnWakeUp.Click
mydog.wakeup()
sleepmode()
End Sub
Private Sub sleepmode()
If mydog.awake Then
btnWakeUp.Enabled = False
btnSleep.Enabled = True
lbloutputs.Text = output & " Your new dog is awake"
Else
btnWakeUp.Enabled = True
btnSleep.Enabled = False
lbloutputs.Text = output & " Your new dog is asleep"
End If
End Sub

I am having no luck figuring out how to write a class for this. I
guess I am not understanding the concepts of the class.
 
T

Tom Leylan

Ron: I understand your desire to start in just writing stuff but I'm going
to make the suggestion that you start by reading a book on the "principles"
of OOP and not specifically on VB.Net. A language is only an implementation
of the concept, it is the concept you need to understand.

Then proceed to examples in VB.Net so you add "syntax" to your basic
understanding. Resist however the inclination to immediately start with a
UI as forms, buttons, events and such isn't OOP or VB.Net. If you need to
learn databinding simultanously with what constitutes a Cat class then by
all means jump right in but you will be confused as the OOP, the VB.Net and
the UI interactions all jumble together to form a mountain of confusion.

There are free books on OOP available on the Internet.
 

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