Class Casting with ArrayList ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cybertof

    #1

    Class Casting with ArrayList ?

    Hello,

    In the code below, is there a shortcut not to use the temp var
    CurrEmployee ?

    I would like to use something like
    "if (Employee)MyArr ayList.Name"
    but don't know the syntax....


    *******
    CODE : (MyArrayList is a list of Employee)
    *******
    Employee CurrEmployee;
    for (int i = 0;i<MyArrayList .Length;i++)
    {

    CurrEmployee = MyArrayList[i];
    if (CurrEmplyee.Na me == "test")
    {
    }
    }


    Regards,
    Cybertof.
  • Daniel Bass

    #2
    Re: Class Casting with ArrayList ?

    I don't think you can do this in the way that you mean.

    The only other option to the one you already have is to have a static method
    in your Employee class called

    Convert(object obj)

    whihc returned an Employee object.

    so you'd end up with something like

    for (int i = 0;i<MyArrayList .Length;i++)
    {
    if ( Employee.Conver t( MyArrayList[i] ).Name == "test" )
    {
    }
    }

    hope that helps.

    Dan.

    "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
    news:MPG.19ef6b 1c147f24b398969 f@msnews.micros oft.com...
    Hello,

    In the code below, is there a shortcut not to use the temp var
    CurrEmployee ?

    I would like to use something like
    "if (Employee)MyArr ayList.Name"
    but don't know the syntax....


    *******
    CODE : (MyArrayList is a list of Employee)
    *******
    Employee CurrEmployee;
    for (int i = 0;i<MyArrayList .Length;i++)
    {

    CurrEmployee = MyArrayList[i];
    if (CurrEmplyee.Na me == "test")
    {
    }
    }


    Regards,
    Cybertof.


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Class Casting with ArrayList ?

      Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
      > In the code below, is there a shortcut not to use the temp var
      > CurrEmployee ?
      >
      > I would like to use something like
      > "if (Employee)MyArr ayList.Name"
      > but don't know the syntax....[/color]
      [color=blue]
      > *******
      > CODE : (MyArrayList is a list of Employee)
      > *******
      > Employee CurrEmployee;
      > for (int i = 0;i<MyArrayList .Length;i++)
      > {
      >
      > CurrEmployee = MyArrayList[i];
      > if (CurrEmplyee.Na me == "test")
      > {
      > }
      > }[/color]

      Well, you could improve the code above by declaring the variable inside
      the loop:

      for (int i=0; i < MyArrayList.Len gth; i++)
      {
      Employee CurrEmployee = (Employee) MyArrayList[i];
      if (CurrEmployee.N ame=="test")
      {
      }
      }

      or, for preference, you could use a foreach loop:

      foreach (Employee CurrEmployee in MyArrayList)
      {
      if (CurrEmployee.N ame=="test")
      {
      }
      }

      You *could* cast directly, but I wouldn't recommend it (as I reckon the
      above is more readable):

      for (int i=0; i < MyArrayList.Len gth; i++)
      {
      if (((Employee)(My ArrayList[i])).Name=="test" )
      {
      }
      }

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Cybertof

        #4
        Re: Class Casting with ArrayList ?

        Daniel & Jon,

        Thanks a lot for your advises.

        The newsgroup is a great knowledge place !

        Regards,
        Cybertof.

        Comment

        • Daniel Bass

          #5
          Re: Class Casting with ArrayList ?

          Jon,

          could you give me your opinion on the question I asked today two hours
          before this one was posted. ("string or stream")

          thanks.
          Dan.

          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.19ef60 dcdbb506c698983 6@msnews.micros oft.com...
          Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
          > In the code below, is there a shortcut not to use the temp var
          > CurrEmployee ?
          >
          > I would like to use something like
          > "if (Employee)MyArr ayList.Name"
          > but don't know the syntax....[/color]
          [color=blue]
          > *******
          > CODE : (MyArrayList is a list of Employee)
          > *******
          > Employee CurrEmployee;
          > for (int i = 0;i<MyArrayList .Length;i++)
          > {
          >
          > CurrEmployee = MyArrayList[i];
          > if (CurrEmplyee.Na me == "test")
          > {
          > }
          > }[/color]

          Well, you could improve the code above by declaring the variable inside
          the loop:

          for (int i=0; i < MyArrayList.Len gth; i++)
          {
          Employee CurrEmployee = (Employee) MyArrayList[i];
          if (CurrEmployee.N ame=="test")
          {
          }
          }

          or, for preference, you could use a foreach loop:

          foreach (Employee CurrEmployee in MyArrayList)
          {
          if (CurrEmployee.N ame=="test")
          {
          }
          }

          You *could* cast directly, but I wouldn't recommend it (as I reckon the
          above is more readable):

          for (int i=0; i < MyArrayList.Len gth; i++)
          {
          if (((Employee)(My ArrayList[i])).Name=="test" )
          {
          }
          }

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too


          Comment

          • Cybertof

            #6
            Re: Class Casting with ArrayList ?

            In your 3 versions, which is the best one regarding 'performance' ?
            I mean :less memory consumming, less object creation, fastest speed...


            Thanks,
            Cybertof.



            In article <MPG.19ef60dcdb b506c6989836@ms news.microsoft. com>,
            skeet@pobox.com says...[color=blue]
            > Well, you could improve the code above by declaring the variable inside
            > the loop:
            >
            > for (int i=0; i < MyArrayList.Len gth; i++)
            > {
            > Employee CurrEmployee = (Employee) MyArrayList[i];
            > if (CurrEmployee.N ame=="test")
            > {
            > }
            > }
            >
            > or, for preference, you could use a foreach loop:
            >
            > foreach (Employee CurrEmployee in MyArrayList)
            > {
            > if (CurrEmployee.N ame=="test")
            > {
            > }
            > }
            >
            > You *could* cast directly, but I wouldn't recommend it (as I reckon the
            > above is more readable):
            >
            > for (int i=0; i < MyArrayList.Len gth; i++)
            > {
            > if (((Employee)(My ArrayList[i])).Name=="test" )
            > {
            > }
            > }[/color]

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Class Casting with ArrayList ?

              Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
              > In your 3 versions, which is the best one regarding 'performance' ?
              > I mean :less memory consumming, less object creation, fastest speed...[/color]

              They're likely to be pretty much identical - but there's an important
              thing to note here, which is that you shouldn't worry about that kind
              of thing unless it proves to be an issue. Readability is far more
              important than performance for most code - chances are it's only going
              to be a tiny part of your code which ends up as the bottleneck, so only
              think about sacrificing readability for performance when you know
              exactly where that is.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Guinness Mann

                #8
                Re: Class Casting with ArrayList ?

                In article <MPG.19ef9be722 677cf998983f@ms news.microsoft. com>,
                skeet@pobox.com says...[color=blue]
                > ... but there's an important thing to note here,
                > which is that you shouldn't worry about that kind
                > of thing unless it proves to be an issue.
                > Readability is far more important than performance
                > for most code - chances are it's only going to be
                > a tiny part of your code which ends up as the
                > bottleneck, so only think about sacrificing
                > readability for performance when you know
                > exactly where that is.[/color]


                Hear! Hear!

                -- Rick

                Comment

                Working...