Page 1 of 1

how to create method returning multiple types of values

Posted: Tue Mar 24, 2015 9:58 am
by odklizec
Hi guys,

I have a below code, which I need to call at several places in one of my methods. Instead of copying and pasting all those lines at various places, I would prefer to use a new method, containing below code, which would return the tagElement with correct capability. Any suggestion how to do this? I'm not quite sure how create a method, which would return different types of values (C# rookie;)). As you can see, the tagElement could be (so far) either Ranorex.InputTag or Ranorex.SelectTag, most probably more in future. Should I use "out" keyword in method definition or is there a better approach?
//get preferred capability of selected element
var tagType = unknownTagElement.Element.PreferredCapability.DisplayName;
if (tagType == "InputTag")
{
	//create adapter with correct capability
	Ranorex.InputTag tagElement = unknownTagElement.Element;
	//get TagName
	tagName = tagElement.Name.ToString();
	if (tagElement.Type=="radio" | tagElement.Type=="checkbox")
	{
		//get attribute value
		tagValue = tagElement.Element.GetAttributeValueText("Checked");
	}
	else
	{
		//get attribute value
		tagValue = tagElement.Element.GetAttributeValueText("TagValue");
	}
}
else if (tagType == "SelectTag")
{
	//create adapter with correct capability
	Ranorex.SelectTag tagElement = unknownTagElement.Element;
	//get TagName
	tagName = tagElement.Name.ToString();
	//get TagValue
	tagValue = tagElement.Element.GetAttributeValueText("TagValue");
}

Re: how to create method returning multiple types of values

Posted: Tue Mar 24, 2015 1:10 pm
by CookieMonster
Hi Pavel,

I'm not sure if I got your question completely. That how I understood your question, you have this method and you want to return the Object (InputTag, SelectTag or ....) and you don't know also what you are planing pass to your method, right?

If I understood it, then can to something like this.

Code: Select all


public WebElement DoSomething(WebElement webElement)
{
 // your code
}

// In your method you do something like this
InputTag myInputTag = (InputTag)DoSomething(unknownWebElement)

Cheers
Dan

Re: how to create method returning multiple types of values

Posted: Tue Mar 24, 2015 3:29 pm
by RobinHood42
Hi Pavel,

I guess this is what you are looking for 8)
private void Init()
		{
			Element unknownTagElement = Host.Local.FindSingle("PathToElement");
			var myAdapterWithCorrectCapability = CreateCorrectAdapterFromElement(unknownTagElement);
		}
		
		private Adapter CreateCorrectAdapterFromElement(Element unknownTagElement)
		{
			//get preferred capability of selected element
			var tagType = unknownTagElement.PreferredCapability.DisplayName;
			
			if (tagType == "InputTag")
			{
				return unknownTagElement.As<InputTag>();
			}
			else if (tagType == "SelectTag")
			{
					return unknownTagElement.As<SelectTag>();
			}
			return null;
		}
Cheers,
Robin

Re: how to create method returning multiple types of values

Posted: Tue Mar 24, 2015 3:42 pm
by odklizec
Hi guys,

Thanks both of you! Robin's solution looks exactly what I'm after!

Re: how to create method returning multiple types of values

Posted: Tue Mar 24, 2015 3:46 pm
by RobinHood42
You are welcome! :)