Class. Need advice.

S

shapper

Hello,

I created a simple class as follows:

Public Class HelloWorld

Public Function SayMessage() As String
Return "Hello World!"
End Function

End Class

Usage:

Dim objHelloWorld As New HelloWorld()
MyLabel.Text = objHelloWorld.SayMessage()

Then I recreated the class using a property:

Public Class HelloWorld

Public ReadOnly Property Message() As String
Get
Return SayMessage()
End Get
End Property

Public Function SayMessage() As String
Return "Hello World!"
End Function

End Class

Usage:

Dim objHelloWorld As New HelloWorld()
MyLabel.Text = objHelloWorld.Message

Which appoach should I use or when to use which one?
Could you advice me on this?

Thanks,
Miguel
 
S

shapper

My question has a reason.

I have a class with a function named Get_OneRecord.

The function Get_OneRecord gets ONLY ONE record from a database using a
Data Reader.

The record has 4 fields. So this function returns a string collection
with the four fields.

Should I use 4 properties as follows:

Public ReadOnly Property Name() As String
Get
Return Get_OneRecord{1}
End Get
End Property

Public ReadOnly Property Address() As String
Get
Return Get_OneRecord{2}
End Get
End Property

Which will return the first field of the collection or array:
MyLabel.Text = MyClass.Name ...

Or should I use the function and access each string collection item
outside: MyLabel.Text = Get_OneRecord{1}

Do you understand my problem?

Thanks,

Miguel
 
C

Cowboy \(Gregory A. Beamer\)

Is this function something that gets called over and over again with
slightly different phrasings? If so, it could be implemented as a static
(Shared keyword in VB.NET) method. This enters memory once and you never
instantiate the class. As an example, suppose we have an add method (yes, I
know this is not data driven):

'I realize this can overflow, it is just an example
Public Shared Function AddNumbers(ByVal a as integer, ByVal b As Integer) As
Integer
Return a + b
End Function

Now, I can call the class like so:

Dim c as Integer = MathLib.AddNumbers(a,b)

this is essentially a helper method.

Now, to the properties. Does a single row have meaning to your system, or
are you merely using it to help answer questions for other true objects? If
it has meaning, you should create properties and fill the object as an
object, as it IS an object.

For the most part, data rows represent an object, tangible or intangible. A
possible exception is enumeration type tables, but you generally do not grab
a single row at a time in these tables.
 
M

Mark Fitzpatrick

It would be better to have a series of private local variables and then use
the property to access them, so instead of returning GetOneRecord{1}, you
would return the local variable named name. You can populate the local
variables right after you perform the query. The reason is, datareaders
maintain a constant connection to the database. That means you want to open,
read, close, dispose in as short a time as possible. Also, if you access the
reader right from the properties, there is no way to ensure that your
connection has been closed properly and the other related items, such as the
command object, have been disposed of. Accessing the reader directly also
doesn't offer the chance for ensuring that no errors occur. What would
happen if suddenly the record returned no results. If you accessed it
directly from the property it would throw an error as the field is of type
DbNull, not string. Easier to do this in a spot where you can test to ensure
you ahve results before setting variables.


--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top