• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

haro

Syntax for C# Array Property

17 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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!
0

Share this post


Link to post
Share on other sites
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];
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
EDIT: -clipped- Responding to a flame with a flame is pointless.

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

Share this post


Link to post
Share on other sites
personaly i use

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

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
haro then i belive it would be easier if not better to have it as a function, due there being limitations in propertie declerations
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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

0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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[i].ToString()
+ " Type = "
+ c.Child[i].GetType().Name);
}
Console.WriteLine("Length = " + c.Child.Length.ToString());
System.Console.ReadLine();
}
}
}
0

Share this post


Link to post
Share on other sites