• Create Account

Syntax for C# Array Property

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

17 replies to this topic

#1haro  Members

Posted 04 February 2004 - 12:26 PM

What is the correct syntax in C# for declaring a property which acts like an array? I did not find many helpful links googling for C# proprties array. What I want to do is something like:
public Node Child[int offset]
{
get
{
.. blah blah blah
return localList[offset];
}
}

I want to access the child of a node in a quadtree via: if( currentNode.child[0] == null) blah blah blah... but the node being accessed isn''t necessarily always going to actually be child[0], so I wanted to use a property as opposed to just making the child list public. I don''t really want to use a getter with an offset as I''ve then just completely missed the point of properties.

#2TangentZ  Members

Posted 04 February 2004 - 12:50 PM

I''m guessing what you want is an "indexer" for your class,
from the syntax.

public class Node    {    public Node this[int offset]        {        get { return localList[offset]; }        }    // end    }// end

Node n = new Node();Node child = n[5];  // The 6-th child

See here for more information, or the MSDN, of course.

Kami no Itte ga ore ni zettai naru!

#3haro  Members

Posted 04 February 2004 - 01:01 PM

Thanks, but that''s not quite what I meant. I don''t want to index directly into the class, but rather into a member of the class. Ie- Node.Child[0];

#4TangentZ  Members

Posted 04 February 2004 - 01:13 PM

OK, see this thread.

Kami no Itte ga ore ni zettai naru!

#5Arild Fines  Members

Posted 04 February 2004 - 01:16 PM

You need to expose a property of a type that has an indexer, like an array or an IList.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

#6haro  Members

Posted 04 February 2004 - 01:51 PM

Those solutions really just don''t work so well when I want to create a property of a list of Object X within an Object X since I don''t want to create a public indexer. I don''t like the interface of having the children of a node represented as indices into the parent node.

#7DrPizza  Members

Posted 04 February 2004 - 11:22 PM

Arild Fines'' answer does exactly what you''ve asked for in the initial post!

If it''s not good enough, ask a different fucking question.

#8haro  Members

Posted 05 February 2004 - 01:04 AM

EDIT: -clipped- Responding to a flame with a flame is pointless.

[edited by - haro on February 5, 2004 8:29:08 AM]

#9Anonymous Poster_Anonymous Poster_*  Guests

Posted 05 February 2004 - 01:09 AM

personaly i use

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

#10quorn3000  Members

Posted 05 February 2004 - 01:19 AM

quote:
Original post by haro
I cannot wait for the new forums. You, DrPizza, seem too typical. You think you lots to say, but yet you never say anything useful. The majority of what you say is nothing more than poorly veiled trolling/flaming. You seem distinctly similiar to another GD''er: SaberWolf ( or some spelling of the such ).

Give it a break, DrPizza has lots of useful stuff to say as does SabreMan. You do sometimes too.

#11haro  Members

Posted 05 February 2004 - 01:38 AM

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.

#12Anonymous Poster_Anonymous Poster_*  Guests

Posted 05 February 2004 - 02:04 AM

haro then i belive it would be easier if not better to have it as a function, due there being limitations in propertie declerations

#13DrPizza  Members

Posted 05 February 2004 - 02:09 AM

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.

#14Arild Fines  Members

Posted 05 February 2004 - 06:44 AM

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]

#15Arild Fines  Members

Posted 05 February 2004 - 06:46 AM

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.

#16HELLmaker  Members

Posted 16 May 2004 - 08:05 PM

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

#17DrGUI  Members

Posted 17 May 2004 - 01:31 AM

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?

#18Anonymous Poster_Anonymous Poster_*  Guests

Posted 26 May 2004 - 03:12 PM

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
{
static void Main(string[] args)
{
Class2 c = new Class2();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Node["
+ i.ToString()
+ "] = "
+ c.Child[i].ToString()
+ " Type = "
+ c.Child[i].GetType().Name);
}
Console.WriteLine("Length = " + c.Child.Length.ToString());