INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Can't create a new object?

Can't create a new object?

(OP)
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...

Quote:

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

RE: Can't create a new object?

(OP)
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

RE: Can't create a new object?

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 http://msdn.microsoft.com/en-us/library/ff650316.a...

Bye, Olaf.


RE: Can't create a new object?

(OP)
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

Quote:

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

Quote:

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

RE: Can't create a new object?

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close