0

I have this code:

string str1 = "Button1";
string str2 = "Button2";
string str3 = str1 + " " + str2;

What I want is to copy the text from str3 ("Button1 Button2") so that

string str4 = "Button1 Button2";

Why do I want such a thing, you may ask? It's because of this method that I'm trying to develop:

    public void SearchNumpadNumbersOnMyApp(double valueRepoItemName)
    {   
        valueRepoItemName = Math.Abs(valueRepoItemName);        
        string repoItemName;
        string result = string.Format("{0:F1}", valueRepoItemName);
        int length = result.Length;         
        char[] arrayOfCharacters = result.ToCharArray();

        for (int i = 0; i < length; i++)
        {                   
        repoItemName = "Button" + arrayOfCharacters[i].ToString();

        // Query RepoItemInfo objects based on the repository item name 
        IEnumerable<RepoItemInfo> myQuery = from things in repo.FO.FLOW2FO.Container2.SelfInfo.Children
                    where ReferenceEquals(things.Name, repoItemName)
                    select things;
        // Create "unkown" adapter for the first found element and click it
        myQuery.First().CreateAdapter<Unknown>(true).Click();               
        }               
    }

When I pass repoItemName to

where ReferenceEquals(things.Name, repoItemName)

I get the error message "Sequence contains no elements", and this occurs when I try to pass repoItemName string. This is the same as passing

where ReferenceEquals(things.Name, "Button" + arrayOfCharacters[i].ToString())

and this is the reason why I'm getting the error. So, what I want is to pass the actual text of the string and not its reference. I want it to be, for example, like this:

where ReferenceEquals(things.Name, "Button5")

Being "Button5" the string structure built with:

repoItemName = "Button" + arrayOfCharacters[i].ToString();

By the way, I already tried:

String.Copy();
String.Clone();

but nothing seems to do what I really want.

Sae1962
  • 1,122
  • 15
  • 31
kenzoviski
  • 81
  • 1
  • 10
  • 6
    You likely want to replace `ReferenceEquals(things.Name, repoItemName)` with `string.Equals(things.Name, repoItemName)` because you want to compare the equality of what the strings represent, not the equality of where each string is residing in memory. – Quantic Jan 03 '17 at 16:40
  • Why are you checking references instead of the actual value? as @Quantic said you just need to do a string equals... – Nilesh Jan 03 '17 at 16:44
  • You my friend, just solved my problem, thank you so much!!! :) Massive hug for you! – kenzoviski Jan 03 '17 at 16:44
  • No need for the char array `repoItemName = "Button" + result[i];` – Edward Jan 03 '17 at 16:59

2 Answers2

1

The solution to my problem was this:

    IEnumerable<RepoItemInfo> myQuery = from things in repo.FO.FLOW2FO.Container2.SelfInfo.Children
        where String.Equals(things.Name, repoItemName)
        select things;

Thanks to all of you guys that helped me :)

kenzoviski
  • 81
  • 1
  • 10
0

You need to change this

IEnumerable<RepoItemInfo> myQuery = from things in repo.FO.FLOW2FO.Container2.SelfInfo.Children
                where ReferenceEquals(things.Name, repoItemName)
                select things;

to

IEnumerable<RepoItemInfo> myQuery = from things in repo.FO.FLOW2FO.Container2.SelfInfo.Children
                where things.Name == repoItemName
                select things;

You want to compare the content of the strings, not the references. A string in C# is an immutable class. So

string str1 = "button1 button2";
string str2 = new string(str1.ToCharArray());

str1 and str2 are equal strings, but the two variables reference two different instances of string. Thatswhy ReferenceEquals(str1, str2) will always return false.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Just FYI your example `string str1 = "button1 button2"; string str2 = "button1 " + "button2"; Console.WriteLine(ReferenceEquals(str1, str2));` returns `True`, I assume because of [this](http://stackoverflow.com/questions/9112610/referenceequals-working-wrong-with-strings). – Quantic Jan 03 '17 at 16:48
  • @Quantic I already feared that this example is not good, because of those damn smart compilers. I changed the example, but now I'm not sure if it's not more confusing... – René Vogt Jan 03 '17 at 16:52
  • @René Vogt I swapped my code with your code and it didn't compiled. Although I already found the solution provided by the user Quantic – kenzoviski Jan 03 '17 at 16:54
  • @kenzoviski that's because I used the `equals` keyword in wrong context. Changed it to `==`, but you may use the `string.Equals()` from Quantic as well. – René Vogt Jan 03 '17 at 16:57