Click here to Skip to main content
14,422,431 members
Rate this:
Please Sign up or sign in to vote.
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 4 days ago
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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


The ReadBarcodeFromFile method should be
public Array ReadBarcodeFromFile(string _Filepath)
{
    String[] barcodes = BarcodeScanner.Scan(_Filepath, BarcodeType.Code39);
    if (barcodes.Length != 0)
    {
        return barcodes;
    }
    
    return null; 
}
   
v2
Comments
AskalotLearnalot 4 days ago
   
Thank you for your help. However this approach resulted in same issue.
Richard MacCutchan 4 days ago
   
Then you must be doing something wrong. But since you have not shown your code I cannot guess what that may be.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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:
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[^]
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;
}
   
Comments
AskalotLearnalot 4 days ago
   
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 4 days ago
   
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 4 days ago
   
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 4 days ago
   
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 4 days ago
   
so same issue that I have in the question happens. but the "code smell" works fine.
Richard Deeming 4 days ago
   
So you're saying that the ReadBarcodeFromFile method reads an array of at least three strings; none of the strings are null; but when the method returns, and your if statement executes, the returned values have changed to null?

There must be something else going on. Tuple return values work for everyone else.
AskalotLearnalot 4 days ago
   
Correct before i step into the last } in the ReadBarcodeFromFile if I go to the action result and hoover over var barcodes it has 3 elements. However, strTopBarCode,strMiddleBarCode.. stays with no value once hoovered over then when I exit the method they get the value of null. In my case it was the get values then when i exit the method they get assigned null, which I just didn't know how to pass/append the values back.
Richard Deeming 4 days ago
   
The second solution has no variables called strTopBarCode or strMiddleBarCode. So there's no way they can be null, because they don't exist!
AskalotLearnalot 4 days ago
   
so what is this public (string topBarCode, string middleBarCode, string bottomBarCode)
and how would it work for me in the if statement. It is null I don't know how
Richard Deeming 4 days ago
   
That's a value tuple:
Tuple types - C# Guide | Microsoft Docs[^]

It's a way of returning multiple values from a method without having to write your own wrapper type.
AskalotLearnalot 4 days ago
   
so any idea on how to make the second solution work. or is this a case where only pass by ref works.
Richard Deeming 4 days ago
   
As I keep saying, if the first solution works, then so does the second.

You seem to be mixing up the solutions, and looking at unrelated variables.
AskalotLearnalot 4 days ago
   
lol it should work. I will do more digging thank you for your time and help.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100