2017-07-10 13 views
0

動的な配列を関数C#に入れる方法は?

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

namespace ConsoleApp1 
{ 
    public class RetardedStudent 
    { 
     public String Name = "1"; 
     public int IQ; 
     public void PrintName() 
     { 
      Console.Write("Student Name "); 
      Console.ForegroundColor = ConsoleColor.Green; 
      Console.Write(Name); 
      Console.ResetColor(); 
      Console.Write(" Student Test Result: "); 
      Console.ForegroundColor = ConsoleColor.Red; 
      Console.Write(IQ); 
      Console.ResetColor(); 
      Console.WriteLine(); 
     } 
    } 
    public class Program 
    { 
     public unsafe void SortRetardedStudents(RetardedStudent[] Retards) 
     { 
      int IQ; 
      string Name; 
      int RetardsNumber = Retards.Length; 
      for (int i = 0; i < RetardsNumber; i++) 
      { 
       for (int j = 0; j < RetardsNumber - i - 1; j++) 
       { 
        if (Retards[j].IQ > Retards[j + 1].IQ || (Retards[j].IQ == Retards[j + 1].IQ) && (String.Compare(Retards[j].Name, 0, Retards[j + 1].Name, 0, Math.Max(Retards[j].Name.Length, Retards[j + 1].Name.Length)) > 0)) 
        { 
         IQ = Retards[j].IQ; 
         Name = Retards[j].Name; 
         Retards[j].IQ = Retards[j + 1].IQ; 
         Retards[j].Name = Retards[j + 1].Name; 
         Retards[j + 1].IQ = IQ; 
         Retards[j + 1].Name = Name; 
        } 

       } 
      } 
     } 
     static void Main(string[] args) 
     { 
      unsafe 
      { 
       Console.Write("Input number of students:"); 
       Console.WriteLine(); 
       int RetardsNumber;// = Convert.ToInt32(Console.ReadLine());//Convert не прокатывает если ввести не число 
       Int32.TryParse(Console.ReadLine(), out RetardsNumber);//на случай ввода не числа не будет вылета - будет словно ввели 0 
       if (RetardsNumber < 1) 
       { 
        Console.WriteLine("You entered zero/nonnumeric values as number of students. Programm execution is finished"); 
        Console.ReadLine(); 
        return; 
       } 
       RetardedStudent[] Retards = new RetardedStudent[RetardsNumber]; 
       for (int i = 0; i < RetardsNumber; i++)//fill students and their scores 
       { 
        Retards[i] = new RetardedStudent(); 
        Console.Write("Input Name and score for student # {0} ", i + 1); 
        String StudentNameAndScore = Console.ReadLine(); 
        int SpaceIndex = StudentNameAndScore.LastIndexOf(" "); 
        if (SpaceIndex != -1) 
        { 
         Retards[i].Name = StudentNameAndScore.Remove(SpaceIndex, StudentNameAndScore.Length - SpaceIndex); 
         Int32.TryParse(StudentNameAndScore.Remove(0, SpaceIndex + 1), out Retards[i].IQ); 
        } 
        else 
        { 
         Retards[i].Name = StudentNameAndScore; 
         Retards[i].IQ = 0; 
        } 
       } 
       if (RetardsNumber == 1) 
       { 
        Console.WriteLine("You entered 1 student " + Retards[0].Name + " with iq score test result: " + Retards[0].IQ + " Programm execution is finished because no need in sorting"); 
        Console.ReadLine(); 
        return; 
       } 
       RetardedStudent tempRetard = new RetardedStudent(); 
       SortRetardedStudents(&Retards); 

       for (int i = 0; i < RetardsNumber; i++) 
       { 
        for (int j = 0; j < RetardsNumber - i - 1; j++) 
        { 
         if (Retards[j].IQ > Retards[j + 1].IQ || (Retards[j].IQ == Retards[j + 1].IQ) && (String.Compare(Retards[j].Name, 0, Retards[j + 1].Name, 0, Math.Max(Retards[j].Name.Length, Retards[j + 1].Name.Length)) > 0)) 
         { 
          tempRetard.IQ = Retards[j].IQ; 
          tempRetard.Name = Retards[j].Name; 
          Retards[j].IQ = Retards[j + 1].IQ; 
          Retards[j].Name = Retards[j + 1].Name; 
          Retards[j + 1].IQ = tempRetard.IQ; 
          Retards[j + 1].Name = tempRetard.Name; 
         } 

        } 
       } 
       Console.WriteLine(); 
       Console.WriteLine("sorted by score then by Name"); 
       Console.WriteLine(); 
       for (int i = 0; i < RetardsNumber; i++) 
       { 
        Console.Write("# {0} ", i + 1); 
        Retards[i].PrintName(); 
       } 
       // Console.WriteLine("X format: {0:X}",99999); 
       Console.ReadLine(); 
      } 
      // 
     } 
    } 
} 

私はSortRetardedStudentsを呼び出そうが、何でも、私は常にエラーが発生しません。この私のコード。私は安全でないものを使用しようとしました。プロジェクトにはプロパティを追加して、安全でないコードを実行できるようにしました。 現在、私のエラーはCS0208

問題の解決に役立ちます。私はポインタとビデオ後の参照evemを理解することが非常に悪いです。

答えて

0

安全でないコードを使用する理由はありません。 原産地コードにstaticが足りないようです。

public static void SortRetardedStudents(RetardedStudent[] Retards) 
+0

ありがとうございました! – ANDYARKLAY

関連する問題