Why does ResizeArray not work in my code? - c#

I have a problem with the code:
namespace hello
{
public class Program
{
public static void Main(string[] args)
{
int xx = 5;
string[,] myArray = new string[1, 5];
if (xx > 4)
{
ResizeArray(ref myArray, 4, 5);
}
else
{
ResizeArray(ref myArray, 2, 5);
}
}
void ResizeArray(ref string[,] original, int rows, int cols)
{
string[,] newArray = new string[rows, cols];
Array.Copy(original, newArray, original.Length);
original = newArray;
}
}
}
I get the error message:
An object reference is required for the non-static field, method, or
property 'hello.Program.ResizeArray(ref string[,], int, int)'

Static members can't access non-static member without creating an instance.
You just need:
static void ResizeArray(ref string[,] original, int rows, int cols)

Related

Constructor does not pass initial array values to the method

I would like an explanation what I'm doing wrong. I paste two block of codes; one is working another is not working. The first code works becasue the initial values are passed into the method "MyGreatMethod" I can see them:
public class Something
{
private int [] MyArray = new int [3];
public Something()
{
MyArray[0] = 100;
MyArray[1] = 200;
MyArray[2] = 300;
}
public void MyGreatMethod()
{
Console.WriteLine(MyArray[0] / 3);
Console.WriteLine(MyArray[1] / 3);
Console.WriteLine(MyArray[2] / 3);
}
}
but if I place values into "MyArray" this way(see below), the method "MyGreatMethod" gets NULLs from constructor, what I do wrong? Please help..
public class Something
{
private int [] MyArray = new int [3];
public Something()
{
int[] MyArray = {100,200,300};
}
public void MyGreatMethod()
{
Console.WriteLine(MyArray[0] / 3);
Console.WriteLine(MyArray[1] / 3);
Console.WriteLine(MyArray[2] / 3);
}
}
It is null because you haven't assigned the values to MyArray. Instead, you have created a new array and assigned it to a local variable with the same name.
You should remove the int[] from the constructor:
public class Something
{
private int [] MyArray = new int [3];
public Something()
{
MyArray = {100,200,300};
}
public void MyGreatMethod()
{
Console.WriteLine(MyArray[0] / 3);
Console.WriteLine(MyArray[1] / 3);
Console.WriteLine(MyArray[2] / 3);
}
}
You need to use new keyword when you want to set values for a specific array after the declaration:
public class Something
{
private int[] MyArray;
public Something()
{
MyArray = new int[3] { 100, 200, 300 };
}
public void MyGreatMethod()
{
Console.WriteLine(MyArray[0] / 3);
Console.WriteLine(MyArray[1] / 3);
Console.WriteLine(MyArray[2] / 3);
}
}

Is there a way to count the index values of duplicate numbers in a binary search?

I want to be able to count multiple entries in an already sorted array. In previous attempts, I've tried to use a list like this.
public static List<int> duplicate = new List<int>();
However, it wouldn't count properly and kept printing the same index number. I'm pretty much stumped on what to do at this point any help would be appreciated.
class binarysearch
{
public static int lowestIndex = 0;
public static int highestIndex;
public static int middleIndex = 0;
public static int indexValue = -1;
public static int binarySearch(double[] data, double target)
{
int highestIndex = data.Length - 1;
while (lowestIndex < highestIndex)
{
middleIndex = lowestIndex + (highestIndex - lowestIndex) / 2;
if (data[middleIndex] == target)
{
indexValue = middleIndex;
highestIndex = middleIndex;
}
else if (data[middleIndex] > target)
{
highestIndex = middleIndex;
}
else
{
lowestIndex = middleIndex + 1;
}
}
return indexValue;
}
}
edit
public static List<int> duplicate = new List<int>();
is declared earlier in the code in
class dataset
{
public static List<int> duplicate = new List<int>();
}
then printed later in the main method
foreach (object dupes in dataset.duplicate)
{
Console.WriteLine(dupes);
}

C# Overwrite string and string[] in another Function

