【问题标题】:method with dictionary parameter带有字典参数的方法
【发布时间】:2022-01-25 11:06:42
【问题描述】:

请告诉我或指出解决这个问题的正确方法,我还没有经验,很难理解如何解决这个问题。如果有任何帮助,我将不胜感激。

我的 Form 类应该只包含一个公共方法:

class Form
{
    public void FillForm(Dictionary<string, string> values)
    {
        ...
    }
    ...
}

它将用仅基于存储在字典中的字段名-值对的值填充此表单(实际上是任何类似实现的表单)。

And I have such a xpath to fill out a form using a dictionary, it must be one for the whole task

表单有多个字段

  1. 带有问题的文本
  2. 姓名
  3. 电子邮件
  4. 联系电话
  5. 位置
  6. 年龄

【问题讨论】:

  • 为什么要使用字典来保存信息?
  • 使用模型而不是字典。
  • @ImirHoxha 需要字典才能填写在网站上发送问题的字段
  • @HirenPatel 在分配我需要一本字典
  • @dantalion 我知道字典的用途,但您似乎没有掌握它。你必须花一些时间来理解它。在这种情况下,您不需要字典,因为您将只为发布一个问题的一位用户保存信息,除非您要累积所有问题

标签: c# dictionary selenium-webdriver xpath


【解决方案1】:

字典包含键值对。密钥必须是唯一的。所以在这种情况下,字典不是一个好方法。

你必须先构建一个领域模型:

//This will hold your user information
public class UserInfo
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Location { get; set; }
    public int Age { get; set; }
    public string Phone { get; set; }
}

//this other class is called a viewmodel which combains your user and the question posed by the user
public class FormData
{
    public UserInfo user { get; set; }
    public string question { get; set; }
}

//this class is your public facing interface which will interact with the user/front-end form

public class Questionaire
{
    FormData _data; 
    public void FillForm(FormData data)
    {
        //do whatever you want with the data
        //save to database, send by email, whatever
        _data = data;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {

//this is how you use it to collect data from the user
            Questionaire questionaire = new Questionaire();
            questionaire.FillForm(new FormData
            {
                question = "your question goes here",
                user = new UserInfo
                {
                    Name = "YourName",
                    Age = 20,
                    Email = "someemail",
                    Location = "yourlocation",
                    Phone = "0123665555"
                }
            });
        }
    }

选项 2(因为您坚持使用字典):

    public class UserInfo
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Location { get; set; }
        public int Age { get; set; }
        public string Phone { get; set; }
    }


        Dictionary<string, UserInfo> _values;
        public void FillForm(Dictionary<string, UserInfo> values)
        {
            _values = values;
        }


//You call it like this:
            Dictionary<string, UserInfo> dic = new Dictionary<string, UserInfo>();
            dic.Add("question1", new UserInfo { Age = 20, Name = "Name1" });
            dic.Add("question2", new UserInfo { Age = 20, Name = "Name2" });

            Form form = new Form();
            form.FillForm(dic);

选项 3,但这不是可行的方法:

public class Form2
{
    Dictionary<string, string> _values;
    public void FillForm(Dictionary<string, string> values)
    {
        _values = values;
    }
}


Dictionary<string, string> user1 = new Dictionary<string, string>();
user1.Add("question1", "Users question goes here");
user1.Add("Name", "name1");
user1.Add("Age", "age");
            //...
var form2 = new Form2();
form2.FillForm(user1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-11
    • 2020-10-02
    • 2021-06-29
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多