开发者

object oriented: is it possible use a static instance like a variable?

开发者 https://www.devze.com 2023-03-18 02:12 出处:网络
I think I couldnt do this thing, but I try to ask (maybe :)). Suppose I have this Main class : public class UiUtils

I think I couldnt do this thing, but I try to ask (maybe :)).

Suppose I have this Main class :

public class UiUtils
{   
    public static MyObject myObject;
    public UiUtils()
    {
       myObject=new MyObject();
    }
}

now if I want to try to call this instance from another Context Class (web application), I do this :

public partial class context_eventi_CustomClass : System.Web.UI.UserControl
{   
    protected void Page_Load(object sender, EventArgs e)
    {
       Console.Write(UiUtils.myObject.Title());
    }
}   

bu开发者_如何转开发t what I'd like to do is this :

public partial class context_eventi_CustomClass : System.Web.UI.UserControl
{   
    protected void Page_Load(object sender, EventArgs e)
    {
       Console.Write(myObject.Title());
    }
}

so, use directly myObject and not UiUtils.myObject :)

I think this is not possible, but maybe I wrong and there are any strategies :) Thanks

** EDIT **

my solution for the moment :

public class UiUtils
{   
    public static MyObject myObject;
    public UiUtils()
    {
       myObject=new MyObject();
       iContext.myObject = myObject;
    }
}

public class iContext : System.Web.UI.UserControl
{
    public static MyObject myObject;

    public iContext()
    {

    }    

    public iContext(MyObject myObject)
    {
        myObject = myObject;
    }
}

public partial class context_eventi_CustomClass : iContext
{   
    protected void Page_Load(object sender, EventArgs e)
    {
       Console.Write(myObject.Title());
    }
}

seems to works! What do you think about?


Per MSDN,

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state.

and

"To access a static class member, use the name of the class instead of a variable name to specify the location of the member."

and

The static member is always accessed by the class name, not the instance name

@Daniel Earwicker says in his answer on SO here:

...Static members fail to integrate well with inheritance. It makes no sense to have them heritable. So I keep static things in separate static classes...

So I am not clear on your design why MyObject needs to be static. All you are trying to save is a little typing, but inheritance will not help you here either.

Edit:

I tried to replicate your code in a simple console application. The output is not what you would expect:

namespace ConsoleApplication1
{
    public class UiUtils
    {
        public static int myObject = 1;
        public UiUtils()
        {
            myObject = new int();
            iContext.myObject = myObject;
            Console.WriteLine("This is UiUtils\n");
        }
    }

    public class iContext
    {
        public static int myObject = 2;

        public iContext()
        {
            Console.WriteLine("This is iContext\n");
        }

        public iContext(int myObject)
        {
            myObject = myObject;
        }
    }

    public class iContext2 : iContext
    {
        public static int myObject = 3;

        public iContext2()
        {

            Console.WriteLine(myObject.ToString() + "\nThis is iContext2\n");
            Console.WriteLine(iContext.myObject.ToString());
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            iContext2 icontext = new iContext2();
            Console.In.ReadLine();
        }
    }
}

The output ends up being this:

This is iContext

3 
This is iContext2

If you add a call to iContext.myObject, then it outputs it's number:

This is iContext

3
This is iContext2
2


To access the object without typing the class you can use inheritance.

public class CustomClass : UiUtils

This will share UiUtils properties with CustomClass

0

精彩评论

暂无评论...
验证码 换一张
取 消