I overwrite the variables 'arr[1]' and 'test' in setValues() function.
arr[1] is changed to 'BBB'
but test doesn't change to '222'
Output: BBB111
but it should be BBB222
Why string test doesn't get updated?
public class Program
{
static void Main(string[] args)
{
string[] arr = new string[10];
arr[1] = "AAA";
string test = "111";
setValues(arr, test);
int exit = -1;
while (exit < 0)
{
for (int i = 0; i < arr.Length; i++)
{
if (!String.IsNullOrEmpty(arr[i]))
{
Console.WriteLine(arr[i] + test);
}
}
}
}
private static void setValues(string[] arr, string test)
{
arr[1] = "BBB";
test = "222";
}
}
You need to pass that string by reference to be able to modify it in a method, you can do this by adding the ref keyword:
public class Program
{
static void Main(string[] args)
{
string[] arr = new string[10];
arr[1] = "AAA";
string test = "111";
setValues(arr, ref test);
int exit = -1;
while (exit < 0)
{
for (int i = 0; i < arr.Length; i++)
{
if (!String.IsNullOrEmpty(arr[i]))
{
Console.WriteLine(arr[i] + test);
}
}
}
}
private static void setValues(string[] arr, ref string test)
{
arr[1] = "BBB";
test = "222";
}
}
You are only altering the local reference to test in your setValues function. You would need to pass this variable by reference (ref)
private static void setValues(string[] arr, ref string test)
{
arr[1] = "BBB";
test = "222";
}
then call it like this:
setValues(arr, ref test);
It's because the test prameter in the setValues method is not marked as ref so it is changed only inside the method and the value does not get outside.
Because the array as object is passed by reference while the string is passed by value.
So, the function setValues() update the local copy of test, invisible to the caller, while update the ONLY instance of string[] arr, visible either to the caller than to the callee.
Paolo
The reason is variable scope. The variables referenced in your SetValues() method aren't the same variables referenced in Main.
There are 2 ways to resolve this, based on the need of the class:
As stated in other answers, pass the values by reference in the SetValues method (this preserves your code as-is):
private static void setValues(string[] arr, ref string test)
{
arr[1] = "BBB";
test = "222";
}
Move the variable declaration outside of the body of Main (This changes the scope of the variables to Class-level variables, which scopes them to the entire class, and possibly allows accessing them from other classes):
public class Program
{
static string[] arr = new string[10];
static string test = "111";
static void Main(string[] args)
{
arr[1] = "AAA";
and then call it by reference as well:
SetValues(arr, ref test);

Make sure data does not get cleaned up in a static class

Is there a way to have a static class have static data that does not clear itself at the end of the function call?
i.e. given:
static class Class1
{
static int[] _array;
static Class1()
{
_array = new[] {2};
}
public static void FillArray()
{
List<int> temp = new List<int>();
for(int i=0;i<100;i++)
temp.Add(i);
_array = temp.ToArray();
}
public static int[] GetArray()
{
return _array;
}
}
How can I get GetArray() to return something other than null?
EDIT: I want to call this code:
int[] array1 = Class1.GetArray();
for (int i = 0; i < array1.Length;i++ )
Console.WriteLine(array1[i]);
Class1.FillArray();
for (int i = 0; i < array1.Length; i++)
Console.WriteLine(array1[i]);
and not get two 2s. How can I make that happen?
int[] array1 = Class1.GetArray();
for (int i = 0; i < array1.Length;i++ )
Console.WriteLine(array1[i]);
Class1.FillArray();
for (int i = 0; i < array1.Length; i++)
Console.WriteLine(array1[i]);
In this code, you are getting the memory address of the first int[] {2} array and storing that as array1. Then when you call FillArray() you are creating the new List of arrays, and only setting its memory back to the _array in the class, not array1. That is not a reference to the memory in the class, but to the actual original array. So then, when you loop back through, you are still looking at the same memory block.
You should probably be doing this instead:
int[] array1 = Class1.GetArray();
for (int i = 0; i < array1.Length;i++ )
Console.WriteLine(array1[i]);
Class1.FillArray();
array1 = Class1.GetArray();
for (int i = 0; i < array1.Length; i++)
Console.WriteLine(array1[i]);
Update
if you change your Class1 to look like this, you will see that you are changing the data in the class.
static class Class1
{
static int[] _array;
static Class1()
{
_array = new[] { 2 };
}
public static void FillArray()
{
List<int> temp = new List<int>();
for (int i = 0; i < 100; i++)
{
temp.Add(i);
}
_array = temp.ToArray();
PrintArray();
}
public static int[] GetArray()
{
PrintArray();
return _array;
}
private static void PrintArray()
{
foreach (int i in _array)
{
System.Console.Write(String.Format("{0},", i));
}
}
}
Because it will print out the elements in the array after each call.
use a static constructor...
static class Class1
{
private static readonly int[] _array;
static Class1()
{
List<int> temp = new List<int>();
for(int i=0;i<100;i++)
temp.Add(i);
_array = temp.ToArray();
}
public static int[] GetArray()
{
return _array;
}
}
you can write something like
private static readonly int[] array = FillArray();

Get; Set; Variables

I'm setting up multiple methods and wondering how to continue to pass one variable (The "top" variable) to different methods.
Main method:
public static void Main(string[] args)
{
int[] anArray = new int[5];
int top = -1;
PushPeek(anArray);
then I need to pass top to:
public static void PushPeek(int[] ar)
{
if (ar[ar.Length -1] == ar.Length -1)
{
//do nothing
}
else
{
top = top + 1;
Console.WriteLine(ar[top]);
}
}\
I know it involves something with get; set; but I don't know how, any help?
Pass it by reference:
public static void PushPeek(int[] ar, ref int top)
{
...
}
int[] anArray = new int[5];
int top = -1;
PushPeek(anArray, ref top);
All about properties: http://msdn.microsoft.com/en-us/library/aa288470(v=vs.71).aspx
Auto-implemented properties are awesome! http://msdn.microsoft.com/en-us/library/bb384054.aspx

Categories

Resources