简体   繁体   中英

How do I create a dynamic (?) variable in a function in C#?

My web.app is basically a calendar interface where you can add workouts to a day. I have a Helpers (class?) where I put all the functions just once so I can call them in other places. Sorry, it's been a long time since I've programmed and don't know all the correct words. I am translating selenium ide tests written in javascript to c#.

So in this Helper below, I add a run to a day, and assign it a variable.

 public static Int64 AddRunToday(IWebDriver driver)
    {
        //hovers over today so day context menu appears, click Add
        Common.Helpers.WaitForElement(By.CssSelector(".day.today.ui-droppable"), driver);
        Common.Helpers.MouseHover(By.CssSelector(".day.today.ui-droppable"), driver);
        driver.FindElement(By.CssSelector(".day.today .daySettings")).Click();
        Common.Helpers.WaitForElement(By.Id("calendarDaySettingsAddLabel"), driver);
        driver.FindElement(By.Id("calendarDaySettingsAddLabel")).Click();

        // selects a Run
        Common.Helpers.WaitForElement(By.CssSelector("button[data-workoutid=\"3\"]"), driver);
        driver.FindElement(By.CssSelector("button[data-workoutid=\"3\"]")).Click();
        Common.Helpers.WaitForElement(By.Id("workoutTitleField"), driver);
        driver.FindElement(By.Id("workoutTitleField")).Click();
        driver.FindElement(By.Id("workoutTitleField")).Clear();
        driver.FindElement(By.Id("workoutTitleField")).SendKeys("Today's Run");
        Common.Helpers.WaitForElement(By.Id("saveClose"), driver);
        driver.FindElement(By.Id("saveClose")).Click();
        Common.Helpers.WaitForElement(By.CssSelector(".day.today .workout.Run[data-workoutid]"), driver);
        Int64 runTodayId = (Int64)Common.Helpers.EvalScript("window.$('.day.today .workout.Run').data('workoutid')", driver);

        return runTodayId;
    }

Then in my test I write

//adds workout
        Int64 runTodayId  = Common.Helpers.AddRunToday(driver);

How can I add an infinite amount of workouts to one day? So, essentially I want to write something like

//adds workout
        Int64 runTodayId1 = Common.Helpers.AddRunToday(driver);
        Int64 runTodayId2 = Common.Helpers.AddRunToday(driver);
        Int64 runTodayId3 = Common.Helpers.AddRunToday(driver);

What do I put in the main function so it will be okay with this? Like, a dynamically named variable or something, maybe? Or how?

Thank you for your help!

If I understand you properly. You want to create a list of Int64.

What I don't understand however is what is the factor that will generate more than one workout. Do you have separate drivers?

You'll need to do some sort of iteration (Driver?) to get different workouts each time. What do you want to do with the workouts when you are done?

List<Int64> workouts = new List<Int64>();

foreach(var something in Collection)
{
   workouts.Add(Common.Helpers.AddRunToday(something));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM