简体   繁体   English

如何在另一个 class 方法中使用 class 和构造函数并将它们存储到我的列表中? C#

[英]How can I use a class and constructor in a another class, method and store them to my list? C#

How can I use a class and constructor in a another class and method?如何在另一个 class 和方法中使用 class 和构造函数? I would like to be able to have a menu in the main method and add names to my list via the method Add_them().我希望能够在主方法中有一个菜单,并通过 Add_them() 方法将名称添加到我的列表中。

One of the errors is that the name does not exist in the current context错误之一是该名称在当前上下文中不存在

using System;
using System.Collections.Generic;
using System.Linq;

namespace test
{
    class Poll
    {
        public string Name { get; set; }

        public Poll(string name)

        {
            this.Name = name;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Menu");
            Console.WriteLine("Enter number for the option you want");
            Console.WriteLine("1. Add names");
            Console.WriteLine("2. Stop program");
            var polls = new List<string>();
            string userChoice = Console.ReadLine();
            int userNumber;
            int.TryParse(userChoice, out userNumber);
            if (userNumber == 1)
            {
                Add_them(polls);

            }
            else if (userNumber == 2)
            {
                End_it();
            }
        }

        static void Add_them(List<string> polls)
        {
            string u = null;
            do
            {
                Poll poll1 = new Poll(string name); // ERROR
                Console.WriteLine("Name:");
                poll1.Name = Console.ReadLine();
                Console.WriteLine("Write end to stop or enter and continue on");
                u = Console.ReadLine();
                polls.Add();
            } while (u != "end");
        }
        static void End_it()
        {
            System.Environment.Exit(1);
        }
    }
}

This line: Poll poll1 = new Poll(string name);这一行: Poll poll1 = new Poll(string name); needs to be changed to actually use a string, a string is anything inside double quotes "" so for example:需要更改为实际使用字符串,字符串是双引号""内的任何内容,例如:

Poll poll1 = new Poll("Poll1");

if you want to set it to an empty value then the string class has a static member called Empty :如果要将其设置为空值,则字符串 class 有一个名为Empty的 static 成员:

Poll poll1 = new Poll(string.Empty);

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

相关问题 C# 如何在字典中存储 class 构造函数 - C# How can I store class constructor in dictionary C# 我如何学习在另一个 Class 中调用我的方法的 Class - C# How can I Learn Class that Call My Method in another Class 我如何存储我的清单 <List<Class> &gt;在WPF中使用C#转换为文本文件? - How can i store my List<List<Class>> to a text file using C# in WPF? 我可以从C#中另一个类的构造函数调用构造函数吗? - Can I call a constructor from the constructor of another class in C#? 在另一个类中使用带有变量的函数或方法? C# - Use a function or method with variables in them in another class? C# 如何让C#类在其构造函数中实例化另一个类? - How can I make a C# class instantiate another class in its constructor? 如何使用C#将两个列表中的元素混合并存储为另一个列表? - How can I mix elements from two list and store them is another list using C#? 如何在另一个 class 中使用在两个不同类中声明的列表,而不使用 C# 中的接口? - How can I use a list declared in two different classes in another class, without using interface in C#? 我可以从 C# 中的同一个类方法调用构造函数吗? - Can I call a constructor from same class method in C#? 如何在类构造函数中使用方法数据 c# xamarin - How to use method data in a class constructor c# xamarin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM