1

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());
    }
}   

but 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?

markzzz
  • 47,390
  • 120
  • 299
  • 507
  • did you mean you want to do: `Console.Write(myObject.Title());` as `myObject`'s instance in CustomClass.. ? – ub1k Jul 08 '11 at 14:29
  • Yeah, use `myObject` and not `UiUtils.myObject` – markzzz Jul 08 '11 at 14:30
  • Your current solution may work but doesn't make sense to do it that way just so you can skip typing a class name. What is the problem with using the class name? – zeal Jul 08 '11 at 15:26

2 Answers2

2

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
Community
  • 1
  • 1
  • Added a solution! What do you think about? – markzzz Jul 08 '11 at 15:00
  • @markzzz: You should make the example simpler and print to the console such as "This is class UiUtils", etc. That way you can watch how the classes are being called. –  Jul 08 '11 at 15:03
  • In fact it works on my web application! but maybe I can do better :) – markzzz Jul 08 '11 at 15:12
  • @markzzz: look again. UiUtils is never called and the inheritance is not working like you wanted. –  Jul 08 '11 at 15:20
  • @markzzz: No you don't. You define it, but it is never called. I copied your example verbatim and made some slight changes but that's it. –  Jul 08 '11 at 15:23
  • UiUtils is called in my application as first class (ever). It create the instance of myObject, and pass it to iContext. Then, on other class (CustomClass) it extend iContext and get the right instance of myObject :) (im on a web application, with .NET, and a flux of operations. UiUtils will ever be called before any class) – markzzz Jul 08 '11 at 15:25
  • You don't call here! But on my application it is called before other classes! :) – markzzz Jul 08 '11 at 15:44
  • @markzzz: I can't help you if you don't post all your code or give me all the information. I am not a mind-reader. –  Jul 08 '11 at 16:41
1

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

public class CustomClass : UiUtils

This will share UiUtils properties with CustomClass

zeal
  • 740
  • 1
  • 4
  • 31
  • Sorry! I forgot to write that my class extend already another class (: System.Web.UI.UserControl) – markzzz Jul 08 '11 at 14:33
  • then make `UiUtils : UserControl` and `CustomClass : UiUtils` – ub1k Jul 08 '11 at 14:35
  • Update the answer! Sorry I forgot a important details :) – markzzz Jul 08 '11 at 14:35
  • I can't! UiUtils it's a "real" class :) – markzzz Jul 08 '11 at 14:35
  • If you can't do inheritance then your probably going to have to stick with using the class name before the variable. What was the issue with using the class name? – zeal Jul 08 '11 at 14:47
  • @0A0D: How is this bad? I cant post comments yet, but your answer was wrong also. – zeal Jul 08 '11 at 14:50
  • @ZEAL: See here http://stackoverflow.com/questions/2281775/c-static-member-inheritance-why-does-this-exist-at-all –  Jul 08 '11 at 14:52
  • and here: http://stackoverflow.com/questions/774181/why-cant-i-inherit-static-classes/774205#774205 –  Jul 08 '11 at 14:57
  • He is talking about static variables not classes. And i know it doesn't make since to do this this way, but it does work there for it is not wrong. – zeal Jul 08 '11 at 15:00
  • @ZEAL: UiUtils is not static. –  Jul 08 '11 at 15:02