Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to use this (current instance reference) in class with C#

1 Answer

0 votes
using System;

namespace class_this
{
	class Class1
	{
		public class CPoint
		{
			public int a = 11;
			public int x, y;
			static int z = 22;
				
			public CPoint() {}
			
			public CPoint(int x, int y)
			{
				this.x = x;
				this.y = y;
			}
			public void show()
			{
				Console.WriteLine("show() a = {0} x = {1} y = {2} z = {3}", a, x, y, z++);
			}
			public void this_show()
			{                
                Console.WriteLine("this_show() a = {0} x = {1} y = {2} z = {3}", this.a, this.x,
                    this.y, z++);
                // this.z = Error (z cannot be accessed with an instance reference - this)
			}
		}
		static void Main(string[] args)
		{
			CPoint p1 = new CPoint();
			CPoint p2 = new CPoint();
			CPoint p3 = new CPoint(60, 80);

			p1.show(); // show() a = 11 x = 0 y = 0 z = 22
			p2.show(); // show() a = 11 x = 0 y = 0 z = 23
			p3.show(); // show() a = 11 x = 60 y = 80 z = 24
			Console.WriteLine("\n");
			p1.this_show();	// this_show() a = 11 x = 0 y = 0 z = 25
			p2.this_show(); // this_show() a = 11 x = 0 y = 0 z = 26
			p3.this_show(); // this_show() a = 11 x = 60 y = 80 z = 27

		}
	}
}




answered Jul 18, 2014 by avibootz
edited Jul 18, 2014 by avibootz
...