Using a class in VB.NET

P

Paul Bromley

I know this may seem a stupid question, but would value a reply. I write
certain path settings to an ini file and use a class to read these settings
into at startup of a project. Throughout the use of my application from a
form I will reference these paths on many occasions. Is it best practice to
create a new instance of this class each time that I need to acces a path,
or is it better to create one instance of the class for the life of my
form??

Many thanks.

Paul Bromley
 
H

Herfried K. Wagner [MVP]

Paul Bromley said:
I know this may seem a stupid question, but would value a reply. I write
certain path settings to an ini file and use a class to read these settings
into at startup of a project. Throughout the use of my application from a
form I will reference these paths on many occasions. Is it best practice to
create a new instance of this class each time that I need to acces a path,
or is it better to create one instance of the class for the life of my
form??


Either declare the members of the class as 'Shared', use a module instead of
a class, or implement the Singleton design pattern (Google is your
friend...).
 
M

Matthew.Gertz

If I understand correctly, each time that you instantiate the class, you're going to read in that .INI file. Therefore, I would personally just keep one instance of the class around to avoid all of the disk hits. Disk hits are at the top of the list in the realm of "performance issues to worry about." You'll also be saving memory and avoiding allocation costs, and furthermore, you're guaranteed to be consistent with regard to the paths -- imagine somehow changing the INI file underneath you -- you might end up with components referencing different paths.

--Matt Gertz--*
VB Compiler Dev Lead

-----Original Message-----
From: Paul Bromley
Posted At: Thursday, November 17, 2005 11:11 AM
Posted To: microsoft.public.dotnet.languages.vb
Conversation: Using a class in VB.NET
Subject: Using a class in VB.NET


I know this may seem a stupid question, but would value a reply. I write
certain path settings to an ini file and use a class to read these settings
into at startup of a project. Throughout the use of my application from a
form I will reference these paths on many occasions. Is it best practice to
create a new instance of this class each time that I need to acces a path,
or is it better to create one instance of the class for the life of my
form??

Many thanks.

Paul Bromley
 
T

tomb

I would even go one more step - save the paths in variables that belong
to the form. No class to deal with after the first read of the file,
and the string variables are faster to access than a class.

T
 

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