473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructors in Singleton Class

Forgive my ignorance on this one as I am trying to use a Singleton class. I
need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class have
a number of paramterised constructors enabling me to pass in parameters or
not?

I am trying to use the following to send in a parmeter to a constructor, but
getting an error with it. I have a feeling that I am not able to do this. Is
there any other way that I can do this.

Dim sPatName As String = "Green"
Dim EMP As CEmPacc = CEmPacc.Create( sPatName)

Class myApp.CEmPacc cannot be indexed because it has no default property

Many thanks

Paul Bromley
Nov 21 '05 #1
14 3009
As usual, posting a question has made me think about a possible answer. I am
getting around the problem that I had below by accesing a property in my
class and passing in parameters that are then used in my property. I assume
that this is permissible? I know that this is a stupid question, but I also
assume that I can have a number of properties in my singleton class as I
would any other class - I have only seen very basic examples of Singleton
classes.

Thanks

Paul Bromley
"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:uB******** ******@TK2MSFTN GP10.phx.gbl...
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class have a number of paramterised constructors enabling me to pass in parameters or
not?

I am trying to use the following to send in a parmeter to a constructor, but getting an error with it. I have a feeling that I am not able to do this. Is there any other way that I can do this.

Dim sPatName As String = "Green"
Dim EMP As CEmPacc = CEmPacc.Create( sPatName)

Class myApp.CEmPacc cannot be indexed because it has no default property

Many thanks

Paul Bromley

Nov 21 '05 #2
Paul,

You can add properties to a singleton class just as you would to a regular
class.

--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:e6******** ******@TK2MSFTN GP14.phx.gbl...
As usual, posting a question has made me think about a possible answer. I
am
getting around the problem that I had below by accesing a property in my
class and passing in parameters that are then used in my property. I
assume
that this is permissible? I know that this is a stupid question, but I
also
assume that I can have a number of properties in my singleton class as I
would any other class - I have only seen very basic examples of Singleton
classes.

Thanks

Paul Bromley
"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:uB******** ******@TK2MSFTN GP10.phx.gbl...
Forgive my ignorance on this one as I am trying to use a Singleton class.

I
need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class

have
a number of paramterised constructors enabling me to pass in parameters
or
not?

I am trying to use the following to send in a parmeter to a constructor,

but
getting an error with it. I have a feeling that I am not able to do this.

Is
there any other way that I can do this.

Dim sPatName As String = "Green"
Dim EMP As CEmPacc = CEmPacc.Create( sPatName)

Class myApp.CEmPacc cannot be indexed because it has no default property

Many thanks

Paul Bromley


Nov 21 '05 #3
Thanks Mike.

Paul
"Mike McIntyre [MVP]" <mi****@getdotn etcode.com> wrote in message
news:On******** ******@TK2MSFTN GP15.phx.gbl...
Paul,

You can add properties to a singleton class just as you would to a regular
class.

--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:e6******** ******@TK2MSFTN GP14.phx.gbl...
As usual, posting a question has made me think about a possible answer. I am
getting around the problem that I had below by accesing a property in my
class and passing in parameters that are then used in my property. I
assume
that this is permissible? I know that this is a stupid question, but I
also
assume that I can have a number of properties in my singleton class as I
would any other class - I have only seen very basic examples of Singleton classes.

Thanks

Paul Bromley
"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:uB******** ******@TK2MSFTN GP10.phx.gbl...
Forgive my ignorance on this one as I am trying to use a Singleton class.
I
need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class

have
a number of paramterised constructors enabling me to pass in parameters
or
not?

I am trying to use the following to send in a parmeter to a
constructor, but
getting an error with it. I have a feeling that I am not able to do
this. Is
there any other way that I can do this.

Dim sPatName As String = "Green"
Dim EMP As CEmPacc = CEmPacc.Create( sPatName)

Class myApp.CEmPacc cannot be indexed because it has no default

property
Many thanks

Paul Bromley



Nov 21 '05 #4
The message you are getting is indicating that you are trying to pass an
argument where one is not expected. What is the "Create" method you have
there?
"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:uB******** ******@TK2MSFTN GP10.phx.gbl...
Forgive my ignorance on this one as I am trying to use a Singleton class.
I
need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class
have
a number of paramterised constructors enabling me to pass in parameters or
not?

I am trying to use the following to send in a parmeter to a constructor,
but
getting an error with it. I have a feeling that I am not able to do this.
Is
there any other way that I can do this.

Dim sPatName As String = "Green"
Dim EMP As CEmPacc = CEmPacc.Create( sPatName)

Class myApp.CEmPacc cannot be indexed because it has no default property

Many thanks

Paul Bromley

Nov 21 '05 #5
Singletons work just as any other class with the exception that you can't
inherit from them.

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:O4******** *****@TK2MSFTNG P14.phx.gbl...
Thanks Mike.

Paul
"Mike McIntyre [MVP]" <mi****@getdotn etcode.com> wrote in message
news:On******** ******@TK2MSFTN GP15.phx.gbl...
Paul,

You can add properties to a singleton class just as you would to a
regular
class.

