Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Static Class
Message
 
À
04/07/2005 06:16:59
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Divers
Thread ID:
01028423
Message ID:
01028815
Vues:
9
Ok, that cleared it up for me.

Thanks



>Kevin,
>You can. To access non static ones you need to use instance (or 'this' within class itself) identifier. To access static ones you directly use class name instead (do not use instance name - error by language definition). ie:
>
>using System;
>
>namespace Static_Instance
>{
>  class Test
>  {
>    [STAThread]
>    static void Main(string[] args)
>	{
>		Console.WriteLine("There are {0} mypoint instances:",myPoint.GetPoints());
>		myPoint[] myPoints =
>			{ new myPoint(10,20), new myPoint(30,40), new myPoint(50,60) };
>		Console.WriteLine("Now there are {0} mypoint instances:",myPoint.GetPoints());
>		for (int i=0;i<myPoint.GetPoints();i++)
		{
			Console.WriteLine(myPoints[i].GetPoint());
		}
	}
  }
  class myPoint
  {
	private int left,top;
	private static int points;
	public myPoint(int left,int top)
	{
		this.left = left;
		this.top  = top;
		points++;
	}
	public static int GetPoints()
	{
		return points;
	}
	public string GetPoint()
	{
		return String.Format("Left:{0},Top:{1}",left,top);
	}
  }
}
Note that to use static method we directly use class' name. It'd be an error to use instance name instead.
>
>
>myPoint pt = new myPoint(1,2);
>pt.GetPoint(); // instance method call
>myPoint.GetPoints(); // static method call
>pt.GetPoints(); // error
>
>Cetin
>
>>Ok, I understand that. But if the methods both exists on the class,
>>and only non-static are available on instances, I should still be
>>able to then access the non-static.
>>
>>
>>>Kevin,
>>>
>>>I think you're talking about why you can't access both methods in the intellisense for the instantiated version of the class? If I'm wrong ignore all that follows :)
>>>
>>>Static methods apply to all instances of a class, so if you have 10 instances they are not using a method on the object, they are using a method on the class. This is why you cannot see the static methods in the intellisense for an instance of the class, but you can when you reference the class directly.
>>>
>>>This has been done on purpose, so it is absolutely clear when you are using a member of a specific instance or of a class as a whole. What you are seeing is two different aspects of the class.
>>>
>>>
>>>>I'm just starting out with C#.Net so I have no idea
>>>>why I would need to do this.
>>>
>>>>But in the example below, there are 2 methods in
>>>>the class. I don't full understand why you cannot
>>>>access both static and non static methods.
>>>
>>>>Kevin,
>>>>
>>>>Can you give us an example of why you'd need to do this? Then maybe we can come up with an easy solution.
>>>>
>>>>~~Bonnie
>>>>
>>>>
>>>>>You misunderstood my question.
>>>>>
>>>>>Intellisense doesn show both methods at once. So how
>>>>>do you call both if you need to?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>>>
>>>
>>>>>>>Class1 oTest = new Class1();
>>>>>>>oTest.Method1(); // Method1 is not static
>>>>>>>
>>>>>>>Test1.Class1.Method2(); // Method2 is static
>>>>>>>
>>>
>>>
>>>>>>>
>>>>>>>What if I had to call both Method1 and Method2?
>>>>>>
>>>>>>I don't think that would be a problem. But I'm not sure what the advantage would be of designing a class that works like you describe.
>>>>>>
>>>>>>Malcolm
>>>
>>>Everything makes sense in someone's mind
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform