Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

ArrayList Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hoop - 31 Mar 2008 15:05 GMT
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()?
not_a_commie - 31 Mar 2008 15:28 GMT
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
[quoted text clipped - 22 lines]
> How to I correcly create a propery to insert and retrive strings from
> an arrayList()?
Hoop - 31 Mar 2008 15:35 GMT
> I think you should start with some books on C#. Get and Set don't
> belong in function declarations.
[quoted text clipped - 27 lines]
>
> - 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
Jon Skeet [C# MVP] - 31 Mar 2008 15:41 GMT
<snip>

> As I mentioned in my post, this a property, not a function, it was,
>
[quoted text clipped - 4 lines]
>         }
> 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
Ben Voigt [C++ MVP] - 31 Mar 2008 23:05 GMT
> <snip>
>
[quoted text clipped - 10 lines]
> 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.
Marc Gravell - 31 Mar 2008 15:41 GMT
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
Hoop - 31 Mar 2008 15:56 GMT
> 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
[quoted text clipped - 38 lines]
>
> 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
colin - 31 Mar 2008 16:19 GMT
On Mar 31, 9:41 am, "Marc Gravell" <marc.grav...@gmail.com> wrote:
> That is a little harsh; until he added the argument "(int location)" it
> was
[quoted text clipped - 39 lines]
>
> 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 =^.^=
Hoop - 31 Mar 2008 17:03 GMT
> On Mar 31, 9:41 am, "Marc Gravell" <marc.grav...@gmail.com> wrote:
>
[quoted text clipped - 78 lines]
>
> - Show quoted text -

Hi,
Thats looks pretty decent.
Thanks
Jeff

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.