Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# string array

C# string array

Scheduled Pinned Locked Moved C#
questioncsharpdata-structures
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sc steinhayse
    wrote on last edited by
    #1

    I have a question about the C# 2008 statement listed below:

    string[] PkgIDs = rData.details.Where(c => c.batch_id == batchID).Select(c => c.Package_ID).Distinct().ToArray();
    foreach (string PkgID in PkgIDs)
    {
    }

    I would want to make certain an exception would not be generated in this situation. If there is nothing selected for the PKgIDs [] table, do I need to to any check for this condition? If so, what would the check be? Would you show me what code I would need to add?

    D L M B 4 Replies Last reply
    0
    • S sc steinhayse

      I have a question about the C# 2008 statement listed below:

      string[] PkgIDs = rData.details.Where(c => c.batch_id == batchID).Select(c => c.Package_ID).Distinct().ToArray();
      foreach (string PkgID in PkgIDs)
      {
      }

      I would want to make certain an exception would not be generated in this situation. If there is nothing selected for the PKgIDs [] table, do I need to to any check for this condition? If so, what would the check be? Would you show me what code I would need to add?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Based on what you've posted so far, there is no exception possible. But, since we can't see the code that declared these types, the details collection, how it's populated, ..., we really couldn't tell you for sure. But, if the Select returns no ID's, the array will come back empty and the foreach iterating over it won't do anything. You don't have to change this code at all. Next time, you might want to try testing the code with test data specifically designed to try and break it.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • S sc steinhayse

        I have a question about the C# 2008 statement listed below:

        string[] PkgIDs = rData.details.Where(c => c.batch_id == batchID).Select(c => c.Package_ID).Distinct().ToArray();
        foreach (string PkgID in PkgIDs)
        {
        }

        I would want to make certain an exception would not be generated in this situation. If there is nothing selected for the PKgIDs [] table, do I need to to any check for this condition? If so, what would the check be? Would you show me what code I would need to add?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        sc steinhayse wrote:

        I would want to make certain an exception would not be generated in this situation

        Look up each command in the documentation, see if they CAN throw an exception. For example, the Distinct[^] method does not throw anything, but ToArray[^] might throw an ArgumentNullException.

        sc steinhayse wrote:

        Would you show me what code I would need to add?

        None, since you can't handle the exception locally (in this example). Have it propagate to the general exception-handler, and log it there.

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        1 Reply Last reply
        0
        • S sc steinhayse

          I have a question about the C# 2008 statement listed below:

          string[] PkgIDs = rData.details.Where(c => c.batch_id == batchID).Select(c => c.Package_ID).Distinct().ToArray();
          foreach (string PkgID in PkgIDs)
          {
          }

          I would want to make certain an exception would not be generated in this situation. If there is nothing selected for the PKgIDs [] table, do I need to to any check for this condition? If so, what would the check be? Would you show me what code I would need to add?

          M Offline
          M Offline
          montevina 2
          wrote on last edited by
          #4

          string[] PkgIDs = rData.details.Where(c => c.batch_id == batchID).Select(c => c.Package_ID).Distinct().ToArray();
          if(PkgIDs.Count > 0)//This will check the number of elements in the List
          {
          foreach (string PkgID in PkgIDs)
          {
          }

          }

          B 1 Reply Last reply
          0
          • M montevina 2

            string[] PkgIDs = rData.details.Where(c => c.batch_id == batchID).Select(c => c.Package_ID).Distinct().ToArray();
            if(PkgIDs.Count > 0)//This will check the number of elements in the List
            {
            foreach (string PkgID in PkgIDs)
            {
            }

            }

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            Totally unnecessary, foreach can iterate over a zero size collection without any problem.

            1 Reply Last reply
            0
            • S sc steinhayse

              I have a question about the C# 2008 statement listed below:

              string[] PkgIDs = rData.details.Where(c => c.batch_id == batchID).Select(c => c.Package_ID).Distinct().ToArray();
              foreach (string PkgID in PkgIDs)
              {
              }

              I would want to make certain an exception would not be generated in this situation. If there is nothing selected for the PKgIDs [] table, do I need to to any check for this condition? If so, what would the check be? Would you show me what code I would need to add?

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              Write unit tests for the situations you are worried about and see if the result is correct?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups