C#,在对象Class中创建数组并执行方法,自定义排序方法

C#, Creating an Array in an Object Class and executing the method, custom sorting method

C#,我正在尝试在 class 中创建一个数组作为对象。当我进行自定义排序时,我希望此程序 运行 该方法。我需要了解如何将已从文本创建的十进制数组中的值传递给该对象?我在某处出错了。当我在表单前端的另一侧打印数组时,我得到的只是表单的名称。

调用 class 的主窗体:Sort sort = new Sort(rawArray);

using System;


namespace BbblSrtProj
{
    
    public class Sort
    {

        private decimal[] theArray;
        public Sort() { }
        public Sort (decimal[] sort)
        {
            this.theArray = sort;
            
        }
        public decimal[] TheArray
        {
            get
            {
                return theArray;
            }
            set 
            {
                theArray = value;
            }
        }

        //Sort Method: Bubble Sort
        public Array SortingMethod()
        {
            for (int i = 0; i <= TheArray.Length - 1; i++)
            {
                // Temp int variable to hold value in
                decimal temp;

                // Swap out adjacent value by order,
                // till completed.
                for (int j = 0; j < TheArray.Length - 1; j++)
                {
                    if (TheArray[j] > TheArray[j + 1])
                    {
                        temp = TheArray[j + 1];
                        TheArray[j + 1] = TheArray[j];
                        TheArray[j] = temp;
                    }
                }
            }

            return TheArray;
        
        }
    }
}

您需要一个 class 和一个 属性 的十进制数组,以及一个对数组进行排序的方法来完成您的作业?这是我对你所写内容的最佳猜测...如果是这样的话:

 public class SortingClass
{
    public decimal[] TheArray { get; set; }

    public void Sort()
    {
        // Do the sort
        
    }
}

然后设置数组并调用方法:

var sortingClass = new SortingClass();
sortingClass.TheArray = YourArray;
sortingClass.Sort();

但是你确定这是作业要你做的吗?因为我没有看到 class 中数组 属性 的任何原因。无论如何,如果您希望人们能够帮助您,您应该分享更多关于您出错的细节。

// Sort.cs 
//This Got it working

using System;
using System.Collections.Generic;
using System.Text;

    namespace SortItProj
    {
    
        public class Sort
        {
            private decimal[] theArray;
    
            public Sort(decimal[] sort)
            {
                this.theArray = sort;
            }
    
            public decimal[] TheArray
            {
                get
                {
                    return theArray;
                }
                set
                {
                    theArray = value;
                }
        }

        public Array SortingMethod()
        {
            for (int i = 0; i <= TheArray.Length - 1; i++)
            {
                // Temp int variable to hold value in
                decimal temp;

                // Swap out adjacent value by order,
                // till completed.
                for (int j = 0; j < TheArray.Length - 1; j++)
                {
                    if (TheArray[j] > TheArray[j + 1])
                    {
                        temp = TheArray[j + 1];
                        TheArray[j + 1] = TheArray[j];
                        TheArray[j] = temp;
                    }
                }
            }    
            return TheArray;    
        }
    }

}

// SortItProj.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SortItProj
{
    internal class Program
    {
        static void Main()
        {
            Random random = new Random();
            decimal[] theArray = new decimal[55];

            int theArrayLength = theArray.Length;   
            decimal[] theArray2 = new decimal[theArrayLength];
            
            int i = 0;

            do
            {
                theArray[i] = (random.Next(1, 1000));
                i++;

            } while (i < theArray.Length);

            Array.Copy(theArray, theArray2, theArrayLength);

            Sort sort = new Sort(theArray);

            sort.SortingMethod();

            int j = 0;
            foreach (decimal number in theArray)
            {
                Console.Write(@$"{theArray2[j]} < RAW | SORTED > ");
                Console.WriteLine(number + "\n");
                j++;
            }
        }
    }
}