Thread: C# Having a class have a default return value

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    62

    C# Having a class have a default return value

    Hey all.
    I'm wondering if it's possible to have a class return a field of it's by default when it's used on the right hand side.

    for example:
    Code:
    namespace NAME_SPACE
    {
        class MyClass
        {
            public int MyInt;
            public int MyClass //I'm guessing something like this?
            {
                get
                {
                    return MyInt;
                }
                set
                {
                    MyInt = value;
                }
            }
            static Main(string [] args)
            {
                int foo = MyClass; //NOTE THIS HERE
            }
        }
    Last edited by Shingetsu Kurai; 12-05-2011 at 11:11 PM. Reason: Intendations to code

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    First, let's take a step backwards because you don't really seem to understand the distinction between a class and an object.

    Think of a class like a recipe for a cake. The recipe itself is not a cake. It's a set of instructions that describes how to make a cake. When you use the recipe to bake a cake, now you have an object. You could make one cake (object), or you could make forty cakes (objects). Your cakes have properties (one cake has blue frosting, the other has red, etc.) but your recipe itself doesn't have frosting. It may tell you how to MAKE frosting, but it's just a recipe; you need to use it to make something first.

    Now, when you want to look at the properties of a cake, you need to look at the properties of a PARTICULAR cake. That is, you need to specify WHICH cake's icing color you want to look at. Likewise, when you access a field in an object, you need to know WHICH object's fields you want.

    In your example code, you could make forty different objects from MyClass, each would have their own MyInt that could be a totally different value. The class itself doesn't HAVE a MyInt - the objects created from the class do (in the same way that a particular cake created from a recipe could have frosting, but the recipe itself doesn't have frosting).

    You need to specify which particular object's value you want, so you always need to use objectName.Property to access the property for a particular object.

    (Yes, static fields change the game a bit, but I think that's something you should worry about only after you have a decent understanding of object-oriented programming, or it'll just confuse the issue).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Look up the implicit operator: implicit
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You do not really need the backing store for your property unless you are doing something more complex with it. There are times when you must have a backing store but this is not one of them. As of C# 2008 you can take advantage of auto-implemented properties. Also you cannot return a value from a class. A method within the class can return a value but not the class itself.
    Last edited by VirtualAce; 12-07-2011 at 10:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class read as variable, default-type int? Say what?!
    By arcaine01 in forum C++ Programming
    Replies: 8
    Last Post: 07-10-2009, 05:34 PM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. Default Class Operators
    By symbiote3 in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2008, 01:44 PM
  4. How to set a "default" return value for a class?
    By Dash_Riprock in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2006, 07:05 PM
  5. Default Message Handler in a Class
    By Epo in forum Windows Programming
    Replies: 2
    Last Post: 07-23-2005, 09:28 PM

Tags for this Thread