الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

شرح ال Indexer

مغلق
بدأه Jordan في 27 نوفمبر 2006 · 6 رد · 1,617 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

أخواني ،، احببت ان أشرح القليل عن الـ Indexer لأني تغلبت لما فهمته .. و ان شالله تعم الفائده ،،

و أنا جاهز لاي استفسار ...

بسم الله نبدأ ،،

Indexer

كما هو معروف ،، فإن الـ Class يستخدم This كــــ keyword ،، و ذلك لتعريف الاندكسير Indexer ،،

و هذا من شأنه السماح للـ array بالوصول الى العناصر و المتغيرات بالـClass

allow array-style indexed access to lists of elements in a class

الـ Syntax للـ Indexer كالتالي :

accessModifier returnType this[ IndexType1 name1, IndexType2 name2, … ]
{	
   get	
			 {		  // use name1, name2, ... here to get data   }
  set	   
			 {		// use name1, name2, ... here to set data	}
}

كن حذراً .. فإن تعريف الـ Indexer كـــ Static هو : syntax Error

و هذا مثال للتوضيح ،،

	1	//  Box.cs
	2	// Box class definition represents a box with length, 
	3	// width and height dimensions with indexers.
	4	public class Box
	5	{
	6	   private string[] names = { "length", "width", "height" };
	7	   private double[] dimensions = new double[ 3 ];
	8	
	9	   // constructor
	10	   public Box( double length, double width, double height )
	11	   {
	12		  dimensions[ 0 ] = length;
	13		  dimensions[ 1 ] = width;
	14		  dimensions[ 2 ] = height;
	15	   }
	16	
	17	   // indexer to access dimensions by integer index number
	18	   public double this[ int index ]							 
	19	   {														   
	20		  get													  
	21		  {														
	22			 // validate index to get							  
	23			 if ( ( index < 0 ) || ( index >= dimensions.Length ) )
	24				return -1;										 
	25			 else												  
	26				return dimensions[ index ];						
	27		  } // end get											 
	28		  set													  
	29		  {														
	30			 if ( index >= 0 && index < dimensions.Length )		
	31				dimensions[ index ] = value;					   
	32		  } // end set											 
	33	   } // end numeric indexer									
	34	
	35	   // indexer to access dimensions by their string names				  
	36	   public double this[ string name ]						 
	37	   {														 
	38		  get													
	39		  {													  
	40			 // locate element to get							
	41			 int i = 0;										  
	42			 while ( ( i < names.Length ) &&					 
	43				( name.ToLower() != names[ i ] ) )			   
	44				i++;											 
	45																 
	46			 return ( i == names.Length ) ? -1 : dimensions[ i ];
	47		  } // end get										   
	48		  set													
	49		  {													  
	50			 // locate element to set							
	51			 int i = 0;										  
	52			 while ( ( i < names.Length ) &&					 
	53				( name.ToLower() != names[ i ] ) )			   
	54				i++;											 
	55																 
	56			 if ( i != names.Length )							
	57				dimensions[ i ] = value;						 
	58		  } // end set										   
	59	   } // end string indexer								   
	60	} // end class Box

Box.cs

1	//BoxTest.cs
	2	// Indexers provide access to a Box object's members.
	3	using System;
	4	
	5	public class BoxTest
	6	{
	7	   public static void Main( string[] args )
	8	   {
	9		  // create a box
	10		  Box box = new Box( 30, 30, 30 );
	11	
	12		  // show dimensions with numeric indexers
	13		  Console.WriteLine( "Created a box with the dimensions:" );
	14		  Console.WriteLine( "box[ 0 ] = {0}", box[ 0 ] );
	15		  Console.WriteLine( "box[ 1 ] = {0}", box[ 1 ] );
	16		  Console.WriteLine( "box[ 2 ] = {0}", box[ 2 ] );
	17	
	18		  // set a dimension with the numeric indexer
	19		  Console.WriteLine( "\nSetting box[ 0 ] to 10...\n" );
	20		  box[ 0 ] = 10;
	21	
	22		  // set a dimension with the string indexer
	23		  Console.WriteLine( "Setting box[ \"width\" ] to 20...\n" );
	24		  box[ "width" ] = 20;
	25	
	26		  // show dimensions with string indexers
	27		  Console.WriteLine( "Now the box has the dimensions:" );
	28		  Console.WriteLine( "box[ \"length\" ] = {0}", box[ "length" ] );
	29		  Console.WriteLine( "box[ \"width\" ] = {0}", box[ "width" ] );
	30		  Console.WriteLine( "box[ \"height\" ] = {0}", box[ "height" ] );
	31	   } // end method Main
	32	} // end class BoxTest

BoxTest.cs

أما نتيجة التنفيذ ،، فهي كالتالي :

Created a box with the dimensions:
box[ 0 ] = 30
box[ 1 ] = 30
box[ 2 ] = 30

Setting box[ 0 ] to 10...

Setting box[ "width" ] to 20...

Now the box has the dimensions:
box[ "length" ] = 10
box[ "width" ] = 20
box[ "height" ] = 30

أتمنى أن يكون واضح ،،

Success Is A Journey Not A Destination

#2

اشكرك وبارك الله فيك اخي Jordan وسدد خطاك

تم تعديل هذه المشاركة بواسطة abumarie2003 في 27 نوفمبر 2006 في 16:52

#3

شكرا شكرا يا عزيزي ولكن هل يمكن عمل اكثر من Indexer في كلاس واحدة ؟

#4
بشا كتب:
شكرا شكرا يا عزيزي ولكن هل يمكن عمل اكثر من Indexer في كلاس واحدة ؟

العفو أخي \ أختي ،،

لكن لماذا تحتاجي أكثر من Indexer في نفس الكلاس ؟؟

انتي تتعاملي مع الـ method التاليه : set & get ... و هم من يقوم باستدعاء المتغيرات من الـ array حسب حاجتك ،،

لا داعي لتعريف أكثر من indexer .. فهو من خلال الميثود يعمل access على أي عنصر بالكلاس ،،

أتمنى أن تكون وصلت الفكره ،،

تأملي الكود مره اخرى :rolleyes:

accessModifier returnType this[ IndexType1 name1, IndexType2 name2, … ]
{	
   get	
			 {		  // use name1, name2, ... here to get data   }
  set	  
			 {		// use name1, name2, ... here to set data	}
}

Success Is A Journey Not A Destination

#5

سؤالي هو هل من الممكن ان يكون في كلاس اكثر من Indexer ؟

لانني حاولت فعل دلك ولم انجح باستخدام كلمة This وعلى حسب ما فهمت تكون كلمة This وحدة بس

عند استخدام Indexer ..

وعموما شكرا شكرا على ردك يا اخي Jordan

تسلم ........ تسلم

#6

شكرا Jordan :)

بشا: يمكن عمل Overloads للـ Indexer بطريقه عاديه لتوفير أكثر من خيار للمستهلك اذا كنت تقصد ذلك :

public int this[int theIndex]
{
get{return...}
set{...}
}

public int this[string theName]
{
get{return...}
set{...}
}

#Read

http://MoutazShams.blogspot.com

projects003.gif

أعتذر عن التغيب هذه الأيام نظرا للانشغال المريب

#7

شكرا لكم جميع سأحاول تطبيق الكلام سابق

سبحان الله وبحمده سبحان الله العظيم

هذا الموضوع مغلق.

مواضيع مشابهة