开发者

use attribute of a method into another method

how can i use attribute of a method into another method?

for example: (i commented in related lines)

i have

    public int merge()
    {

        string[] source = textBox3.Text.Split(',');

        int[] nums = new int[source.Length];//i want to use nums in mergesort() too,how can i do that?

        for (int i = 0; i < source.Length; i++)
        {
            nums[i] = Convert.ToInt32(source[i]);
        }
            }

and

  public int mergesort()
    {
        if (nums.Length > 1)///i wrote nums here but compiler doesnt know what nums is.
        {
            int n = nums.Length;
            int p = (int)Math.Floor(n / 2.0);
            int m开发者_开发问答 = n - p;
            List<int> lst1 = new List<int>();
            lst1.AddRange(nums.Skip(n / 2));

            List<int> lst2 = new List<int>();
            lst2.AddRange(nums.Take(n / 2));
  }


Define int[] nums at a class level private variable / property

private int[] nums = null;

 public int merge() 

  {       
      string[] source = textBox3.Text.Split(','); 
       nums = new int[source.Length];//i want to use nums in mergesort() too,how can i do that?       
        for (int i = 0; i < source.Length; i++)    
           { 
               nums[i] = Convert.ToInt32(source[i]);
           }


    }

now use nums in mergesort function .


Write

public int mergesort(int[] nums)
{
    // ...
}

that seems to be the problem.


While a class-level variable would work for you, I would recommend sending nums as a parameter to mergesort because the eventual refactoring would likely dictate the sort be in another class.

public int mergesort(int[] nums)
{
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