hi im working on a website in asp.net and i am a begginer in C#. i want to get rid of these nested IF statements ihave in the following function. can someone help me do this? thanks.

protected void btnUpload_Click(object sender, EventArgs e)  //upload file function
    {
        //Condition for fileupload
        if (Uploader.HasFile)
        {
            if (CheckFileType(Uploader.FileName))
            {
                if (Uploader.PostedFile.ContentLength > 1000 && Uploader.PostedFile.ContentLength < 10000)  //Maximum content length
                {
                    try 
                    {

                        string PathName = Server.MapPath("~/assets/uploads"); //get path name
                        string FileName = Uploader.FileName;
                        string FileSrc = Path.Combine(PathName, FileName);

                        //Disable Overwrite
                            if (File.Exists(FileSrc))   //File exists condition
                            {                           //Display Message
                            lblUploadResult.Text = "Error!: File " + FileName + " already exists. <br />";
                            lblUploadResult.ForeColor = System.Drawing.Color.White;
                            }
                            else //file does not exist condition
                            {
                            Uploader.SaveAs(FileSrc);
                            lblUploadResult.Text = "Result!: File " + FileName + " has uploaded successfully. <br />";
                            lblUploadResult.ForeColor = System.Drawing.Color.Green;
                            string[] FileInfo = { PathName, FileName, FileSrc };
                            Session["FILEINFO"] = FileInfo;
                        }
                    }
                    catch (Exception ex)    //exception trap
                    {
                        //display error message
                        lblUploadResult.Text = ex.Message;
                        lblUploadResult.ForeColor = System.Drawing.Color.Red;
                    }//catch
                }// if file size is in range
                else
                {
                    //display error message
                    lblUploadResult.Text = "Error!!!: There must be more than a 1000 characters and less that 10000 characters. <br />";
                    lblUploadResult.ForeColor = System.Drawing.Color.Red;
                }// if file size is out of range
            }//if checkfiletype returns true
            else
            {
                lblUploadResult.Text = "Error!!!: The file extentions that are allowed are: .txt, .log, .cfg, .doc, .docx. <br />";
                lblUploadResult.ForeColor = System.Drawing.Color.Red;
            }
        }//if (Uploader.HasFile)
        else
        {
            //display error message
            lblUploadResult.Text = "Error!!!: Please Upload Something. <br />";
            lblUploadResult.ForeColor = System.Drawing.Color.Red;
        }//ELSE condition for if uploader has a file
        Popuplate_DDL();
    }

One esy way is to 'invert' the if like

protected void btnUpload_Click(object sender, EventArgs e)  //upload file function
    {
        //Condition for fileupload
        if (!Uploader.HasFile)
        {
            //display error message
            lblUploadResult.Text = "Error!!!: Please Upload Something. <br />";
            lblUploadResult.ForeColor = System.Drawing.Color.Red;
            Popuplate_DDL();
            return;
        }
        if (!CheckFileType(Uploader.FileName))
        {
            lblUploadResult.Text = "Error!!!: The file extentions that are allowed are: .txt, .log, .cfg, .doc, .docx. <br />";
            lblUploadResult.ForeColor = System.Drawing.Color.Red;
            Popuplate_DDL();
            return;
       }
       if (Uploader.PostedFile.ContentLength <= 1000 || Uploader.PostedFile.ContentLength >= 10000)  //Maximum content length
       {
            //display error message
            lblUploadResult.Text = "Error!!!: There must be more than a 1000 characters and less that 10000 characters. <br />";
            lblUploadResult.ForeColor = System.Drawing.Color.Red;
            Popuplate_DDL();
            return;
       }// if file size is out of range
       try 
       {
            string PathName = Server.MapPath("~/assets/uploads"); //get path name
            string FileName = Uploader.FileName;
            string FileSrc = Path.Combine(PathName, FileName);

            //Disable Overwrite
            if (File.Exists(FileSrc)) 
            { 
                //Display Message
                lblUploadResult.Text = "Error!: File " + FileName + " already exists. <br />";
                lblUploadResult.ForeColor = System.Drawing.Color.White;
                Popuplate_DDL();
                return;
            }
            Uploader.SaveAs(FileSrc);
            lblUploadResult.Text = "Result!: File " + FileName + " has uploaded successfully. <br />";
            lblUploadResult.ForeColor = System.Drawing.Color.Green;
            string[] FileInfo = { PathName, FileName, FileSrc };
            Session["FILEINFO"] = FileInfo;
       }
       catch (Exception ex)    //exception trap
       {
            //display error message
            lblUploadResult.Text = ex.Message;
            lblUploadResult.ForeColor = System.Drawing.Color.Red;
       }//catch
        Popuplate_DDL();
    }

I did not tested if I miss some thing, but there is the idea on how to.

JetBrains Resharper is an utility I use that helps a lot revamp your code.

Hope this helps

Edited 5 Years Ago by lolafuertes: n/a

This article has been dead for over six months. Start a new discussion instead.