Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a method that works fine it gets values of bar codes here is the method:
public Array ReadBarcodeFromFile(string _Filepath, string strTopBarCode, string strMiddleBarCode, string strBottomBarCode)
        {
            String[] barcodes = BarcodeScanner.Scan(_Filepath, BarcodeType.Code39);
            if (barcodes.Length != 0)
            {
                strTopBarCode = barcodes[0];
                strMiddleBarCode = barcodes[1];
                strBottomBarCode = barcodes[2];
                return barcodes;
            }

            return null; 
        }

when I call it. the values of the parameters change from null to the correct values. Also before I reach the end of the method, If I check the values of the Parameters in the other method that i want to pass values to I still get the correct value.
However, when I reach the end of the method those values change to null.
Here is my other method:
public ActionResult Index(string strTopBarCode, string strMiddleBarCode, string strBottomBarCode)
        {
            string barcodeTopParent = "something1";
                    string barcodeMiddleParent = "something2";
                    string barcodeBottomParent = "something3";
                      
                        helper.ReadBarcodeFromFile(file.FullName, strTopBarCode, strMiddleBarCode, strBottomBarCode);
                          //strTopBarCode strMiddleBarCode strBottomBarCode always become null even though the method doesn't pass null
                        if (strTopBarCode == barcodeTopParent || strMiddleBarCode == barcodeMiddleParent || strBottomBarCode == barcodeBottomParent)
                        {
                           
                          //do something
                        }

I think its a return issue and it is something simple due to lack of experience. Thank you in advance.

What I have tried:

the code above is what I tried. I also tried method type string that return only one element and append it to a value in the action result that works:
string oneBarcode = helper.ReadBarcodeFromFile(file.FullName)

Method:
public Array ReadBarcodeFromFile(string _Filepath)
        {
            String[] barcodes = BarcodeScanner.Scan(_Filepath, BarcodeType.Code39);
            if (barcodes.Length != 0)
            {
                return barcodes[0];
               }             
            return null; 
        }
Posted
Updated 15-Jan-20 3:26am
v2

You are not capturing the return values from the call to ReadBarcodeFromFile, and the extra three parameters are never used.
C#
helper.ReadBarcodeFromFile(file.FullName, strTopBarCode, strMiddleBarCode, strBottomBarCode);

The code should be
C#
Array barcodes = helper.ReadBarcodeFromFile(file.FullName) // -> not used, strTopBarCode, strMiddleBarCode, strBottomBarCode);
if (barcodes != null)
{
    // process the barcodes
}


The ReadBarcodeFromFile method should be
C#
public Array ReadBarcodeFromFile(string _Filepath)
{
    String[] barcodes = BarcodeScanner.Scan(_Filepath, BarcodeType.Code39);
    if (barcodes.Length != 0)
    {
        return barcodes;
    }
    
    return null; 
}
 
Share this answer
 
v2
Comments
AskalotLearnalot 15-Jan-20 9:31am    
Thank you for your help. However this approach resulted in same issue.
Richard MacCutchan 15-Jan-20 11:41am    
Then you must be doing something wrong. But since you have not shown your code I cannot guess what that may be.
By default, parameters are passed by value. That means that any changes made to the value of the parameters within the method will not be visible to the calling method.

Passing Parameters - C# Programming Guide | Microsoft Docs[^]
Parameter passing in C#[^]

If you want the changes to be visible to the calling method, then you need to pass the parameters by reference. You do this by using either the ref or out modifiers.

ref keyword - C# Reference | Microsoft Docs[^]
out parameter modifier - C# Reference | Microsoft Docs[^]

For example:
C#
public ActionResult Index()
{
    const string barcodeTopParent = "DT-COMPSTART";
    const string barcodeMiddleParent = "DB-TEST";
    const string barcodeBottomParent = "SP-SLD";
    
    string strTopBarCode, strMiddleBarCode, strBottomBarCode;
    helper.ReadBarcodeFromFile(file.FullName, out strTopBarCode, out strMiddleBarCode, out strBottomBarCode);
    if (strTopBarCode == barcodeTopParent || strMiddleBarCode == barcodeMiddleParent || strBottomBarCode == barcodeBottomParent)
    {
        ...
    }
}

public void ReadBarcodeFromFile(string _Filepath, out string strTopBarCode, out string strMiddleBarCode, out string strBottomBarCode)
{
    string[] barcodes = BarcodeScanner.Scan(_Filepath, BarcodeType.Code39);
    if (barcodes.Length != 0)
    {
        strTopBarCode = barcodes[0];
        strMiddleBarCode = barcodes[1];
        strBottomBarCode = barcodes[2];
    }
    else
    {
        strTopBarCode = null;
        strMiddleBarCode = null;
        strBottomBarCode = null;
    }
}

NB: There are some cases where you are not allowed to pass by reference - particularly async methods and iterator methods.

Passing by reference is also often considered a "code smell", and should be avoided wherever possible. You could either create a specific type to hold multiple return values, or return a ValueTuple instead.

Tuple types - C# Guide | Microsoft Docs[^]
C#
public ActionResult Index()
{
    const string barcodeTopParent = "DT-COMPSTART";
    const string barcodeMiddleParent = "DB-TEST";
    const string barcodeBottomParent = "SP-SLD";
    
    var barcodes = helper.ReadBarcodeFromFile(file.FullName);
    if (barcodes.topBarCode == barcodeTopParent || barcodes.middleBarCode == barcodeMiddleParent || barcodes.bottomBarCode == barcodeBottomParent)
    {
        ...
    }
}

public (string topBarCode, string middleBarCode, string bottomBarCode) ReadBarcodeFromFile(string _Filepath)
{
    string[] barcodes = BarcodeScanner.Scan(_Filepath, BarcodeType.Code39);
    if (barcodes.Length != 0) return (barcodes[0], barcodes[1], barcodes[2]);
    return default;
}
 
Share this answer
 
Comments
AskalotLearnalot 15-Jan-20 9:35am    
Hi thank you for your time and explanation. The second suggestion resulted in same error. However, the first one solved the issue. In your experience does "code smell" have side affects.
Richard Deeming 15-Jan-20 9:40am    
What do you mean by "the same error"? The second option will only return null values if the Scan method didn't return any barcodes.

A "code smell" is something which isn't necessarily wrong, but could indicate a deeper problem. In this case, your method wants to return multiple values; there are better ways to do that than passing by reference; and the alternatives result in code which is clearer and easier to understand.
AskalotLearnalot 15-Jan-20 9:46am    
Hi thanks for the replay in bothe cases I ran the same folder that contains bar codes so it is not scan method issue.
Richard Deeming 15-Jan-20 9:48am    
Try stepping through the ReadBarcodeFromFile method to check what it's returning, and then examine the return value in the Index method after it returns.
AskalotLearnalot 15-Jan-20 9:59am    
so same issue that I have in the question happens. but the "code smell" works fine.

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