How to acces class object that I put in arraylist: read the property

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Troela
    New Member
    • Oct 2010
    • 2

    #1

    How to acces class object that I put in arraylist: read the property

    Hello,

    I make a arraylist
    public ArrayList oaTaken = new ArrayList();

    I add the class "clstaak" to my arraylist
    oaTaken.Add(new clsTaak(iNummer ,bAfgewerkt,sDa tum,sTekst));

    Now I wanna read a property from the class that is located in oaTaken[0], i = oaTaken[0].getsetDatum;

    but I get always the following error


    Error 1 'object' does not contain a definition for 'getsetDatum' and no extension method 'getsetDatum' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\aaa\My Documents\Visua l Studio 2008\Projects\T akenlijkstV2\Ta kenlijkstV2\tak enlijst.cs 102 28 TakenlijkstV2


    Somebody got an idea?

    Thanks in advance
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    When you use the Add method of the Array list your object clsTaak is being converted(for a lack of a better word) to an Object type. This works because in C# all objects derive from Object. This is bad news because your container can have any number of different classes in it ie String, clsTaak.

    To get around this make your ArrayList strongly typed by declaring it like this

    Code:
    public List<clsTaak> oaTaken = new List<clsTaak>();
    Now only classes of type clsTaak can be added to the List.

    Now you should be able to use the getsetDatum property.

    If you need to use an ArrayList that supports multiple different classes you could do some casts, but that is considered bad form.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Not necessarily bad form, but unnecessary to say the least.

      As you said, all classes derive from Object, so an Object can hold anything. An ArrayList is the same thing as a List<Object>. It was included before the System.Collecti ons.Generics library was implemented to give us a resizable array. But now that we have generics, there is actually no reason to use them anymore.

      Anyway, the simplest way to cast your objects should you continue this way:
      Code:
      (oaTaken[0] as clsTaak).getsetDatum;
      Also, some unsolicited advice. You could name your classes and properties much more clearly. Classes should be self explanatory, if possible, and start with capital letters. What is a clsTaak? Nobody knows. But you know what a WebClient is, or an HttpWebRequest, or a StringBuilder (you see where I'm going with this, right?).

      Here's a good read on design guidelines:
      http://msdn.microsoft.com/en-us/library/ms229042.aspx

      Take a look at naming guidelines, it's nice to know the best practices.

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        The reason I say it is bad form is because if you have loaded the ArrayList with different data types, ie String, Object, clsTaak, then if you use your code...

        Code:
        (oaTaken[0] as clsTaak).getsetDatum;
        if oaTaken[0] is not of type clsTaak it will evaluate to NULL. If you do the . operator on Null it will throw a Null Reference Exception at runtime.

        If you use List<clsTaak> if anybody tries to put a string in the collection it will give a compile time error.

        I like all my potential problems to be found at compile time and not at run time.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Oh, I don't disagree with you about that. Just that "bad form" is a bit ambiguous.

          I certainly wouldn't be using an ArrayList myself. I'd actually just use the generic List, or perhaps an IEnumerable, depending on the situation. I should have been more explicit about my cast being dangerous as well. I stand corrected on that.

          Comment

          • Troela
            New Member
            • Oct 2010
            • 2

            #6
            Thank you very much for the answers and tips.

            Comment

            Working...