how to create method returning multiple types of values

Best practices, code snippets for common functionality, examples, and guidelines.
User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3941
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

how to create method returning multiple types of values

Post by odklizec » Tue Mar 24, 2015 9:58 am

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");
}
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

CookieMonster
Certified Professional
Certified Professional
Posts: 74
Joined: Mon Aug 14, 2006 7:17 pm
Location: CH

Re: how to create method returning multiple types of values

Post by CookieMonster » Tue Mar 24, 2015 1:10 pm

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

User avatar
RobinHood42
Posts: 240
Joined: Fri Jan 09, 2015 3:24 pm

Re: how to create method returning multiple types of values

Post by RobinHood42 » Tue Mar 24, 2015 3:29 pm

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

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3941
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: how to create method returning multiple types of values

Post by odklizec » Tue Mar 24, 2015 3:42 pm

Hi guys,

Thanks both of you! Robin's solution looks exactly what I'm after!
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
RobinHood42
Posts: 240
Joined: Fri Jan 09, 2015 3:24 pm

Re: how to create method returning multiple types of values

Post by RobinHood42 » Tue Mar 24, 2015 3:46 pm

You are welcome! :)