C# string array
-
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?
-
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?
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 -
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?
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[^]
-
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?
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)
{
}}
-
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)
{
}}
-
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?