473,397 Members | 2,056 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,397 software developers and data experts.

Class. Need advice.

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

Oct 4 '06 #1
3 1611
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

shapper wrote:
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
Oct 4 '06 #2
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.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
"shapper" <md*****@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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

Oct 4 '06 #3
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


"shapper" <md*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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

shapper wrote:
>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

Oct 4 '06 #4

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

Similar topics

8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
3
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an...
4
by: Peter Speybrouck | last post by:
I have a little problem with a webservice. I reproduced the problem in the following simplified example. I just create a new C# ASP.NET webservice and a c# console application. I added a new...
5
by: Jeff Cobelli | last post by:
I am trying to include two classes as members of another class in a webservice. The definitions look basically like this: Public Class clsLender Public ID As String Public Name As String End...
6
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
2
by: Fish | last post by:
I have been researching the correct way to organize my solution so that it makes best use of VB.NET inherent ability to manage resources such as objects. My solution contains 2 projects and the...
7
by: Chris Jewell | last post by:
Hi, I'm wondering what the best way of registering a data storage class with a data handler class is. At the moment I have two classes: class EpiCovars // Storage class { ....
9
by: needin4mation | last post by:
I have a .aspx file and it's src. I also have a third file, a class that the src references. Everything is in the same directory/folder. Everything has the same namespace. How do I reference the...
4
by: Joseph Geretz | last post by:
We use a Soap Header to pass a token class (m_Token) back and forth with authenticated session information. Given the following implementation for our Logout method, I vastly prefer to simply code...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.