2016-09-19 15 views
-1

Ich bin neu bei C# und ich habe ein Problem in diesem kleinen Programm Ich möchte die eingegebenen Informationen in Methode ClientsDetails zurückgeben, um sie in der Methode Print() zu verwenden. Irgendwelche Hilfe PLZ?So rufen Sie eine Methode, die mehrere Parameter in C#

public static void Main(string[] args) 
    { 

     ClientsDetails(); 

     Print(???,???,???); 

     Console.ReadLine(); 


    } 

    public static void ClientsDetails() 
    { 
     Console.Write("Client's first name: "); 
     string firstName = Console.ReadLine(); 
     Console.Write("Client's last name: "); 
     string lastName = Console.ReadLine(); 
     Console.Write("Client's birthdate: "); 
     string birthday = Console.ReadLine(); 
    } 

    public static void Print(string first, string last, string birthday) 
    { 
     Console.WriteLine("Client : {0} {1} was born on: {2}", first, last, Birthday); 
    } 
} 
+2

Sie mehr studieren können über die C# zuerst, es sieht aus wie eine Hausaufgabe r ather als eine bestimmte Frage. – Prisoner

+1

Sieht so aus, als ob du die Unterrichtsmaterialien gelesen hast, genauso wie du es getan hast [Ask] oder die [faq] – Plutonix

+0

Hallo @Alex, ich bin bereits im Kurs mit Bob Tabor. Vielen Dank für Ihre Antwort – Khalil

Antwort

-1

Es gibt einen Haufen Art und Weise ist, dass Sie die erforderlichen Argumente an Ihre Methode übergeben können, so zum Beispiel einfach Sie es wie folgt tun:

String f = "first string"; 
String l = "last string"; 
String b = "birthday string"; 
Print(f,l,b); 

BTW, in Ihrem Fall scheint es, dass Sie wollen, dass die Eingaben des Benutzers an die Print Methode übergeben werden, so dass eine einfache Möglichkeit, nur ist wie dies die Print Methode in Ihrem ClientsDetails Methode aufzurufen:

Print(firstName, lastName, birthday); 

Für eine umfassende Ressource zu diesem Thema können Sie wie immer auf die docs verweisen. Im Moment können Sie den Async Methods Teil einfach vernachlässigen.

+0

Hallo @mok, die Argumente sind Vom Benutzer in der Methode ClientsDetails() eingegeben, brauche ich die gleichen Argumente in der Methode Print(), – Khalil

+0

@Khalil {Hi}. Die andere Möglichkeit besteht darin, die Werte von Variablen zurückzugeben, die von Benutzern außerhalb der 'ClientDetails'-Methode, z. indem Parameter an die Methode übergeben werden. – mok

+0

Sehr geehrte @mok, Aufruf Print() in ClientsDetails() -Methode löste das Problem, vielen Dank :) – Khalil

0

Ich habe gerade Ihr Programm als Ihre Anforderung korrigiert.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     private static String FirstName; 
     private static String LastName; 
     private static String Birthday; 
     static void Main(string[] args) 
     { 
      ClientsDetails(); 
      Print(FirstName, LastName, Birthday); 
      Console.ReadKey(); 
     } 

     public static void ClientsDetails() 
     { 
      Console.Write("Client's first name: "); 
      FirstName = Console.ReadLine(); 
      Console.Write("Client's last name: "); 
      LastName = Console.ReadLine(); 
      Console.Write("Client's birthdate: "); 
      Birthday = Console.ReadLine(); 
     } 

     public static void Print(string first, string last, string birthday) 
     { 
      Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, Birthday)); 
     } 
    } 
} 
+0

Hallo @Touhid, die drei Argumente außerhalb der Methoden zu deklarieren ist keine Wahl Ich würde es tun, wenn es erlaubt war, aber nein, ich brauche sie in der Methode ClientsDetails() deklariert dann verwenden Sie sie in der Hauptmethode beim Aufruf der Methode Print(), Vielen Dank für Ihre freundliche Antwort – Khalil

+0

Sie können nicht zugreifen die Variablen von einer anderen Methode direkt. Also erkläre ich 3 Variable zu der Klasse und zugewiesen von derClientsDetails(). Auf die zugewiesenen Variablen kann also von überall zugegriffen werden. – Touhid

+0

Ich habe das geändert, weil ich nicht glaube, dass es hätte abgelehnt werden sollen. Sie haben diese Anforderung in der Beschreibung nicht erwähnt, daher ist dies eine gültige Antwort. Da ich diese Anforderung jetzt kenne, habe ich auch eine Antwort gegeben. – Jered

1

Sie könnten eine Struktur verwenden:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    struct Person{ 
     public static FirstName; 
     public static LastName; 
     public static Birthday; 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     {     
      Person person = ClientsDetails(); 
      Print(person.FirstName, person.LastName, person.Birthday); 
      Console.ReadKey(); 
     } 

     public static Person ClientsDetails() 
     { 
      Person returnValue = new Person(); 
      Console.Write("Client's first name: "); 
      returnValue.FirstName = Console.ReadLine(); 
      Console.Write("Client's last name: "); 
      returnValue.LastName = Console.ReadLine(); 
      Console.Write("Client's birthdate: "); 
      returnValue.Birthday = Console.ReadLine(); 
      return returnValue; 
     } 

     public static void Print(string first, string last, string birthday) 
     { 
      Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday)); 
     } 
    } 
} 

Oder ein Array:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     {     
      string person = ClientsDetails(); 
      Print(person[0], person[1], person[2]); 
      Console.ReadKey(); 
     } 

     public static string[] ClientsDetails() 
     { 
      string[] returnValue = new string[3]; 
      Console.Write("Client's first name: "); 
      returnValue[0] = Console.ReadLine(); 
      Console.Write("Client's last name: "); 
      returnValue[1] = Console.ReadLine(); 
      Console.Write("Client's birthdate: "); 
      returnValue[3] = Console.ReadLine(); 
      return returnValue; 
     } 

     public static void Print(string first, string last, string birthday) 
     { 
      Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday)); 
     } 
    } 
} 

oder Referenzen (als Verweis übergeben):

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     {  
      string firstName, lastName, birthday; 
      ClientsDetails(ref firstName, ref lastName, ref birthday); 
      Print(firstName, lastName, birthday); 
      Console.ReadKey(); 
     } 

     public static void ClientsDetails(ref string firstName, ref string lastName, ref string birthday) 
     { 
      Console.Write("Client's first name: "); 
      firstName = Console.ReadLine(); 
      Console.Write("Client's last name: "); 
      lastName = Console.ReadLine(); 
      Console.Write("Client's birthdate: "); 
      birthday = Console.ReadLine(); 
     } 

     public static void Print(string first, string last, string birthday) 
     { 
      Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday)); 
     } 
    } 
} 
+0

Sehr geehrte @ Jered, Vielen Dank für Ihre sehr nützliche Antwort. es wird mir helfen, "struct" und "pass by reference" zu verstehen – Khalil

Verwandte Themen