473,661 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.S ayMessage()

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.M essage

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

Thanks,
Miguel

Oct 4 '06 #1
3 1619
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.S ayMessage()

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.M essage

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(ByVa l 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.AddNumb ers(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.goo glegroups.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.S ayMessage()

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.M essage

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.goo glegroups.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.S ayMessage()

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.M essage

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
2411
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. Generally, the documentation suggests that queues or similar constructs should be used for thread inter-process comms. I've had a lot of success in doing that (generally by passing in the queue during the __init__ of the thread) and I can see...
3
1811
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 abstract parent with four basic functions: Open, Read, Write, and Close. // Note alot of the stuff has been left out // just to give a basic picture class IO { public:
4
2412
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 class test to the namespace of the webservice which I try to access from the console application. //Service1.asmx.cs
5
1746
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 Class ---------------------------------------------------------------------------------------- Namespace DataTypesVB.Enumerations Public Class DefaultValues
6
1436
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 really see an advantage of a Class over a module containing the same functions, but now that this Class is working for me, I have found a possible use. Since this class will hold all the values used in a report I am building, I think I will now...
2
1622
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 main problem is that the mem usage continues to grow until the Service stops responding. I have received advice to: "create those objects at a class level; instantiate them when the service starts, and dispose of them when the service ends. Then...
7
1610
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
1650
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 class in my c# .src file? I don't believe this is an ASP.NET question, but a OOP C# question, thus my post here. I have searched and I thought it was with: using FilterSQL; //the name of my class using reports.FilterSQL; //name of...
4
2477
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 m_Token = null in order to destroy the session token when the user logs out. However, I'm finding that setting class instance to null results in no header being sent back to the client, with the result that the client actually remains with an...
0
8856
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.