properties in c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello

I have such problem:

int[] var1 = new int[10];
how to write properties for this variable? but propoerty must contain
independent table of int's stored in var1.
This statement is not sufficient:

public int[] Var1
{
get{return var1;}
set{var1=value;}
}

thanks
 
Do you mean to write an indexer?

public class WithIndexer
{

int[] vars=new int[100];

public int this[int index]
{
get{return vars[index];}
set{vars[index]=value;}
}

}

The variables can be accessed using the following...

WithIndexer x=new WithIndexer()
x[10]=5;
int y=x[10];

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
hi ya,
ok i think i kinda know what u want...the sample code you've given is a little abstract :) but...

//this is creating an Array of 10 int's, with the array name var1
int[] var1=new int[10];

//now setting a value to one of the array elements...
void SetElement(int element, int value)
{
var1[element]=value;
}

//now to display an array element..
void GetElement(int element)
{
cout<<"Var1["<<element<<"] contains the value "<<var1[element];
//i think this is right might be ...<<var1[element].ToString



//OR do you mean you want to display 'properties' or ..members
//like a class would? eg...var1.
 
Hi DAMAR,

This statement should do what you want:

private int[] var1;

public int[] Var1
{
get{
int[] tempVar1=new int[var1.Length];
Array.Copy(var1, tempVar1, var1.Length);
return tempVar1;
}
set{
var1=new int[value.Length];
Array.Copy(value, var1, value.Length);
}
}

HTH
Marcin
 
Back
Top