?
Solved

Quick 500 - Creating an instance of a Class automatically by using imports?

Posted on 2005-03-09
3
Medium Priority
?
253 Views
Last Modified: 2010-04-23
Hi, i'm still a little new to .Net so this might be an impossible question,

I've been looking at the Microsoft Configuration Block recently, to use it you basically add the reference to your project and type in:

Imports Microsoft.Practices.EnterpriseLibrary.Configuration

Once you do this, you can instantly use the ConfigurationManager and other Namespaces without having to Create an instance of them...

i.e. Obj_ConfigManager = Microsoft.Practices.EnterpriseLibrary.Configuration.GetInstance

I was wondering if there is a way that i can also supply an 'Automatic' instance of a Class in this manner, i.e. at the moment, with the Structure that i define, i have to basically declare a new instance of it somewhere before i can start hacking at it's properties.

So for this example, could someone tell me by (in a new referenced project if possible) if i can automatically allow my main project to start using the following example class without having to go:

Obj = new <instance>

Here's a simple class with a single property

Public Class Configuration
    'Sets the Class Objects up as a Property to return
    Public Property Config_Database() As Configuration_Database
        Get
            Return CType(ConfigurationManager.GetConfiguration("DatabaseInfo"), Configuration_Database)
        End Get
        Set(ByVal Value As Configuration_Database)
            ConfigurationManager.WriteConfiguration("DatabaseInfo", Value)
        End Set
    End Property
End Class
0
Comment
Question by:Cloud9_User
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
Sourabhm earned 1000 total points
ID: 13494530

Use Shared

Public Class Configuration
    'Sets the Class Objects up as a Property to return
    Public Shared Property Config_Database() As Configuration_Database
        Get
            Return CType(ConfigurationManager.GetConfiguration("DatabaseInfo"), Configuration_Database)
        End Get
        Set(ByVal Value As Configuration_Database)
            ConfigurationManager.WriteConfiguration("DatabaseInfo", Value)
        End Set
    End Property
End Class

Now you can directly access

Configuration.Config_Database = <...............>
0
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 1000 total points
ID: 13494942
Well as suggested by Sourabhm
you can go for Shared Methods in a class
in that case if your class is

Public Class MyClass
    Public Shared Sub MySub()
    ....
    End Sub
 
    Public Shared Property ....

End Class


Then you can use it as
MyClass.MySub
and so on... Here we do not declare a variable for the class at all

However the example you have shown is different..
Obj_ConfigManager = Microsoft.Practices.EnterpriseLibrary.Configuration.GetInstance

Here the Method "GetInstance" is of the Nested Classes is Shared so that we can call it directly, Now this method returns an Instance of the class which we can assign to an object (here Obj_ConfigManager) and then use the instance methods of the class as well.

To get this type of functionality your class will have a Private Constructor so that an instance can not be created directly and the Shared method will return an Instance of the class.

0
 

Author Comment

by:Cloud9_User
ID: 13504024
Nice one guys!

Thats exactly what i needed!

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question