Solved Best way to create a users class/property

June 12, 2013 at 10:06:25
Specs: Windows 7, 4.2GHz Core 2 quad / 8gb ram
I want to create a users class which will have properties about user including id, name, status, etc.

What would the best way to go about this?

I was thinking creating a public static class and when the user logs out all I would have to do is dispose the class?


Note I am new at c#

See More: Best way to create a users class/property

June 12, 2013 at 11:10:42
✔ Best Answer
We need to clarify something here. Classes are what you write. They define the behavior of objects. Namely, the data they store and the functions that act on the data.

Objects are created from classes. They're the ones that hold the data you care about (id, name, status, etc.), and they're acted on by the functions defined in the class. They can be created, copied, changed, destroyed, etc. Objects are the living, breathing parts of your program, and they literally put the "Object" in Object Oriented Programming.

Static methods and fields are parts of classes that do not require an object to do their job, but are bundled with their class because they're related in some way. System.String is rife with this. String.IsNullOrEmpty() is a nice example of a static method. It's obviously related to Strings, and it has a clear reason to be static. After all, you cannot call a normal method with a null object.

A static field is String.Empty. It's public, readonly, and returns an object being defined by the class, but it doesn't need to be any of these. It is, however, closely related to Strings and String operations; the aforementioned IsNullOrEmpty() couldn't do its job without String.Empty!

Static classes, therefore, are classes that contain only static members, and cannot create objects.

So what you really want is just a normal class, and you can just create new user object from that class. Once you're done with the object, you can remove it from whatever collection you're using to hold user objects, set its value to null, or just let it fall out of scope.

How To Ask Questions The Smart Way

Report •

June 12, 2013 at 11:39:32
That is correct. That's exactly what I want to do. What is the best way to create a object that contains those values and have the object be seen throughout the application or is that not the correct way to do that?

Report •

June 12, 2013 at 12:25:33
You could have a static field in some class that's a collection of user objects.

You could create a singleton, which is the closest C# gets to globals.

You could have some sort of "main" object responsible for maintaining the list and use events to mange the user collection. This is the Windows Forms way; the central object is the main form, and this main form registers events raised by the other controls.

Usually I find it easier to just pass around a reference to the collection from object to object.

How To Ask Questions The Smart Way

Report •

Related Solutions

June 12, 2013 at 14:31:46
by passing object1 to object 2 does that copy object1 for object2s use or is it just a reference back to object1.

I'm worried about if object1 is updated that object2 wont see it.

Report •

June 12, 2013 at 14:49:39
It's a reference. There's only a few value types. Mostly just numbers, structs, and enums. Data types (MSDN)

How To Ask Questions The Smart Way

Report •

June 13, 2013 at 07:34:28
Thank you for your help.

Report •

Ask Question