Page 1 of 1

IF Item Exists not working

Posted: Thu Apr 24, 2014 11:41 am
by houseofcutler
Hi All, I guess there is a simple answer to my question - but alas I appear to be too simple to find it!

Within my website under test it is possible that a menu item may not exist - however if it does exist I want to click on it to expand it.

I have tried to use the code below to achieve this but when running the test I get the following error:
Cannot assign to 'Exists' because it is a 'method group' (CS1656) - C:\Users\bcutl_000\Documents\Ranorex\RanorexStudio Projects\Messenger5-Regression\Messenger5-Regression\SendMessage_Menu_Groups.UserCode.cs:118,17
Can someone tell me where I am going wrong please?

Many thanks

Code: Select all

        public void Mouse_Click_SavedGroups()
        {
            Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'GroupcallMessengerRoot.SendMessage.SideMenu.SavedGroups' at Center.", repo.GroupcallMessengerRoot.SendMessage.Recipients.Menu_Groups.SavedGroupsInfo);
            if (repo.GroupcallMessengerRoot.SendMessage.Recipients.Menu_Groups.SavedGroupsInfo.Exists = true) {
            	repo.GroupcallMessengerRoot.SendMessage.Recipients.Menu_Groups.SavedGroups.Click();
            }
        }

Re: IF Item Exists not working

Posted: Thu Apr 24, 2014 2:52 pm
by dhh
This line:
if (repo.GroupcallMessengerRoot.SendMessage.Recipients.Menu_Groups.SavedGroupsInfo.Exists = true)
should be:
if (repo.GroupcallMessengerRoot.SendMessage.Recipients.Menu_Groups.SavedGroupsInfo.Exists() == true)
'Exists' is a method so needs brackets, also '=' is an assignment, "==" is a test for equality

Re: IF Item Exists not working

Posted: Thu Apr 24, 2014 2:53 pm
by krstcs
Exists is a method and must have a parameter group behind it in C#. This is what the error is telling you (in different words :D ).

Add a parenthesis to the end of Exists like this:

Code: Select all

myObject.Exists();
Parenthesis denotes a parameter group. You can pass parameters to the method by putting them inside the parenthesis. Some methods do not need parameters, such as Exists(), but others must have them, such as WaitForExists(Duration).

Here I'm passing a parameter of an integer 200 as a Duration object to the WaitForExists method.

Code: Select all

myObject.WaitForExists(200);
For primatives and some object types (integer, boolean, char, string) you can just put a raw value like I did above. For other types (objects types mainly) you have to create a new object of the type needed before you can use it. Some object types will be inferred from the call, like what I did above. WaitForExists takes a Duration type object as a parameter, but you can just put in an integer and .NET will create a Duration object with using that integer as a value for the Duration "constructor".

I could have done this:

Code: Select all

Duration myDuration = new Duration(200);

myObject.WaitForExists(myDuration);
A lot of this is basic Object Oriented Coding and .NET, so reading up on it will help. :D

Re: IF Item Exists not working

Posted: Thu Apr 24, 2014 3:06 pm
by krstcs
Also, = is an assignment operator. A logical EQUALITY operator is what you want in the 'if', so use "==" instead.

= -> Assign right side into left side (or set left side equal to right side).
== -> Are the two equal?

Now, one thing to note: == is not always what you want.

When dealing with some object types, you are better off using the Equals() method. == on object types usually (more on this at the end) only tells you if the object references point at the same object, not if the internal value is equals.

For example (made up stuff following...):

Code: Select all

object s1 = "my string";
object s2 = "my string";

if (s1 == s2) ...   // returns FALSE since s1 and s2 do not point at the same object behind the scenes
if (s1.Equals(s2)) ...  // returns TRUE since it is actually looking at the internal value
So, for integer values and booleans (true/false) and other primitive types, you can use == and be ok, but for other objects you want to use the object's Equals() method if it has one because you may not get what you are expecting from == all the time.


NOTE: In .NET EVERYTHING is an object, so what I say above may be a bit confusing except that .NET can overload the operators to make them do specific things. In the case of "==", it has been overloaded in many cases to do the same thing as the Equals() method. Strings, for example, you can use ==, even though the objects point to different things behind the scenes. It works because the string class has overloaded the == operator. My point above is that you can't always assume that.

Re: IF Item Exists not working

Posted: Fri Apr 25, 2014 12:58 pm
by houseofcutler
Thank you both for your updates.

Krtcs for the lesson and recommendation (C# for Dummies on order :wink: )
DHH for the straightforward solution

Now on to my next problem.... :roll: