Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't create a new object? 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
I used Visual Studio to create a new class for me.

It produced this..
Code:
namespace CDC_Test1
{
	/// <summary>
	/// Database User
	/// 
	/// </summary>
	public sealed class User
	{
		private static User instance = new User();
		
		public static User Instance {
			get {
				return instance;
			}
		}
		
		private User()
		{

		}
	}
}

I've then tried to instantiate an instance of User but I just get this error...
Error 1 'CDC_Test1.User.User()' is inaccessible due to its protection level C:\C#\CDC_Test1\CDC_Test1\Program.cs 24 38 CDC_Test1

What has VS created and why can't I use it?

I'm assuming the method User is the constructor where I would initialise attributes etc.. and below that would start adding my own public/private methods.

I don't get it?

Thanks,
1DMF


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
I think my error was I chose singleton via VS which I assume creates an instance of the object when the application is ran.

Which is auto creating the new 'User' object and assigning it to the attribute 'instance' which is accessible via the getter 'Instance'.



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Yes, but the constructor is private. What are you trying to do? It looks you want this class to be limited to a single-instance object later when using it, that's when you use a singleton.

You can't create an instance of this class by new, you use the static property instance instead, so instead of User theUser = new User() you create the user instance by User theUser = User.Instance;

see
Bye, Olaf.
 
I just wanted to create a User object, the application will only ever have one User object, it is an application global, so assumed I wanted a singleton, but the construct is alien to me as is C#

If I had three vars
Code:
User user1 = User.Instance;
User user2 = User.Instance;
User user3 = User.Instance;

you say
you create the user instance by User theUser = User.Instance;

Which implies user1, 2 & 3 are three separate instances of a User object.

But isn't User.Instance just a getter so user1, 2 & 3 are pointing to a single instance of the same object.

so if the User object had an attribute 'Name' with a setter & getter and I did
Code:
user1.Name("Paul");
user2.Name("Fred");
user3.Name("John");

Console.WriteLine(user1.Name());
Console.WriteLine(user2.Name());
Console.WriteLine(user3.Name());

I would get
John
John
John

Is that correct?








"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Yes, simply check ReferenceEquals(user1,user2) etc., that will also tell you the variables have the same reference, or in other words reference the same single instance.

You could also simply use User.Instance as your user instance, but often enough it's better readable code putting the global user instance into a variable to address it locally, it just adds another local and mostly temporary alias name to the single instance.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top