Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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

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
Cloud9_User
Asked:
Cloud9_User
2 Solutions
 
SourabhmCommented:

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
 
arif_eqbalCommented:
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
 
Cloud9_UserAuthor Commented:
Nice one guys!

Thats exactly what i needed!

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now