Solved

If Else block not working correctly

Posted on 2007-03-30
14
236 Views
Last Modified: 2010-04-16
Hi
Can anyone tell me why this if else block fails to work correctly.
It supposed to work only if the image is jpg or gif, if i enter a png etc it fails to work and sends the attachments.

protected void Button1_Click(object sender, EventArgs e)
        {
        //string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        string Img1, Img2, Img3;
        Img1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        Img2 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
        Img3 = System.IO.Path.GetExtension(FileUpload3.PostedFile.FileName);
        Page.Validate();
        if (Page.IsValid)
            {

            if (!(Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" || Img2.ToLower() == ".jpg" || Img2.ToLower() == ".gif" || Img3.ToLower() == ".jpg" || Img3.ToLower() == ".gif" || Img1 == null || Img2 == null || Img3 == null))
                {
                Response.Write(Img1 + " " + Img2 + " " + Img3);
                }
            else
                {
                Response.Write("Sent");
                }
            }
        }
    }
0
Comment
Question by:LearningCSharpSQL
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 18822210
Do you get an error message or anything?
0
 

Author Comment

by:LearningCSharpSQL
ID: 18822247
Hi angus
No it just sends the attachments
George
0
 
LVL 3

Accepted Solution

by:
Nico earned 250 total points
ID: 18822286
I suppose if any of the conditiions in the if statement are true, then the if statement itself would evaluate to false. if (not  (true or false or false or false)= true ) = false

I would rather change your if statement to check conditions for each group:

if( (Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" || Img1.ToLower() != null) && (Img2.ToLower() == ".jpg" || Img2.ToLower() == ".gif" || Img2.ToLower() != null) && (Img3.ToLower() == ".jpg" || Img3.ToLower() == ".gif" || Img3.ToLower() != null) )

In this case Img1, Img2, AND Img3 have to be a jpg or gif and may not be null.

I recommend putting the checking of conditions in a seperate file btw, this is not a very elegant if statement -> consider something like: if(ValidFile(Img1) && ValidFile(Img2) && ValidFile(Img3) ) etc.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 3

Expert Comment

by:Nico
ID: 18822319
Whipped up a little example:

if(ValidFile(Img1) && ValidFile(Img1) && ValidFile(Img1))  {
    // send stuff
}
else {
    // don't send stuff
}
                        
private bool ValidFile(string extension) {
// GetExtension will return empty string when file doesn't have an extension and will return null when the input path is also null      
      // so if the extension is not empty or null AND has jpg or gif as extension this method will return true, in any other case will return false
      if((extension != "" || extension != null) && (extension.ToLower == ".jpg" || extension.ToLower == ".gif") {
            return true;
      }
      return false;
}
0
 
LVL 3

Expert Comment

by:Nico
ID: 18822320
(sorry for the bad formatting, I accidently sent the page before I finished editing 8-) )
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18822368
write code more structured:

protected void Button1_Click(object sender, EventArgs e)
        {
        //string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        string Img1, Img2, Img3;
        Img1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        Img2 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
        Img3 = System.IO.Path.GetExtension(FileUpload3.PostedFile.FileName);
        Page.Validate();
        if (Page.IsValid)
        {
           bool do_send = true;
           ArrayList ex = new ArrayList ();
           ex.Add(".jpg");
           ex.Add(".gif");


           if ( Img1 == null || Img2 == null || Img3 == null )
           {  do_send = false; }

           if (do_send)
           {
               do_send = (ex.FindByValue(Img1.ToLower()) == null)
           }
           if (do_send)
           {
               do_send = (ex.FindByValue(Img2.ToLower()) == null)
           }
           if (do_send)
           {
               do_send = (ex.FindByValue(Img3.ToLower()) == null)
           }

            if (do_send)
            {
                Response.Write("Sent");
            }
            else
            {
               Response.Write(Img1 + " " + Img2 + " " + Img3);
            }
        }
    }
0
 

Author Comment

by:LearningCSharpSQL
ID: 18822901
Hi AngellIII
I get the following error:
Error      2      'System.Collections.ArrayList' does not contain a definition for 'FindByValue'      \test.aspx.cs      118

Hi derkesthai:
I'm unable to get your code to work, still sends png attachments
George      
0
 
LVL 3

Expert Comment

by:Nico
ID: 18823110
This code is doing exactly what I think your code should do....

public Form1()
{
    InitializeComponent();

    string Img1 = ".jpg";
    string Img2 = ".gif";
    string Img3 = ".png";
    if (ValidFile(Img1) && ValidFile(Img2) && ValidFile(Img3))
    {
        MessageBox.Show("Files valid..");      
    }
    else
    {
        MessageBox.Show("One of the files has an invalid extension!");
    }
}

private bool ValidFile(string extension)
{
    if ((extension != "" || extension != null) && (extension.ToLower() == ".jpg" || extension.ToLower() == ".gif"))
    {
        return true;
    }
    return false;
}
0
 

Author Comment

by:LearningCSharpSQL
ID: 18823213
Hi derkesthai:
Being working with your example and changed it slightly to below and seems to work.
if ((Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" && Img1.ToLower() != null) && (Img2.ToLower() == ".jpg" || Img2.ToLower() == ".gif" && Img2.ToLower() != null) && (Img3.ToLower() == ".jpg" || Img3.ToLower() == ".gif" && Img1.ToLower() != null))

Not sure why changing the || to && when checking for null value made a differance
if ((Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" || Img1.ToLower() != null) && (Img2.ToLower() == ".jpg" || Img2.ToLower() == ".gif" || Img2.ToLower() != null) && (Img3.ToLower() == ".jpg" || Img3.ToLower() == ".gif" || Img3.ToLower() != null))
Regards
George
0
 
LVL 3

Expert Comment

by:Nico
ID: 18823340
for instance: test.jpg

Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" && Img1.ToLower() != null
true OR false AND NOT null   -> only evaluates to true when img isn't null and is a jpg or gif

Img1.ToLower() == ".jpg" || Img1.ToLower() == ".gif" || Img1.ToLower() == null
true OR false OR null   -> evaluates to true when its a gif, or jpg and also when it's null

And actually, the GetExtension method only returns null when you put null in the GetExtension method. When a file doesn't have an extension then GetExtension returns an empty string.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18823357
sorry, use contains instead of FindByValue ...


protected void Button1_Click(object sender, EventArgs e)
        {
        //string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        string Img1, Img2, Img3;
        Img1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        Img2 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
        Img3 = System.IO.Path.GetExtension(FileUpload3.PostedFile.FileName);
        Page.Validate();
        if (Page.IsValid)
        {
           bool do_send = true;
           ArrayList ex = new ArrayList ();
           ex.Add(".jpg");
           ex.Add(".gif");


           if ( Img1 == null || Img2 == null || Img3 == null )
           {  do_send = false; }

           if (do_send)
           {
               do_send = (ex.Contains(Img1.ToLower()) == null)
           }
           if (do_send)
           {
               do_send = (ex.Contains(Img2.ToLower()) == null)
           }
           if (do_send)
           {
               do_send = (ex.Contains(Img3.ToLower()) == null)
           }

            if (do_send)
            {
                Response.Write("Sent");
            }
            else
            {
               Response.Write(Img1 + " " + Img2 + " " + Img3);
            }
        }
    }


now, just as an explanation as why yours does not work:

you need to FIRST check against the NULL, and then only against a non-null value.
0
 
LVL 3

Expert Comment

by:Nico
ID: 18823475
angelll: ArrayList.Contains() returns a boolean, not the contained object...
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18823510
thanks derkesthai for the tip:

protected void Button1_Click(object sender, EventArgs e)
        {
        //string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        string Img1, Img2, Img3;
        Img1 = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
        Img2 = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName);
        Img3 = System.IO.Path.GetExtension(FileUpload3.PostedFile.FileName);
        Page.Validate();
        if (Page.IsValid)
        {
           bool do_send = true;
           ArrayList ex = new ArrayList ();
           ex.Add(".jpg");
           ex.Add(".gif");


           if ( Img1 == null || Img2 == null || Img3 == null )
           {  do_send = false; }

           if (do_send)
           {
               do_send = !ex.Contains(Img1.ToLower())
           }
           if (do_send)
           {
               do_send = !ex.Contains(Img2.ToLower())
           }
           if (do_send)
           {
               do_send = !ex.Contains(Img3.ToLower())
           }

            if (do_send)
            {
                Response.Write("Sent");
            }
            else
            {
               Response.Write(Img1 + " " + Img2 + " " + Img3);
            }
        }
    }

0
 

Author Comment

by:LearningCSharpSQL
ID: 18823559
Hi
Thanks for the help, as both of you helped with the solution and both work i've split the points.
Thanks
George
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question