Click here to Skip to main content
14,743,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get the error mentioned in the title. Reason: Array need to return 3 elements but those elements are not being passed to the array. I don't know why. Here is the code then I will ask a direct question:
public void barcodereader()
    {
        string str = string.Empty;
         string strImage = string.Empty;
         string strBarCode = string.Empty;
        DirectoryInfo foldersInsideDropHere = new DirectoryInfo(@"C:\Users\name\Desktop\images\DropHere");
        DirectoryInfo[] foldersInsideDropHereSave = foldersInsideDropHere.GetDirectories();
        foreach (DirectoryInfo Folder in foldersInsideDropHereSave)
        {
            string dropHereFolders = Folder.Name;
            DirectoryInfo dir = new DirectoryInfo(Path.Combine(@"C:\Users\name\Desktop\images\DropHere\", dropHereFolders));
             string fulldirectory = dir.FullName;
            FileInfo[] dropHereSubDirectoriesfiles = dir.GetFiles();
            foreach (FileInfo dropHereSubDirectoriesfile in dropHereSubDirectoriesfiles)
            {
                if (dir != null)
                {
                    Bitmap bitmap = null;
                    try
                    {
                        bitmap = new Bitmap(dropHereSubDirectoriesfile.FullName);
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                    if (bitmap == null)//If file is not image return error message to use "your folder contains non image files please remove and retry"

                    {
                        str = "Your folder contains non image files please remove and retry";
                    }
                    else
                    {
                        strImage = "something";
                        strBarCode = ReadBarcodeFromFile(Path.Combine(fulldirectory, dropHereSubDirectoriesfile.Name));
                    }
                }
            }
        }
    }
    public String ReadBarcodeFromFile(string _Filepath)
    {

            String[] barcodes = BarcodeScanner.Scan(_Filepath, BarcodeType.Code39);//Issue is here this array stays 0
            return barcodes[0];// error here


    }


This is a method that I call from controller in the controller I have:
[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(string str, string strBarCode, string strImage)
        {
            

            Methods Method0 = new Methods();
            Method0.barcodereader();
          
            ViewBag.ErrorMessage = str;
            ViewBag.BarCode = strBarCode;
            ViewBag.BarImage = strImage;
            return View();
            
        }


Question: How do I pass those elements or what am i doing wrong.

What I have tried:

I tried debugging to find the issue and more information about it.
Posted
Updated 13-Jan-20 4:00am
v2

We can't tell - it takes your code running and with you actual data to diagnose problems like this.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Comments
AskalotLearnalot 13-Jan-20 8:52am
   
Thanks for the advice but that is how I knew what the issue is and whats needs to be done to avoid the error. I just didn't know how to implement the solution.
The barcode scanner looks for barcodes in this case if it finds the barcode it returns the 3 required elements. However, if it doesn't it returns empty array. Therefor all we need to do is add an if else statment.
public String ReadBarcodeFromFile(string _Filepath)
    {
if(barcodes.Length!= 0)
{
            String[] barcodes = BarcodeScanner.Scan(_Filepath, BarcodeType.Code39);//Issue is here this array stays 0
            return barcodes[0];// error here
}
else
{
return false;
}

    }

Something within those lines or however you want the if else to preform/do. hope this helps someone.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900