first off, I am an IT pro trying to dust off the cobwebs of programming in a
Master degree program so please be nice:)
I am trying to pass a system.array object by reference to a procedure (or
class) and keep getting the compile error "an object reference is required
for the nonstatic field, method, or property... (you know the rest) .. please
look at my code and tell me why my pass by reference is not working
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
try
{
Array arrayHeap = Array.CreateInstance(typeof(int),0);
// THIS SYSTEM.ARRAY WILL RESIZE and HAVE Array.Length > 1
arrayHeap = Class1.Collect_InputFiles(sInputFile1, sInputFile2, arrayHeap);
//****THIS IS MY PROBLEM STATEMENT
Heapify(ref arrayHeap,arrayHeap.Length);
}
catch(Exception e)
{
Console.WriteLine("exception " + e.ToString());
}
}
public void Heapify(ref System.Array tempHeap, int n)
{
Console.WriteLine("Begin Heapify");
try
{
Console.WriteLine("i/2 will be =" + n/2);
for (int i = n/2; i >= 1; i--)
{
Console.WriteLine("Heapify : Loop #" + i.ToString());
//****I WANT TO STILL REFERENCE THE SAME ARRAY HERE
SiftDown(ref tempHeap,i,n);
}
}
catch (Exception e)
{
Console.WriteLine("exception " + e.ToString());
}
}
public void SiftDown(Array arrayHeap, int i , int n)
{
try
{
int temp = Convert.ToInt32(arrayHeap.GetValue(i-1));
while (2*i <= n)
{
int child = 2 * i;
if ((child < n)
&& (Convert.ToInt32(arrayHeap.GetValue(child)) >
Convert.ToInt32(arrayHeap.GetValue(child-1))))
{
child = child + 1;
}
if (Convert.ToInt32(arrayHeap.GetValue(child-1)) > temp)
{
arrayHeap.SetValue(Convert.ToInt32
arrayHeap.GetValue (child-1)),i-1);
}
else
{
break;
}
i = child;
}
arrayHeap.SetValue(temp, i-1);
}
catch(Exception e)
{
Console.WriteLine("exception " + e.ToString());
}
}