Using property accessors with a collection or array

  • Thread starter Thread starter EmperorKane
  • Start date Start date
E

EmperorKane

I know that I can use an indexer to pull data from an array.

data = SomeClass[0];

But what I want to see as the interface is:

data = SomeClass[0].Data

Specifically what I'm trying is this:

struct MyStruct {
string name;
int value;
}

public class MyClass()
{
MyStruct s = new MyStruct[50];
}

Main()
{
MyClass s;

s[0].Name = "Value";
s[0].Value = 1234;

value = s[0].value;
}


What I want is exactly what the listbox has for Items. It is a
collection that you would access the same way.

In the past I have used Methods to accomplish this;

SetData(int index, int data);
value = GetData(int index);

I like the way properties work and was wondering if anyone can help me
with the implementation of this code.

Thanks very much for any help
 
What you have below looks ok, is it intellisense that is not working, the
only syntax error I see is:

value = s[0].value;

should be:

value = s[0].Value;

This should work, but when you hit the . intellesense may not work.

EmperorKane said:
I know that I can use an indexer to pull data from an array.

data = SomeClass[0];

But what I want to see as the interface is:

data = SomeClass[0].Data

Specifically what I'm trying is this:

struct MyStruct {
string name;
int value;
}

public class MyClass()
{
MyStruct s = new MyStruct[50];
}

Main()
{
MyClass s;

s[0].Name = "Value";
s[0].Value = 1234;

value = s[0].value;
}


What I want is exactly what the listbox has for Items. It is a
collection that you would access the same way.

In the past I have used Methods to accomplish this;

SetData(int index, int data);
value = GetData(int index);

I like the way properties work and was wondering if anyone can help me
with the implementation of this code.

Thanks very much for any help
 
Back
Top