Resolved Equality (==) Comparison Not Working?

justhumm

Member
Joined
Jun 2, 2023
Messages
15
Programming Experience
Beginner
I'm trying to create an Excel UDF that acts like the native MATCH function, but with multiple criteria. While debugging with a simple comparison between numbers, the objListItem == objListCriteria line isnt't catching the match and is just skipped over "when it should" catch.

I've looked at the locals window and both variables are the same data type and the same value (i = 5, objListItem = 12, objListCriteria = 12), but it doesn't register as a match.

Can anyone point out what I'm doing wrong? Thanks!

C#:
// loop through main index array
i = 0;  j = 0;
int? z = null;   // set NULLABLE value for matching row
// i is the data row to search in
for (i = 0; i < arrIndex.GetLength(0); i++)
{
    // j is to column(s) of data rows
    for (j = 0; j < dblList.Count - 1; j++)
    {
        object objListItem = arrIndex[i, j];
        Type T1 = objListItem.GetType();
        object objListCriteria = objList[j];
        Type T2 = objListCriteria.GetType();

        // if row IS a match for this criteria
        if (objListItem == objListCriteria)
        {
            z = i;
            continue;
        }
        // if row is NOT a match
        else if (objListItem != objListCriteria)
        //if (arrIndex[i, j] != objList[j])
        {
            // if row is not a match, reset return value to NULL
            z = null;
            goto OuterLoop; // then skip to the next row
        }
    }
OuterLoop:
    continue;
}
excel001.png
 
Since you are comparing objects, C# only knows to compare references. It has no idea how to compare the values that the references are pointing to. In fact this is the default C# behavior for all reference types unless the class implements equality methods/interfaces.
 
OK. That's kinda' what I was thinking.

The locals window was showing the data type a OBJECT {double} (or something like that)...so I would need to convert it do an actual DOUBLE for c# to compare it correctly.
 
Just for record...I've never used dynamic data types before, but this is what I ended up doing, for now.

@Skydiver thanks for the info, but my main goal in this routine is to search through rows & rows of output from analysis software. This should be either text strings or fixed-width decimal numbers. So I THINK I should be okay.

Calling Operation:
object objRngItem = arrRng[i, j];
var varRngItem = ToolsArgs.ObjGet(objRngItem);
object objCriteria = objListCrit[j];
var varCriteria = ToolsArgs.ObjGet(objCriteria)
    
    .....
    
if (j != jLimit && varRngItem == varCriteria)

Referenced Method:
internal static dynamic ObjGet(object arg)
{
    Type T1 = arg.GetType();

    if (arg is string)
        return (string)arg;
    else if (arg is double)
        return (double)arg;
    else if (arg is ExcelMissing)
        throw new ArgumentException();  // Will return #VALUE to Excel
    else
        throw new ArgumentException();  // Will return #VALUE to Excel
}
 
Check out the result of this C# code:
C#:
double a = 0.1;
double x = 0.3 - 0.2;
Console.WriteLine("{0:f20}", a);
Console.WriteLine("{0:f20}", x);
Console.WriteLine(x == a);

 
Back
Top Bottom