Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return multiple data types

I have a method I want to be able to return a list of IWebElements, a list of just the names of the elements or a string array. Is it possible to return multiple data types with one method? Is there a more feasible way to get the different return types without using just one method?

/// <summary>
/// Gets all options belonging to this selected tag
/// </summary>
/// <returns>Returns a list of IWebElements</returns>
public List<IWebElement> SelectAllOptions(IWebDriver driver, ref DataObject masterData)
{
    //Get the ID of the dropdown menu
    DatabaseRetrieval.GetObjectRepository(ref masterData);
    var strDropMenuId = masterData.DictObjectRepository["ID"];
    //Find the dropdown menu and pull all options into a list
    try
    {
        var dropMenu = new SelectElement(driver.FindElement(By.Id(strDropMenuId)));
        return dropMenu.Options as List<IWebElement>;
    }
    catch (NoSuchElementException exception)
    {
        masterData.Logger.Log(Loglevel.Error, "Boom: {0}", exception.Message);
    }
    masterData.Logger.Log(Loglevel.Debug, "No options found for DropDownMenu: {0}", strDropMenuId);
    return null;
}
like image 647
Jonathan Kittell Avatar asked Jul 23 '26 15:07

Jonathan Kittell


2 Answers

You can't return multiple types. You can however:

  1. Create a type that holds all the possible return types
  2. Use .Net's Tuple instead
  3. Have out parameters.

But that's a very bad design decision and you should really consider avoiding it. (It usually means your method does more than it should)

like image 192
i3arnon Avatar answered Jul 25 '26 05:07

i3arnon


Yes but You have to use the out paramaters like the following

 public List<IWebElement> SelectAllOptions(IWebDriver driver, ref DataObject masterData, out SomeType returnResult1, out SomeType returnResult2)
like image 21
Dan Hunex Avatar answered Jul 25 '26 05:07

Dan Hunex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!