ArrayList Question

H

Hoop

Hello,
I am trying to create a property to read and write strings into an
arrayList.
What I have is I create an arrrayList in my class,

ArrayList m_circuitNames = new ArrayList();

And now am trying to create a property to read and write the list,

public ArrayList CircuitName( int location)
{
get
{
return (ArrayList)m_circuitNames[location];
}

set
{
m_circuitNames.Insert(location, value);
}

}

However, once I add the location argument it does not compile anymore.
How to I correcly create a propery to insert and retrive strings from
an arrayList()?
 
N

not_a_commie

I think you should start with some books on C#. Get and Set don't
belong in function declarations.
 
H

Hoop

I think you should start with some books on C#. Get and Set don't
belong in function declarations.


Hello,
I am trying to create a property to read and write strings into an
arrayList.
What I have is I create an arrrayList in my class,
 ArrayList m_circuitNames = new ArrayList();
And now am trying to create a property to read and write the list,
public ArrayList CircuitName( int location)
        {
            get
            {
                return (ArrayList)m_circuitNames[location];
            }
            set
            {
                m_circuitNames.Insert(location, value);
            }
        }
However, once I add the location argument it does not compile anymore.
How to I correcly create a propery to insert and retrive strings from
an arrayList()?- Hide quoted text -

- Show quoted text -

Hi,
As I mentioned in my post, this a property, not a function, it was,

public ArrayList CircuitNames
{
get { return m_circuitNames; }
set { m_circuitNames = value; }
}
And now I need to pass an index via the Property, to store the string.
And access it at some point also.

Thanks for the Help
Jeff
 
M

Marc Gravell

That is a little harsh; until he added the argument "(int location)" it was
a property, albeit one with the wrong type (should have been string not
ArrayList) - so the real answer is:

"C# doesn't support named indexers; and if it did, they would probably be
with square-bracket notation to distinguish from functions."

Also - a typed collection would be preferable. If you are using 1.1, then
StringCollection - otherwise (2.0) perhaps just List<string>.

A bigger problem is possibly that this use of Insert is going to make it
very hard to update an item - i.e.

foo[0] = "abc";
foo[0] = "def";
foo[0] = "ghi";

// contains 3 items, not 1


But taking the existing example (with a standard "this" (Item) indexer):

class Foo {
StringCollection circuitNames = new StringCollection();
public string this[int location]
{
get
{
return circuitNames[location];
}
set
{
circuitNames.Insert(location, value);
}
}
}

then Foo foo = new Foo();
foo[0] = "abc";
foo[1] = "def";

etc
 
J

Jon Skeet [C# MVP]

As I mentioned in my post, this a property, not a function, it was,

public ArrayList CircuitNames
{
get { return m_circuitNames; }
set { m_circuitNames = value; }
}
And now I need to pass an index via the Property, to store the string.

At that point you'd be turning it into an indexer - but you can't name
it. For instance:

public string this[int index]
{
get { return (string)m_circuitNames[index]; }
set { m_circuitNames.Insert(index, value); }
}

Jon
 
H

Hoop

That is a little harsh; until he added the argument "(int location)" it was
a property, albeit one with the wrong type (should have been string not
ArrayList) - so the real answer is:

"C# doesn't support named indexers; and if it did, they would probably be
with square-bracket notation to distinguish from functions."

Also - a typed collection would be preferable. If you are using 1.1, then
StringCollection - otherwise (2.0) perhaps just List<string>.

A bigger problem is possibly that this use of Insert is going to make it
very hard to update an item - i.e.

foo[0] = "abc";
foo[0] = "def";
foo[0] = "ghi";

// contains 3 items, not 1

But taking the existing example (with a standard "this" (Item) indexer):

class Foo {
    StringCollection circuitNames = new StringCollection();
    public string this[int location]
    {
        get
        {
            return circuitNames[location];
        }
        set
        {
            circuitNames.Insert(location, value);
        }
    }

}

then Foo foo = new Foo();
foo[0] = "abc";
foo[1] = "def";

etc

Hi Marc, Jon,

Unfortunately I have three of the arrayLists in this class, all three
will have to follow the same insert concept.
I am thinking I will just have to go with a standard access scheme
rather than properties, since I need an index for
each list.

Jeff
 
C

colin

That is a little harsh; until he added the argument "(int location)" it
was
a property, albeit one with the wrong type (should have been string not
ArrayList) - so the real answer is:

"C# doesn't support named indexers; and if it did, they would probably be
with square-bracket notation to distinguish from functions."

Also - a typed collection would be preferable. If you are using 1.1, then
StringCollection - otherwise (2.0) perhaps just List<string>.

A bigger problem is possibly that this use of Insert is going to make it
very hard to update an item - i.e.

foo[0] = "abc";
foo[0] = "def";
foo[0] = "ghi";

// contains 3 items, not 1

But taking the existing example (with a standard "this" (Item) indexer):

class Foo {
StringCollection circuitNames = new StringCollection();
public string this[int location]
{
get
{
return circuitNames[location];
}
set
{
circuitNames.Insert(location, value);
}
}

}

then Foo foo = new Foo();
foo[0] = "abc";
foo[1] = "def";

etc

Hi Marc, Jon,

Unfortunately I have three of the arrayLists in this class, all three
will have to follow the same insert concept.
I am thinking I will just have to go with a standard access scheme
rather than properties, since I need an index for
each list.

Jeff

each property could return a class wich is itself indexable.

eg
class myArray
{
string[] s;
public string this[int index] {get {return s[index];}}
}
class myclass
{
myArray _array1,_array2,_array3;
myArray array1 {get {return _array1;}}
}
main()
{
myclass a;
a.array1[0]="abc";
a.array2[1]="xyz";
}

ofc this only shows the syntax

Colin =^.^=
 
H

Hoop

That is a little harsh; until he added the argument "(int location)" it
was
a property, albeit one with the wrong type (should have been string not
ArrayList) - so the real answer is:
"C# doesn't support named indexers; and if it did, they would probably be
with square-bracket notation to distinguish from functions."
Also - a typed collection would be preferable. If you are using 1.1, then
StringCollection - otherwise (2.0) perhaps just List<string>.
A bigger problem is possibly that this use of Insert is going to make it
very hard to update an item - i.e.
foo[0] = "abc";
foo[0] = "def";
foo[0] = "ghi";
// contains 3 items, not 1
But taking the existing example (with a standard "this" (Item) indexer):
class Foo {
StringCollection circuitNames = new StringCollection();
public string this[int location]
{
get
{
return circuitNames[location];
}
set
{
circuitNames.Insert(location, value);
}
}

then Foo foo = new Foo();
foo[0] = "abc";
foo[1] = "def";

Hi Marc, Jon,

Unfortunately I have three of the arrayLists in this class, all three
will have to follow the same insert concept.
I am thinking I will just have to go with a standard access scheme
rather than properties, since I need an index for
each list.

Jeff

each property could return a class wich is itself indexable.

eg
class myArray
{
    string[] s;
    public string this[int index] {get {return s[index];}}}

class myclass
{
    myArray _array1,_array2,_array3;
    myArray array1 {get {return _array1;}}}

main()
{
    myclass a;
    a.array1[0]="abc";
    a.array2[1]="xyz";

}

ofc this only shows the syntax

Colin =^.^=- Hide quoted text -

- Show quoted text -

Hi,
Thats looks pretty decent.
Thanks
Jeff
 
B

Ben Voigt [C++ MVP]

Jon said:
At that point you'd be turning it into an indexer - but you can't name
it. For instance:

Only in C# are you restricted to either arguments or a name but not both.
VB.NET and C++/CLI both support indexed properties.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top