--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:e6******** ******@TK2MSFTN GP14.phx.gbl...
> As usual, posting a question has made me think about a possible answer. I > am
> getting around the problem that I had below by accesing a property in
> my
> class and passing in parameters that are then used in my property. I
> assume
> that this is permissible? I know that this is a stupid question, but I
> also
> assume that I can have a number of properties in my singleton class as
> I
> would any other class - I have only seen very basic examples of Singleton > classes.
>
> Thanks
>
> Paul Bromley
>
>
> "Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
> news:uB******** ******@TK2MSFTN GP10.phx.gbl...
>> Forgive my ignorance on this one as I am trying to use a Singleton class. > I
>> need to use this to have one instance of my Class running and I think
>> I
>> understand how to do this. My question however is can a singleton
>> class
> have
>> a number of paramterised constructors enabling me to pass in
>> parameters
>> or
>> not?
>>
>> I am trying to use the following to send in a parmeter to a constructor, > but
>> getting an error with it. I have a feeling that I am not able to do this. > Is
>> there any other way that I can do this.
>>
>> Dim sPatName As String = "Green"
>> Dim EMP As CEmPacc = CEmPacc.Create( sPatName)
>>
>> Class myApp.CEmPacc cannot be indexed because it has no default property >>
>> Many thanks
>>
>> Paul Bromley
>>
>>
>
>



Nov 21 '05 #6
Paul,

"Paul Bromley" <fl*******@dsl. pipex.com> schrieb:
Forgive my ignorance on this one as I am trying to use a Singleton class.
I
need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class
have
a number of paramterised constructors enabling me to pass in parameters or
not?


Quick and dirty:

\\\
Public Class Singleton
Private m_UserName As String
Private m_Password As String

Private m_DefInstance As Singleton

Private Sub New()
'
End Sub

Private Sub New( _
ByVal UserName As String, _
ByVal Password As String _
)
m_UserName = UserName
m_Password = Password
End Sub

Public Function CreateInstance( ) As Singleton
If m_DefInstance Is Nothing Then
m_DefInstance = New Singleton
End If
Return m_DefInstance
End Function

Public Function CreateInstance( _
ByVal UserName As String, _
ByVal Password As String _
) As Singleton
If m_DefInstance Is Nothing Then
m_DefInstance = New Singleton(UserN ame, Password)
End If
Return m_DefInstance
End Function
End Class
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7
Errata:
Public Class Singleton


=> 'Public NotInheritable Class Singleton'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8
Many thanks Herfried, Scott & Mike,

I see exactly where I am going wrong now especially from the example that
Herfried has given - I was foolishly trying to send parameters to a Private
Sub New(), and I see quite obviously now that I need to send these to a
Public Function CreateInstance( ) As Singleton instead. I had not seen any
examples of using this type of class with parameters. Another stupid
question - I assume that I can place other initialisation code in this
function - i.e. calls to other Sub routines etc?

Again many thanks - as always I have learnt a lot - will try this later -
off to work now.

Best wishes

Paul Bromley


"Scott M." <s-***@nospam.nosp am> wrote in message
news:Og******** ******@tk2msftn gp13.phx.gbl...
The message you are getting is indicating that you are trying to pass an
argument where one is not expected. What is the "Create" method you have
there?
"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:uB******** ******@TK2MSFTN GP10.phx.gbl...
Forgive my ignorance on this one as I am trying to use a Singleton class. I
need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class
have
a number of paramterised constructors enabling me to pass in parameters or not?

I am trying to use the following to send in a parmeter to a constructor,
but
getting an error with it. I have a feeling that I am not able to do this. Is
there any other way that I can do this.

Dim sPatName As String = "Green"
Dim EMP As CEmPacc = CEmPacc.Create( sPatName)

Class myApp.CEmPacc cannot be indexed because it has no default property

Many thanks

Paul Bromley


Nov 21 '05 #9
Paul,

"Paul Bromley" <fl*******@dsl. pipex.com> schrieb:
I see exactly where I am going wrong now especially from the example that
Herfried has given - I was foolishly trying to send parameters to a
Private
Sub New(), and I see quite obviously now that I need to send these to a
Public Function CreateInstance( ) As Singleton instead. I had not seen any
examples of using this type of class with parameters. Another stupid
question - I assume that I can place other initialisation code in this
function - i.e. calls to other Sub routines etc?


Yes, you can do that.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #10

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

Similar topics

3
21377
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have an idea that we can have private constructors and destructors but am not able to find a situation where they can be used... Regards RVG...
4
2734
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... }; idiom (don't know the name of it, if there is one). The problem is that I don't want any of my classes to have public constructors. They should be...
2
1792
by: John Ratliff | last post by:
What are the ramifications of creating a private copy constructor? Say I didn't want any copies to be created, say if I had a singleton for example. Should I make the copy constructor public or private? I don't plan on defining it. Does it make a difference? If I make it private, what happens to the assignment operator? Does it try to...
7
1868
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain: - An instance of the class is created.
4
5034
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand that they are not mutually exclusive of one another. The above classification assimilates my knowledge of having used constructors in both the above...
12
2555
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in advance, Joe
12
8934
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
22
5180
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
2
1874
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define SINGLETON_HPP template<typename T> struct DefaultCreatorFunctor {
0
7411
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
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. ...
1
7439
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...
0
7773
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...
1
5343
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
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...
0
3468
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...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.