How do I fix this class?


R

Ron

I'm making this dog class and I am getting confused on properties and
methods and what they are.

How would I fix this program?
Public Class Form1
Dim mydog As New dog 'This needs to be declared at Form level
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCreate.Click
mydog.Name = txtname.Text
mydog.age = txtage.Text
mydog.weight = txtweight.Text
mydog.breed = txtbreed.Text
mydog.temp = txttemp.Text
mydog.color = txtcolor.Text
'etc, etc
End Sub
Private Sub btnSleep_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSleep.Click


'Sleep should be a method, ie Sub,not a Property (what does
this mean)?
sleepmode()
End Sub

Private Sub btnWakeUp_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnWakeUp.Click
'Dim mydog As New dog

mydog.wakeup() 'wakeup should be a method, ie Sub,not a
Property
sleepmode()
End Sub
Private Sub sleepmode()
'Dim mydog As New dog

If mydog.awake Then
btnWakeUp.Enabled = False
btnSleep.Enabled = True
lblresult.Text = output & " Your new dog is awake" 'What
is output? Where did you declare it?
Else
btnWakeUp.Enabled = True
btnSleep.Enabled = False
lblresult.Text = output & " Your new dog is asleep" 'What
is output? Where did you declare it?
End If
End Sub

Private Sub btnBark_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnBark.Click
Dim mydog As New dog

mydog.bark()
End Sub

End Class

and the dog class:
Public Class dog
'Public name As String = txtname.text 'These textboxes are on the
form
Private mname As String 'Data memebers are Private, only get
exposed via properties
Public breed As String 'the class knows nothing about them
Public temp As String
Public color As String
Public weight As Int16
Public age As Int16
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property
Public ReadOnly Property sleep() As Boolean
Get

End Get
End Property

Public ReadOnly Property awake() As Boolean
Get

End Get
End Property
'Public ReadOnly Property bark() As Boolean
Public Function bark() As String
Dim retVal As String = "Woof Woof"
Return retVal
End Function
End Class
 
Ad

Advertisements

Z

zacks

I get these errors:

Wakeup is not a member of...
name output is not declared
name output is not declared

Looks like the answers are in the comments in the code.
 
G

Guest

Ron said:
I get these errors:

Wakeup is not a member of...
name output is not declared
name output is not declared

Your Dog class does not have a method called Wakeup(). Therefore, Wakeup is
not a member of...

In the sleepmode() sub of Form1 you referance output, "lblresult.Text =
output & " Your new...", but there is no local variable named output and
Form1 has no field or property named output. Therefore, name output is not
declared.

Methods (Subs and Functions) are actions your class performs.

Properties are data. They are similar to Fields, however within the Get/Set
you can perform some action, typically data validation.
 
Z

zacks

I guess I'm not understanding how to mak wakeup a member of the class

Add a new Sub of Function to the class whose name is wakeup. If the
method needs to return a value, it needs to be function, otherwise it
needs to be a sub.

Public Sub Wakeup()

' do something

End Sub
 
Ad

Advertisements

C

Cor Ligthert [MVP]

Ron,

If you make objects using classes, why dont you than not use binding, either
by the designer either direct by code?

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Cor said:
Ron,

If you make objects using classes, why dont you than not use binding, either
by the designer either direct by code?

Cor

You really should try to write simpler sentences. Even I don't
understand a thing of what you are trying to say.
 
C

Cor Ligthert [MVP]

Goran,

I don't understand what you are writing: What do you mean with *Even I*.
What is the distinct between you and other people?

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Cor said:
Goran,

I don't understand what you are writing: What do you mean with *Even I*.
What is the distinct between you and other people?

Cor

Göran Andersson said:
You really should try to write simpler sentences. Even I don't understand
a thing of what you are trying to say.

Yes, you obviously do understand what I am writing, you just want to
question it. I, on the other hand, truly did not understand what you
were writing in your previous post. All the words were familiar, but
they did not form a sentence that made sense.

The difference between me and the "other people", as you put it, is that
the "other people" are the ones that you directed your message at, and
they are beginners at programming. I have several years of experience
developing .NET applications, and many more in programming in general,
but not even that knowledge enabled me to understand what you were
trying to say.
 
Ad

Advertisements

C

Cor Ligthert [MVP]

Goran

Maybe you understand it seeing this. It is only a small part.

\\\
Public Class Form1
Private mydog As New dog("Goran", 15)
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
txtName.DataBindings.Add("Text", mydog, "Name")
txtAge.DataBindings.Add("Text", mydog, "Age")
End Sub
End Class
///

\\\
Public Class dog
Private mName As String
Private mAge As Integer
Public Sub New(ByVal name As String, ByVal age As Integer)
mName = name
mAge = age
End Sub
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property
Public Property Age() As Integer
Get
Return mAge
End Get
Set(ByVal value As Integer)
mAge = value
End Set
End Property
End Class
///




 
Ad

Advertisements


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