如何在 C# 中将值从主程序传递到类

本文关键字:主程序 | 更新日期: 2023-09-27 18:35:23

请帮忙,我卡在连接类中的变量和主程序中的变量上。我尝试从用户那里获取值并将这些值存储在类中以供进一步处理。当我尝试使用带有参数的类构造函数时,它不起作用。这是我的代码。提前谢谢你。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SkrlPrototype
{
public class  DieCast
{
    private int _Machine1 { get; set; }
    private int _Machine2 { get; set; }
    private int _Machine3 { get; set; }
    private int _Machine4 { get; set; }
    private int _Machine5 { get; set; }
    private int _Machine7 { get; set; }
    private int _Machine8 { get; set; }
    private int _Machine10 { get; set; }
    private int _Machine12 { get; set; }
    public int GoodPieces;
    public int Scrap;
    public int Shots;
    public DieCast(int Machine1, int Machine2, int Machine3, int Machine4, int    Machine5, int Machine7, int Machine8, int Machine10, int Machine12)//I tried to create a constructor in order to pass value to the variables in a class for further calculation, but couldn't
    {
        _Machine1 = Machine1;
        _Machine2 = Machine2;
        _Machine3 = Machine3;
        _Machine4 = Machine4;
        _Machine5 = Machine5;
        _Machine7 = Machine7;
        _Machine8 = Machine8;
        _Machine10 = Machine10;
        _Machine12 = Machine12;
    }
    int[] Machine;
    public static void bestOperatorAccordingToShots()
    { 
      //To do        
    }
    public static void bestOperatorAccordingToGoodPieces()
    {
           //To do
    }
    public static void worstOperatorAccordingToShot()
    {
            //To do
    }
    public static void worstOperatorAccordingToScrap()
    {
           //To do
    }
    public static void AllGoodPieces()
    {      
     //To do        
    }
    public static void AllScrap()
    {
                //To do
    }
    public static void AllShots()
    {
              //To do
    }
    public static void display()
    {
                 //To do
    }
}
}
///////////////////////////////My main program///////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace kessPrototype
{
static class Program
{
    static void Main()
{
        int _machine1;
        int _machine2;
        int _machine3;
        int _machine4;
        int _machine5;
        int _machine7;
        int _machine8;
        int _machine10;
        int _machine12;
        DieCast die = new DieCast(_machine1, _machine2, _machine3, _machine4, _machine5, _machine7, _machine8, _machine10, _machine12)
        {
            //_Machine1=Machine1;
            //_Machine2=Machine2;
            // _Machine3=Machine3;
            // _Machine4=Machine4;
            // _Machine5=Machine5;
            // _Machine7=Machine7;
            // _Machine8=Machine8;
            // _Machine10=Machine10;
            // _Machine12=Machine12;
          //This is where the problem is. I couldn't pass value to the class

        };
        Console.Write("Please enter Machine1");
        _machine1 = int.Parse(Console.ReadLine());
        Console.Write("Please enter Machine2");
        _machine2 = int.Parse(Console.ReadLine());
        Console.Write("Please enter Machine3");
        _machine3 = int.Parse(Console.ReadLine());
        Console.Write("Please enter Machine4");
        _machine4 = int.Parse(Console.ReadLine());
        Console.Write("Please enter Machine5");
        _machine5 = int.Parse(Console.ReadLine());
        Console.Write("Please enter Machine7");
        _machine7 = int.Parse(Console.ReadLine());
        Console.Write("Please enter Machine8");
        _machine8 = int.Parse(Console.ReadLine());
        Console.Write("Please enter Machine10");
        _machine10 = int.Parse(Console.ReadLine());
        Console.Write("Please enter Machine12");
        _machine12 = int.Parse(Console.ReadLine());
        Console.WriteLine("press any key to close");
        Console.ReadKey();
    }
}

}

如何在 C# 中将值从主程序传递到类

在将机器传递给构造函数之前,您应该初始化机器。然后,在使用 new 时无需重新定义构造函数。

试试:

int _machine1, _machine2; //...
Console.Write("Please enter Machine1");
_machine1 = int.Parse(Console.ReadLine()); //...
DieCast die = new DieCast(_machine1, _machine2, _machine3, _machine4, _machine5, _machine7, _machine8, _machine10, _machine12);