C#将变量值转换为变量

时间:2017-06-17 08:24:19

标签: c# asp.net c#-4.0 c#-3.0 c#-2.0

WITH list as (
SELECT id,to_char(iso, 'iyyy-iw'),(hr/weeks)::numeric (5,2) as hr_week 
FROM (SELECT id,hr,generate_series(start,stop,interval '1 week') as iso, 
(stop - start)/7 as weeks FROM task) as sub)

SELECT DISTINCT ON (week) week, sum(hr_week) 
FROM list 
GROUP BY 1

我想向每个数字(num)的人输入许多(S)。

如何重复程序(程序),每次重复(s)变量都有新名称。

如果我在程序(程序)中写下一个:

class Program
{
    struct St_test
    {
        public string f_name;
        public string l_name;
        public int age;
        public string email;
    }
    static void proced(int number)
    {
        St_test s = new St_test();
        Console.WriteLine("Enter the first name :");
        s.f_name = Console.ReadLine();
        Console.WriteLine("Enter the last name :");
        s.l_name = Console.ReadLine();
    agee:
        Console.WriteLine("Enter the age :");
        try { s.age = int.Parse(Console.ReadLine()); }
        catch { Console.WriteLine("You enterd viod age"); goto agee; }
        Console.WriteLine("Enter the e_mail :");
        s.email = Console.ReadLine();
    }
    static void Main(string[] args)
    {
        int num;
        nume:
        Console.WriteLine("enter the count of people you would like to store");
        try {  num = int.Parse(Console.ReadLine()); }
        catch { Console.WriteLine("you enterd void number"); goto nume; }
        for (int i = 0;  i < num;  i++)
        {
            proced(num);
        }

如何将结果字符串(r)转换为变量,以便为每个循环使用它而不是(s)变量

1 个答案:

答案 0 :(得分:3)

你不能(很容易地,无论如何)通过这样的名称访问变量 - 但是有一个很多更好的解决方案,即创建某种类型的集合 - 数组或列表,示例

我建议:

  • 更改St_test结构:
    • 将其设为非嵌套类型
    • 给它一个更清晰的名称(例如Person
    • 让它成为一个班级
    • 不要公开字段 - 公开属性
    • 可能使其不可变,获取构造函数中的所有值
  • 更改proced方法:
    • 让它返回一个新的Person
    • 更改名称以遵循.NET命名约定
    • 停止使用goto
    • 将“请求来自用户的整数”分解为方法
  • 更改Main方法:
    • 创建List<Person>
    • 反复调用以前称为proced的内容。但将返回值添加到列表中

你最终会得到类似这样的代码 - 但是不会盲目地复制它。确保你了解这里发生的一切。

using System;
using System.Collections.Generic;

public sealed class Person
{
    public string FirstName { get; }
    public string LastName { get; }
    public int Age { get; }
    public string Email { get; }

    public Person(string firstName, string lastName, int age, string email)
    {
        // TODO: Validation
        FirstName = firstName;
        LastName = lastName;
        Age = age;
        Email = email;
    }
}

public class Test
{
    private static Person CreatePersonFromUserInput()
    {
        Console.WriteLine("Enter the first name:");
        string firstName = Console.ReadLine();
        Console.WriteLine("Enter the last name:");
        string lastName = Console.ReadLine();
        Console.WriteLine("Enter the age:");
        int age = RequestInt32();
        Console.WriteLine("Enter the email address:");
        string email = Console.ReadLine();
        return new Person(firstName, lastName, age, email);
    }        

    private static int RequestInt32()
    {
        string text = Console.ReadLine();
        int ret;
        while (!int.TryParse(text, out ret))
        {
            Console.WriteLine("Invalid value. Please try again.");
            text = Console.ReadLine();
        }
        return ret;
    }

    private static void Main()
    {
        Console.WriteLine("Enter the count of people you would like to store:");
        int count = RequestInt32();
        List<Person> people = new List<Person>();
        for (int i = 0; i < count; i++)
        {
            people.Add(CreatePersonFromUserInput());
        }
        // Just to show them...
        foreach (Person person in people)
        {
            Console.WriteLine(
                $"First: {person.FirstName}; Last: {person.LastName}; Age: {person.Age}; Email: {person.Email}");
        }
    }

}