-
March 7th, 2013, 10:40 PM
#1
Private Array is Implicitly Modified
Hello, all. I'm new here (and to the C# programming language), so please excuse the format of my post for now.
I've encountered a problem in a simple C# script I am writing. This is a test script and serves no major purpose beside understanding the fundamentals of classes and scope in C#. So here are the relevant details:
1) I declared two private arrays inside a class called IntArray.
2) The backup array, myIntArrayBackup, should be modified only once, when the IntArray object (list1) is instantiated, via the constructor.
3) Every time I call public methods from the MainTest class using the IntArray object, the backup array is somehow implicitly being modified along with the myIntArray array that I am explicitly modifying. Any changes made to myIntArray are also being made to myIntArrayBackup.
The way in which I am debugging this is very simple. In the main method I am calling the function Reset() which is supposed to restore the original array (taken in as a parameter when the list1 object is instantiated). But since the backup is constantly being modified, the original array is lost.
The point is that the backup array should remain fixed as to what the IntArray constructor sets it to. Yet somehow its elements are always always equal to those in the non-backup (the arrays are essentially identical). Any ideas? The complete C# script is shown below for a complete reference:
* Note that class Test2 is not relevant here
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Testing
{
class MainTest
{
static void Main(string[] args)
{
// Test simple I/O
/*
Console.WriteLine("Hello World");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("x = " + x++);
Console.WriteLine("x = " + x);
*/
// Test simple use of object and method
/*
Test2 test2obj = new Test2(18);
Console.WriteLine("PrintMe = " + test2obj.PrintMe(12));
*/
int[] someIntArray = new int[] {9, 5, 2, 8, 33, 4, 7, 1, 8, 9};
IntArray list1 = new IntArray(someIntArray);
Console.WriteLine("Default order:");
list1.Print();
list1.SortAscending();
Console.WriteLine("Ascending order:");
list1.Print();
list1.Reset();
Console.WriteLine("Default order:");
list1.Print();
list1.SortDescending();
Console.WriteLine("Descending order:");
list1.Print();
Console.ReadLine();
}
}
class Test2
{
private int experiment10;
public Test2(int experiment10)
{
this.experiment10 = experiment10;
}
public int PrintMe(int val)
{
int printed = experiment10 - val;
return (printed);
}
}
// Perform several functions on an array of integers
class IntArray
{
private int[] myIntArray; // This array will be operated on
private int[] myIntArrayBackup; // This array will allow myIntArray to be reset
public IntArray(int[] myIntArray)
{
this.myIntArray = myIntArray;
this.myIntArrayBackup = myIntArray;
}
// Print each element in the array
public void Print()
{
foreach(int i in myIntArray)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
// Swap element at index1 with element at index2
public void Swap(int index1, int index2)
{
int temp;
temp = myIntArray[index1];
myIntArray[index1] = myIntArray[index2];
myIntArray[index2] = temp;
}
// Sort all elements in ascending order
public void SortAscending()
{
int done = 0;
while(done == 0)
{
done = 1;
for(int i = 0; i < myIntArray.Length - 1; i++)
{
if(myIntArray[i] > myIntArray[i+1])
{
Swap(i, i+1);
done = 0;
}
}
}
}
// Sort all elements in descending order
public void SortDescending()
{
int done = 0;
while(done == 0)
{
done = 1;
for(int i = 0; i < myIntArray.Length - 1; i++)
{
if(myIntArray[i] < myIntArray[i+1])
{
Swap(i, i+1);
done = 0;
}
}
}
}
// Restore original array
public void Reset()
{
for(int i = 0; i < myIntArray.Length; i++)
{
myIntArray[i] = myIntArrayBackup[i];
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|