views:

114

answers:

2
  1. My Goal: Extracting one value from an Excel Range, and verify for these cells' value to be the same within this range;
  2. When a cell's value is not the same as the other, I need to return null.

Here's a piece of code:

internal object GetValueFromCells(string start, string end, Formats format) {
    // Verifying for empty or null parameters here and throwing accordingly...

    try {
        Range cells = Excel.get_Range(start, end) as Range;

        object value = null;
        bool sameValue = false;

        foreach(Range cell in cells) {
            // This condition block shall execute only once, since 'value' shall not be null afterwards.
            if (value == null || value == DBNull.Value)
                if (Formats.Formated == format) {
                    value = cell.Text;
                    // The following results to be false !?...
                    sameValue = value == cell.Text; // Shall this not be true?
                } else {
                    value = cell.Value2;
                    // The following results to be false !?...
                    sameValue = value == cell.Value2; // Shall this not be true?
                }

            // This results being always false!?...
            // Shall this not be true, I wonder?
            sameValue = Formats.Formated == format ? value == cell.Text : value == cell.Value2; 

            if(!sameValue)
                return null;
        }

        return value;
    } catch (Exception ex) {
        // Exception handling...
    }
}

Reading this code, I would humbly expect a value to be returned when all of the cells in the range have the same value (for instance 334).

However, this methods always returns null (Nothing in Visual Basic)!

Anyone might explain what I'm missing here while this:

value == cell.Value2

always returns false?

Perhaps is it my algorithm that isn't quite right?

EDIT #1

This has solved the problem:

sameValue = Formats.Formatted == format ? cell.Text.Equals(value) : cell.Value2.Equals(value);

I accepted @Jerod Houghtelling's answer as his answer suggests both the ToString() and the Equals() methods to solve the problem.

In addition to it, I dislike having to call the ToString() method, since the value can be numbers, and comparing numbers under a string looks odd to me. So I prefer the Equals() way which I adopted within my solution.

I would like to thank @Sir Gallahad and @Jerod Houghtelling for their good answers. This was the first time I had to face such a situation, and they both helped me better understand what was going on under the hood, plus the others who contributed too through comments.

And thanks to those who upvoted my question. This serves a purpose to demonstrate that I was not so dumb asking! =P Hehehe...

+3  A: 

Probably the value == cell.Value2 are comparing objects that are from different instances.

Try value.ToString() == cell.Value2.ToString()

Sir Gallahad
Please, elaborate about the subject "comparing objects that are from different instances", because I really don't get that fact that after assigning a value to a variable, and comparing for equality with the value provider (the variable from which comes the value assigned) returns false.
Will Marcouiller
@Will: Object properties are a strange breed. `cell.Value2` may not be as simple as you think it is. It is probably linked to a getter/setter pair which creates a new instance of the value instead of returning the original data.
casablanca
I don't know the exact internal behavior but I imagine that the Text and the Value2 properties are making requests to the Excell object and creating new instances on each fecth to expose the data.
Sir Gallahad
+1 Thanks for answering my question. This enlightened me toward the solution. =)
Will Marcouiller
+3  A: 

I'm guessing that cell.Value2 is returning a new instance of an object each time you call it. Therefore I would deduce the == is checking to see if both sides of the equation are the same instance of the object. To actually compare the value stored on both side you will have to use the .Equals or convert the values to something that can be compared, for example a string.

sameValue = value.Equals( cell.Value2 ); 
/* or */
sameValue = value.ToString() == cell.Value2.ToString();

Also I don't see value being set in your example.

Jerod Houghtelling
*value* is set only once, when passing the first time into the if condition block where I verify whether `value == null`. When it is null (only the first time), I assume the first cell has the right value in it and assign it to my *value* variable, and test for equality afterwards while loop through the other cells of the range.
Will Marcouiller
Ahhh! Yes I see it now. sorry
Jerod Houghtelling
+1 Thanks for your guidance.
Will Marcouiller