Syntax for C# Array Property

Started by
16 comments, last by haro 19 years, 10 months ago
quote:Original post by Anonymous Poster
personaly i use

<access flags> <type>[] Name { get { return array; } }


I believe that is what Arild was suggesting. But this just really doesn''t solve the problem I am having. There would be little difference in this, and making the array public access in the class. If I use this, then a user of the code still ends up with complete write access to the entire tree structure in one go.

I wanted to limit the interface to nodes. Furthermore this takes away the power of the class which is returning the node, to determine which node to return. IE- if it is determined that there are only 3 main value types of the nodes ( and there are 100''s of nodes), then I would probably just have the 3 actual nodes, and a list of value type lookups for the rest of the nodes. Then whenever a user requested say: child[14] I would return the value type which child[14] referred to from the internal lookup table. This isn''t possible if I just create an array property, since I''m just returning the whole load of data in one go.
Advertisement
haro then i belive it would be easier if not better to have it as a function, due there being limitations in propertie declerations
quote:This isn''t possible if I just create an array property,

The suggestion was to return something with an indexer, not necessarily an array.

quote:since I''m just returning the whole load of data in one go.

Then DON''T DO THAT. Duh.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Take a look at f.ex the System.Windows.Forms.TreeView and TreeNode classes.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
BTW: Indexed properties are supported by the CLR, MC++ and VB.NET(I think). Not adding them to C# was a conscious choice by the language designers. Googling around the microsoft.public.dotnet.languages.csharp group should give you the rationale.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
I got almoust the same problem, (If you don''t mind me interrupting the topic).
i need to create a property to a control (in C#) that will be an array. it supposed to look like a static ENum property but made of an array or a list(collection in general).

i mean that i will see in the properties of the control a list box containing strings that are sitting in a collection.

THNX

I am a VB .NET programmer thinking of moving to C#, but I think you can just do like:

public int property Blah(int index)
{
get
{
return m_Blah(index);
}
set(int value)
{
m_Blah[index] = value;
}
}

Of course that might be wrong... why don''t you look it up on NET framework SDK?
Hi,

I was looking for an indexed property in C# also, but apparently C# doesn''t allow this. So here is a way that a co-worker and I came up with to work around that. It requires an extra indexer class, but it works. Class2 is your class and the ChildListIndexer class does the work.

Daniel


using System;
namespace ConsoleApplication1
{
public class ChildListIndexer
{
private int[] list;
public ChildListIndexer(int[] list)
{
this.list = list;
}
public int this[int index]
{
get { return list[index]; }
}
public int Length
{
get { return list.Length; }
}
}

public class Class2
{
public Class2()
{
localList = new int[10];
for (int i = 0; i < 10; i++)
localList = i + 1;
listIndexer = new ChildListIndexer(localList);
}
private int[] localList;
public ChildListIndexer Child
{
get { return listIndexer; }
}
private ChildListIndexer listIndexer;
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
Class2 c = new Class2();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Node["
+ i.ToString()
+ "] = "
+ c.Child.ToString()<br> + &quot; Type = &quot;<br> + c.Child.GetType().Name);<br> }<br> Console.WriteLine(&quot;Length = &quot; + c.Child.Length.ToString());<br> System.Console.ReadLine();<br> }<br> }<br>}<br> </i>

This topic is closed to new replies.

Advertisement