Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 993557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:26:33+00:00 2026-05-16T06:26:33+00:00

My Goal: Extracting one value from an Excel Range , and verify for these

  • 0
  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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:26:34+00:00Added an answer on May 16, 2026 at 6:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.