我如何传递这些值c# [英] how do I pass these value c#

查看:55
本文介绍了我如何传递这些值c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将我在signup()中设置的值显示到seevalue()函数中。 

我希望用户能够设置值并在seevalue中查看它们()函数。

how to do I display the value I set in signup() into seevalue() function. 
I want the user to be able to set value and see them in seevalue() function.

namespace MyApp
{
    class members
    {
        public string name_;
        public int age_;
        public double salary_; 
        public int Age
        {
            get { return age_; }
            set { age_ = value; }
            
        }
        public string Name 
        {
            get { return name_; }
            set { name_ = value; }
        }
        public double Salary
        {
            get { return salary_; }
            set { salary_ = value; }

        }

        public static void Main(string[] args)
        {

            while (true)
            {
                Console.Clear();

                menu();

                string input = Console.ReadLine();
                if (input == "1")
                {
                    Signup();
                }
                else if (input == "2")
                {
                    seeValue();
                }
                else if ( input == "3")
                {
                    Quit();
                }
            }
        }

        public static void menu()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("sign up: ");
            Console.WriteLine("See members: ");
            Console.WriteLine("Quite: ");

        }
        public static int Signup()
        {
            members newMn = new members();
            Console.Write("Whats your name: ");
           
            newMn.name_ = Console.ReadLine();
            Console.Write("Whats your age: ");
            newMn.age_ = Convert.ToInt32(Console.ReadLine());
            Console.Write("Whats yor salary income: ");
            newMn.salary_ = Convert.ToDouble(Console.ReadLine());
            
            
            return 1;

        }

        public  static void seeValue(members newMn)
        {
            
            Console.WriteLine(newMn);
        }
        static void Quit()
        {
            Environment.Exit(0);
        }




推荐答案

方法seeValue采用一个参数成员类型。但是当你在Main函数中调用它时,没有参数。您可以更改注册方法以返回成员类型并轻松返回newMn对象。

Method seeValue takes one parameter members type. But when you call it in Main function there is not parameter. You can change Signup method to return members type and return newMn object easily.

namespace MyApp { class members { public string name_; public int age_; public double salary_; public int Age { get { return age_; } set { age_ = value; } } public string Name { get { return name_; } set { name_ = value; } } public double Salary { get { return salary_; } set { salary_ = value; } }

公共覆盖字符串ToString()

public override string ToString()

{

返回string.Format( "{0} \t {1} \t {2}",姓名,年龄,薪水);

return string.Format("{0}\t{1}\t{2}", Name, Age, Salary);

}

public stat ic void 主要 string [] args
{
members signUpResult = null;
true
{
控制台 清除 ();

菜单
();

string input = 控制台 ReadLine ();
if 输入 < span class ="pun"> == " 1 "
{
signUpResult =注册 ();
}
else 如果 输入 == " ; 2"
{ < span class ="pln">
seeValue (signUpResult);
}
else 如果 输入 == " 3"
{
退出 ();
}
}
}

public 静态 void 菜单 ()
{
控制台 ForegroundColor = ConsoleColor 黄色 ;
控制台 WriteLine "注册:" );
控制台 WriteLine "见成员:" );
控制台 WriteLine " Quite:" );

}
public static 成员 注册 ()
{
成员newMn
= new 成员 ();
控制台 < span class ="pun">( "你叫什么名字:" );

newMn
name_ = 控制台 ReadLine ();
控制台 "什么是你的年龄:" );
newMn
age_ = 转换 ToInt32 控制台 ReadLine ());
控制台 < span class ="pun">。
" Whats yor工资收入:" );
newMn
salary_ = 转换 ToDouble 控制台 ReadLine ());


返回 newMn;

}

public static void seeValue 成员newMn
{

控制台 WriteLine newMn?.ToString() ) ;
}
static void 退出 ()
{
环境 退出 0 );
}

} public static void Main(string[] args) { members signUpResult = null; while (true) { Console.Clear(); menu(); string input = Console.ReadLine(); if (input == "1") { signUpResult = Signup(); } else if (input == "2") { seeValue(signUpResult); } else if ( input == "3") { Quit(); } } } public static void menu() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("sign up: "); Console.WriteLine("See members: "); Console.WriteLine("Quite: "); } public static members Signup() { members newMn = new members(); Console.Write("Whats your name: "); newMn.name_ = Console.ReadLine(); Console.Write("Whats your age: "); newMn.age_ = Convert.ToInt32(Console.ReadLine()); Console.Write("Whats yor salary income: "); newMn.salary_ = Convert.ToDouble(Console.ReadLine()); return newMn; } public static void seeValue(members newMn) { Console.WriteLine(newMn?.ToString()); } static void Quit() { Environment.Exit(0); }

}

}





这篇关于我如何传递这些值c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