|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Static vs Non-staticstatic members. In the below example static method 'name' accessing non-static method creating reference. Seems to me it is not holding to the theory? Pls explain. Here is an example: namespace StaticNonStaticMembers { class Program { static void Main(string[] args) { person p = new person(); System.Console.WriteLine(person.name()); System.Console.WriteLine(p.changename()); } } class person { public string changename() { return "changed"; } public static string name() { person p = new person(); return p.changename(); } } } Thanks. Rajesh,
From a static method/property, you can not access the instance members directly WITHOUT a reference. In your example, you have a reference to an instance, and therefore, can call the instance methods on that instance. Hope this helps. -- Show quoteHide quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "Rajesh" <rajesh.godavar***@gmail.com> wrote in message news:1176433402.791297.115100@p77g2000hsh.googlegroups.com... > Based on my understanding static members do not have access to non- > static members. In the below example static method 'name' accessing > non-static method creating reference. Seems to me it is not holding to > the theory? Pls explain. > > Here is an example: > namespace StaticNonStaticMembers > { > class Program > { > static void Main(string[] args) > { > person p = new person(); > System.Console.WriteLine(person.name()); > System.Console.WriteLine(p.changename()); > > } > } > > class person > { > public string changename() > { > return "changed"; > } > > public static string name() > { > person p = new person(); > return p.changename(); > } > } > } > > Thanks. > On Thu, 12 Apr 2007 20:03:22 -0700, Rajesh <rajesh.godavar***@gmail.com>
wrote: > Based on my understanding static members do not have access to non- That's not true. A static member of a class can access non-static > static members. members, as long as they specify an instance. > In the below example static method 'name' accessing Yes, the static method "name()" does access the non-static method > non-static method creating reference. "changename()" of the instance created. Of course, that doesn't prove anything because the method is public and so *any* code can access that. It would be more interesting for you to show code where a static method of the class accesses a protected or private method or field of the same class, since that's a scenario in which only the methods within the class are allowed to access that member. You would find that also works, but at least it is an interesting example. > Seems to me it is not holding to the theory? Pls explain. What theory?Pete
Show quote
Hide quote
On Apr 12, 8:32 pm, "Peter Duniho" <NpOeStPe***@nnowslpianmk.com> Agree with you.wrote: > On Thu, 12 Apr 2007 20:03:22 -0700, Rajesh <rajesh.godavar***@gmail.com> > wrote: > > > Based on my understanding static members do not have access to non- > > static members. > > That's not true. A static member of a class can access non-static > members, as long as they specify an instance. > > > In the below example static method 'name' accessing > > non-static method creating reference. > > Yes, the static method "name()" does access the non-static method > "changename()" of the instance created. Of course, that doesn't prove > anything because the method is public and so *any* code can access that. > > It would be more interesting for you to show code where a static method of > the class accesses a protected or private method or field of the same > class, since that's a scenario in which only the methods within the class > are allowed to access that member. You would find that also works, but at > least it is an interesting example. > > > Seems to me it is not holding to the theory? Pls explain. > > What theory? > > Pete Theory meaning "static members do not have access to non-static members." But it cleared up .. thanks for your help. "Rajesh" <rajesh.godavar***@gmail.com> wrote in message That definition is inacurate.news:1176433402.791297.115100@p77g2000hsh.googlegroups.com... > Based on my understanding static members do not have access to non- > static members. Try static members do not have access to non static members of the same class without first creating an instance of the object and accessing the nonstatic members via the instance. take a look at this: using System; class Foo { private int x; public Foo(int x){this.x = x;} static void Main(string[] args) { Foo foo1 = new Foo(5); Foo foo2 = new Foo(10); Console.WriteLine(foo1.x); Console.WriteLine(foo2.x); } } --------------------- prints 5 10 --------------------- Since there is one x for every instance of Foo, you obviously can't access x without knowing which foo it lives in. It is not that "they" are attempting to restrict what static method can/cannot do. It simply does not make sense for a static method to access a nonstatic member without an instance of the object. Now you may argue "Hey, what if it is a nonstatic Method? why can't I access that?" For instance (added to prior example) public void Method1() { Console.WriteLine("x:{0}",x); } and public void Method2() { Console.WriteLine("Hello from the inside of Foo"); } Method1 makes no sense without an instance of Foo because it needs to know the value of x Method2 on the other hand knows nothing of the internal state of Foo...so why can't I call it???? Because you didn't make it static!!! There is absolutely no reason for Method2 to NOT be static. Hopefully, my rambling will help clear this up a bit Bill
Show quote
Hide quote
On Apr 12, 8:37 pm, "Bill Butler" <qwe***@asdf.com> wrote: Bill,> "Rajesh" <rajesh.godavar***@gmail.com> wrote in message > > news:1176433402.791297.115100@p77g2000hsh.googlegroups.com... > > > Based on my understanding static members do not have access to non- > > static members. > > That definition is inacurate. > Try > static members do not have access to non static members of the same > class without first creating an instance of the object and accessing the > nonstatic members via the instance. > > take a look at this: > > using System; > class Foo > { > private int x; > > public Foo(int x){this.x = x;} > > static void Main(string[] args) > { > Foo foo1 = new Foo(5); > Foo foo2 = new Foo(10); > Console.WriteLine(foo1.x); > Console.WriteLine(foo2.x); > } > } > > --------------------- > prints > 5 > 10 > > --------------------- > > Since there is one x for every instance of Foo, you obviously can't > access x without knowing which foo it lives in. > > It is not that "they" are attempting to restrict what static method > can/cannot do. It simply does not make sense for a static method to > access a nonstatic member without an instance of the object. > > Now you may argue "Hey, what if it is a nonstatic Method? why can't I > access that?" > For instance (added to prior example) > public void Method1() > { > Console.WriteLine("x:{0}",x); > } > > and > public void Method2() > { > Console.WriteLine("Hello from the inside of Foo"); > } > > Method1 makes no sense without an instance of Foo because it needs to > know the value of x > Method2 on the other hand knows nothing of the internal state of > Foo...so why can't I call it???? > Because you didn't make it static!!! > > There is absolutely no reason for Method2 to NOT be static. > > Hopefully, my rambling will help clear this up a bit > > Bill > Since there is one x for every instance of Foo, you obviously can't Your point makes perfect sense with above desc... It cleared the> access x without knowing which foo it lives in. > > It is not that "they" are attempting to restrict what static method > can/cannot do. It simply does not make sense for a static method to > access a nonstatic member without an instance of the object. confusion. Appreciate your help !!! Rajesh,
There is nothing that has access to a non-static member of a class, simple because it does not exist as long as it is not instanced from the template (the non static class). . Only after that it is instanced as a part of your program, an object in this case, you can use the members in that object. A static member (which can be a part of your class) is nothing than a module part of your program. It exist from the begin to the end and it will be there from the start to the end of the program occupying the memory where it is static in. Therefore those static parts will never be constructed by you. Cor Show quoteHide quote "Rajesh" <rajesh.godavar***@gmail.com> schreef in bericht news:1176433402.791297.115100@p77g2000hsh.googlegroups.com... > Based on my understanding static members do not have access to non- > static members. In the below example static method 'name' accessing > non-static method creating reference. Seems to me it is not holding to > the theory? Pls explain. > > Here is an example: > namespace StaticNonStaticMembers > { > class Program > { > static void Main(string[] args) > { > person p = new person(); > System.Console.WriteLine(person.name()); > System.Console.WriteLine(p.changename()); > > } > } > > class person > { > public string changename() > { > return "changed"; > } > > public static string name() > { > person p = new person(); > return p.changename(); > } > } > } > > Thanks. > "Rajesh" <rajesh.godavar***@gmail.com> schrieb im Newsbeitrag static and non-static has nothing to do with accessibility.news:1176433402.791297.115100@p77g2000hsh.googlegroups.com... > Based on my understanding static members do not have access to non- > static members. The point is: non-static members can only be called in the context of an instance. Inside that member this instance can be accessed by the keyword "this". Inside a non-static method, you can call any nonstatic member implicitly on "this". In static members there is no "this", (because they don't run in the context of an instance.) So this implicit reference to this is not possible. But this is not a question of accessibility. You alwys name any instancs e member explicitly on an instance. Christof As long as you have an instance of the class, whose members you are
calling, you can access instance members from a static member. In your example, you have a Person instance which is being used to call the instance member. with regards, J.V.Ravichandran - http://www.geocities.com/ jvravichandran - Or, just search on "J.V.Ravichandran" at http://www.Google.com *** Sent via Developersdex http://www.developersdex.com ***
Other interesting topics
closing excel from c#
Refactoring class location Generics Screen Scraping a Password Protected Site Screen resolution & color depth for website Need Help: Common IsolatedStorage per user regardless of application How to use object? Can someone show an example of how to read an XML file into an XMLDocument object? Exception when accessing backgroundWorker.CancellationPending GridView not being displayed: Lil silly mistake i guess.... |
|||||||||||||||||||||||