Click here to Skip to main content
15,903,385 members
Home / Discussions / C#
   

C#

 
AnswerRe: DataBinding issue Pin
darkelv9-Mar-10 4:06
darkelv9-Mar-10 4:06 
GeneralRe: DataBinding issue Pin
GDavy9-Mar-10 20:06
GDavy9-Mar-10 20:06 
QuestionC# database connection Pin
2010JohnFByers9-Mar-10 3:15
2010JohnFByers9-Mar-10 3:15 
AnswerRe: C# database connection Pin
PIEBALDconsult9-Mar-10 3:31
mvePIEBALDconsult9-Mar-10 3:31 
AnswerRe: C# database connection Pin
R. Giskard Reventlov9-Mar-10 4:07
R. Giskard Reventlov9-Mar-10 4:07 
GeneralRe: C# database connection Pin
Mirko19809-Mar-10 6:16
Mirko19809-Mar-10 6:16 
GeneralRe: C# database connection Pin
2010JohnFByers10-Mar-10 5:36
2010JohnFByers10-Mar-10 5:36 
Questionbeginner question: foreach .. else? Pin
Ted On The Net9-Mar-10 2:02
Ted On The Net9-Mar-10 2:02 
hi,

I'm just learning C# after being a Delphi coder for waayyy to long Big Grin | :-D
I was wondering, why isn't it possible to use an else statement with the foreach statement?

Check out my quick and dirty demo code:
namespace ConsoleApplication
{
    class Product
    {
        public string Code { get; set; }
        public string Description { get; set; }

    }

    class Program
    {
        static void Main(string[] args)
        {
            //create some enumerable list
            IEnumerable<Product> myProductList = new List<Product>
                                         {
                                             new Product() {Code = "CDR", Description = "CD-Rom"},
                                             new Product() {Code = "DVD", Description = "DVD-Rom"},
                                             new Product() {Code = "BRD", Description = "BlueRay Disc"},
                                         };

            //select some products with LINQ
            var myProductSelection = (from aProduct in myProductList where aProduct.Code.StartsWith("R") select aProduct);

            //Print header text
            Console.WriteLine("The products that were found:");

            //loop through products in selection
            foreach (var aProduct in myProductSelection)
            {
                Console.WriteLine("Found product: " + aProduct.Code);
            }
          
            //pause to check results
            Console.ReadKey();            
        }
    }
}

The above code returns nothing, since none of the product codes start with a "R".

Would be nice if I could do this when my result is empty:
//Print header text
Console.WriteLine("The products that were found:");

//loop through products in selection
foreach (var aProduct in myProductSelection)
{
    Console.WriteLine("Found product: " + aProduct.Code);
}
else
{
    Console.WriteLine("No products were found!");
}


Just some ranting.. any good solutions to this unlike something like this?
//Print header text
Console.WriteLine("The products that were found:");

int x = 0;
//loop through products in selection
foreach (var aProduct in myProductSelection)
{
    x++;
    Console.WriteLine("Found product: " + aProduct.Code);
}

if (x > 0)
  Console.WriteLine("No products were found!");


thx for any suggestions offered Smile | :)
- Life would be so much easier if I had the source code!
- If C# had true garbage collection, most applications would delete themselves upon execution Wink | ;)

AnswerRe: beginner question: foreach .. else? Pin
harold aptroot9-Mar-10 2:07
harold aptroot9-Mar-10 2:07 
AnswerRe: beginner question: foreach .. else? Pin
kevinnicol9-Mar-10 2:19
kevinnicol9-Mar-10 2:19 
GeneralRe: beginner question: foreach .. else? Pin
Ted On The Net9-Mar-10 3:24
Ted On The Net9-Mar-10 3:24 
GeneralRe: beginner question: foreach .. else? Pin
riced9-Mar-10 11:35
riced9-Mar-10 11:35 
AnswerRe: beginner question: foreach .. else? [modified] Pin
Paulo Zemek9-Mar-10 3:28
Paulo Zemek9-Mar-10 3:28 
GeneralRe: beginner question: foreach .. else? Pin
harold aptroot9-Mar-10 4:19
harold aptroot9-Mar-10 4:19 
AnswerRe: beginner question: foreach .. else? Pin
StarBP10-Mar-10 10:05
StarBP10-Mar-10 10:05 
QuestionFind in memory string another process Pin
pawel_19809-Mar-10 2:00
pawel_19809-Mar-10 2:00 
GeneralRe: Find in memory string another process Pin
harold aptroot9-Mar-10 2:09
harold aptroot9-Mar-10 2:09 
GeneralRe: Find in memory string another process Pin
pawel_19809-Mar-10 2:23
pawel_19809-Mar-10 2:23 
GeneralRe: Find in memory string another process Pin
harold aptroot9-Mar-10 2:41
harold aptroot9-Mar-10 2:41 
GeneralRe: Find in memory string another process Pin
pawel_19809-Mar-10 3:33
pawel_19809-Mar-10 3:33 
QuestionNeed to try casting it, or declaring tmp as double and then parsing double values into it later on Pin
suprsnipes9-Mar-10 1:22
suprsnipes9-Mar-10 1:22 
AnswerRe: Need to try casting it, or declaring tmp as double and then parsing double values into it later on Pin
Luc Pattyn9-Mar-10 1:37
sitebuilderLuc Pattyn9-Mar-10 1:37 
GeneralRe: Need to try casting it, or declaring tmp as double and then parsing double values into it later on Pin
suprsnipes9-Mar-10 2:24
suprsnipes9-Mar-10 2:24 
GeneralRe: Need to try casting it, or declaring tmp as double and then parsing double values into it later on Pin
Luc Pattyn9-Mar-10 2:28
sitebuilderLuc Pattyn9-Mar-10 2:28 
GeneralRe: Need to try casting it, or declaring tmp as double and then parsing double values into it later on Pin
suprsnipes9-Mar-10 3:07
suprsnipes9-Mar-10 3:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.