将用户输入读入全局字符串C#

时间:2015-12-01 06:48:34

标签: c# class input global

我正在将文件读入DataTables,但需要为我的产品的不同版本使用不同的文件名。如何获取用户在类中设置为全局变量所输入的产品版本号?

我想将一个案例陈述应用于"使用(TextFieldParser)"如此,它将使用正确的文件为正确的版本。

static public DataTable VolumeImport()
{
    DataTable Volumes = new DataTable();
    Volumes.Columns.Add("MediaId", typeof(string));
    Volumes.Columns.Add("MediaTypeName", typeof(string));
    Volumes.Columns.Add("MediaGroupName", typeof(string));
    Volumes.Columns.Add("Barcode", typeof(string));
    Volumes.Columns.Add("PoolName", typeof(string));

    using (TextFieldParser volumes = new TextFieldParser("C:\\Input Files\\752.dat"))

    {
        volumes.TextFieldType = FieldType.Delimited;
        volumes.Delimiters = new string[] { "," };
        volumes.HasFieldsEnclosedInQuotes = true;
        volumes.TrimWhiteSpace = true;


        string[] deviceprops;

        while (!volumes.EndOfData)
        {
            deviceprops = volumes.ReadFields();
            Volumes.Rows.Add((deviceprops[3]).Replace("'", String.Empty), (deviceprops[7]).Replace("'", String.Empty), (deviceprops[8]).Replace("'", String.Empty),
            (deviceprops[16]).Replace("'", String.Empty), (deviceprops[50]).Replace("'", String.Empty));
        }
    }
    return Volumes;
}

我添加了以下内容,但它并没有要求提供该版本。我怀疑这是因为它不属于虚空主

public class Version
{
    public string nbuversion { get; set; }

    public void GetVersion()
    {
        Console.WriteLine("Enter the Version:"); // Prompt for version
        string nbuversion = Console.ReadLine();
    }
}

更新:我已根据下面的建议设法解决了这个问题。非常感谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic.FileIO;
using System.IO;
using System.Xml;
using System.Data;

public class DataCollect
{
    static public Dictionary<int, string> Day_Lookup = new Dictionary<int, string>();
    public string nbuversion { get; set; }
    public string filename { get; set; }

    class NBUXMLWriter
    {
        static public void Main(string[] args)
        {

            DataCollect s = new DataCollect();
            s.GetVersion();
            s.SetFileLocations();
            s.DayImport();
        }
    }
    public void GetVersion()
    {
        Console.WriteLine("Enter the Version:");
        nbuversion = Console.ReadLine();
    }

    public void SetFileLocations()
    {
        switch (nbuversion)
        {
            case "7.6":
                filename = "input1.txt";
                break;
            case "7.7":
                filename = "input2.txt";
                break;
        }
    }

    public void DayImport()
    {

        string filepath = "C:\\Input Files\\Static\\";

        using (TextFieldParser daysofweek = new TextFieldParser((filepath)+(filename)))

        {
            daysofweek.TextFieldType = FieldType.Delimited;
            daysofweek.Delimiters = new string[] { " " };
            daysofweek.HasFieldsEnclosedInQuotes = true;
            daysofweek.TrimWhiteSpace = true;


            string[] dayprops;

            while (!daysofweek.EndOfData)
            {
                dayprops = daysofweek.ReadFields();
                int daynumber;
                bool daynumberparsed = Int32.TryParse((dayprops[0]), out daynumber);
                string dayname = dayprops[1];
                Day_Lookup.Add((daynumber), (dayname));
            }

        }
        Console.WriteLine("Day is" + Day_Lookup[(4)]);
        Console.ReadKey();

    }
}

1 个答案:

答案 0 :(得分:0)

执行此操作会将ReadLine转换为GetVersion()方法中的局部变量:

public void GetVersion()
{
    Console.WriteLine("Enter the Version:"); 
    string nbuversion = Console.ReadLine();
}

这会写入Version.nbuversion属性:

public string nbuversion { get; set; } //readline into "Global" property here
public void GetVersion()
{
    Console.WriteLine("Enter the Version:"); 
    nbuversion = Console.ReadLine(); //remove "string"
}

此示例代码应足以让您入门

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Version v = new Version();
            v.GetVersion();

            //do stuff with nbuversion - 1st way
            Console.WriteLine("1:" + v.nbuversion);

            //do stuff with nbuversion - 2nd way
            v.VolumeImport();
        }
    }

    public class Version
    {
        public string nbuversion { get; set; }

        public void  GetVersion()
        {
            Console.WriteLine("Enter the Version:");
            //string nbuversion = Console.ReadLine(); <- this will Readline into local variable - WRONG
            nbuversion = Console.ReadLine();
        }

        public DataTable VolumeImport()
        {
            Console.WriteLine("2:" + nbuversion);
            //do stuff with nbuversion
            ...
        }
    }
}