简体   繁体   English

在C#的下拉列表中选择随机字符串

[英]Select random string in drop down list in c#

I am trying to figure out how to have my method randomly select an account type from a drop down list. 我试图弄清楚如何让我的方法从下拉列表中随机选择一种帐户类型。 Basically in my web application the user will choose: active, inactive, prospect, or suspended from a drop down list. 基本上,在我的Web应用程序中,用户将选择:活动,非活动,潜在客户或从下拉列表中暂停。 What I am trying to do is test this functionality and have my method randomly select a value when I run the test. 我想做的是测试此功能,并让我的方法在运行测试时随机选择一个值。

I am having a hard time referencing the method to the string value in the code if this makes sense. 如果这有意义,我很难在代码中将方法引用为字符串值。 Any help would be great and if you need additional information let me know! 任何帮助都将非常有用,如果您需要其他信息,请告诉我!

Here is what I have so far: 这是我到目前为止的内容:

   public void RandomStatusTypes()
     {
         List<string> statusTypes = new List<string> { "ACTIVE", "INACTIVE", "PROSPECT", "SUSPENDED" };
         Random randStatus = new Random();
         int index = randStatus.Next(0, 6);
         string value = statusTypes[index];
     }

The code will have a line saying StatusTyp = _Status, and hopefully I can assign a random status to the value so everytime I run the program it randomly picks a value. 该代码将在一行上显示StatusTyp = _Status,希望我可以为该值分配一个随机状态,因此每次运行程序时,它都会随机选择一个值。

Thank you! 谢谢!

Perhaps: 也许:

private static List<string> StatusTypes = new List<string>(){ "ACTIVE", "INACTIVE", "PROSPECT", "SUSPENDED" };
private static Random randStatus = new Random();

public string RandomStatusTypes()
{
    int index = randStatus.Next(0, StatusTypes.Count);
    return StatusTypes[index];
}

Don't create the random instance in the method itself. 不要在方法本身中创建随机实例。 Otherwise you will create the same statuses when the method is called very fast(fe in a loop). 否则,在非常快地调用该方法时(在循环中),您将创建相同的状态。 You should use a field(as shown above) or pass the random as argument to the method. 您应该使用一个字段(如上所示)或将random作为参数传递给该方法。

I've also moved the list outside of the method since it's ineffective to create it always when it doesn't change anyway. 我也将列表移到了方法之外,因为始终无法更改它总是无法创建它。 Last i've used StatusTypes.Count to ensure that you always use a valid range even if you will change the list in future(fe add new statuses). 最后,我使用StatusTypes.Count来确保您始终使用有效范围,即使将来要更改列表(添加新状态)也是如此。

If you're just interested in selecting a value from the DropdownList, you should just randomly change the SelectedIndex property: 如果您只想从DropdownList中选择一个值,则应该随机更改SelectedIndex属性:

 Random randStatus = new Random();
 public void RandomStatusTypes()
 {
          ddlStatus.SelectedIndex = randStatus.Next(ddlStatus.Items.Count);
 }

This will randomly change to any value in the dropdown. 这将随机更改为下拉菜单中的任何值。 Notice it uses ddlStatus.Items.Count so if you ever add or remove items, you won't have to modify this code. 请注意,它使用ddlStatus.Items.Count因此,如果您添加或删除项目,则不必修改此代码。

If you need random value for the DropdownList, you should set the SelectedIndex property to a random number 如果需要DropdownList的随机值,则应将SelectedIndex属性设置为随机数

   public void RandomStatusTypes()
     {
         List<string> statusTypes = new List<string> { "ACTIVE", "INACTIVE", "PROSPECT", "SUSPENDED" };
         Random randStatus = new Random();
         dropList.Items.AddRange(statusTypes);
         dropList.SelectedIndex = randStatus.Next(0, 3);
     }

Try this function which returns a random item from the passing myList 尝试使用此function ,该function从传递的myList returns a random item

public string RandomListItem(List<string> myList)
{
    int randNo = new Random().Next(0, myList.Count());
    return myList[randNo];
}

First of all you need to return an index from that method ( void = it returns nothing ) to use as a selected value for your DropDownList . 首先,您需要从该方法返回一个索引(void =不返回任何内容),以用作DropDownListselected value

public int RandomStatusTypes()
 {
     List<string> statusTypes = new List<string> { "ACTIVE", "INACTIVE", "PROSPECT", "SUSPENDED" };
     Random randStatus = new Random();
     return randStatus.Next(0, statusTypes.Count);
 }

After that you can call that method somewhere like this 之后,您可以在这样的地方调用该方法

  var statusForDropDown= RandomStatusTypes(); 

And assign statusForDropDown to your DropDownList 并将statusForDropDown分配给您的DropDownList

     MyDropDownList.SelectedIndex=statusForDropDown;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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