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();
Related
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)
How can I initialize array for this generic class please help
public class MarvellousArray<T>
{
private T[] array;
public MarvellousArray(int size)
{
array = new T[size];
}
public void Accept()
{
var i = 0;
for (i = 0; i < array.Length; i++)
{
}
}
public void Display()
{
}
}
If i understand what you mean... You could do this if you need to new T
public class MarvellousArray<T>
where T : new()
{
private readonly T[] _array;
public MarvellousArray(int size)
{
_array = new T[size];
for (var i = 0; i < size; i++)
_array[i] = new T();
}
...
or just default which will be null for reference types
public class MarvellousArray<T>
{
private readonly T[] _array;
public MarvellousArray(int size)
{
_array = new T[size];
for (var i = 0; i < size; i++)
_array[i] = default(T);
}
...
If you want to initialize any array, there is the whole array Initilazier Syntax:
All possible array initialization syntaxes
The question is really what you want to Initialize it with.
The default value? ckuri pointed out that in most cases Array are initialied with that already.
A Specific value?
public static void setAll(int[] array, int value){
for(int i = 0; i<array.Lenght;i++)
array[i]=value;
}
I wrote this from memory. I am unsure if it was Size or Lenght with arrays. You could improve it with stuff like generics. Or a integer variable on how many times you want the value written.
Do you want them of a minimim size? Use List[T] and just define the minimum size with the constructor overload.
The values of another Array? List.AddRange is there for that.
Do you look for Lazy Initializsation? Then look at Lazy[T]. Alterantively you could write your class Array accessors. And initialize a empte value before you hand it out. Same way people do it with properties.
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);
}
I am trying to get the output of "1111111111".
The Message function is called by AddMessage and it stores the message into an array.
However when i output the array values, I get the address instead of the value. How do i fix this?
class Program
{
public delegate int print();
public static void Main()
{
print[] array1 = new print[10];
AddMessage(ref array1, Message);
for (int i = 0; i < 10; i++)
{
Console.WriteLine(array1[i]);
}
}
public static void AddMessage(ref print[] array, print msg)
{
for(int i =0; i< 10; i++)
{
array[i] = msg;
}
}
public static int Message()
{
int msg;
msg = 1;
return msg;
}
}
}
A delegate is a function, you're passing a reference to the function itself (not the result) into Console.WriteLine.
Console.WriteLine(array1[i]);
must turn into
Console.WriteLine(array1[i]());
You're printing the object name because you're not calling the delegate:
for (int i = 0; i < 10; i++)
{
Console.WriteLine(array1[i]);
}
You should change this to array1[i]()
How can I make an array with 400 elements of type myClass and pass different args to each of them?
I have two classes: mainClass and myClass. I want to create the array in mainClass. as you can see myClass needs 3 args.
myClass:
namespace prj1
{
class myClass
{
public myClass(int A, int B int C)
{
...
...
...
}
}
}
mainClass:
namespace prj1
{
class mainClass
{
public myClass[] myVar = new myClass[400];
public mainClass(int y, int m, int d)
{
...
...
...
}
}
}
If I have to use setValue to initialize them how can I do this? How should I pass 3 args?
for (int i = 0; i < 400; i++)
{
myVar.SetValue(object Value, i);
}
Why don't you just construct each instance within the loop you already have?
for(int i = 0; i < myVar.Length; i++)
{
myVar[i] = new myClass(arg1, arg2, arg3);
}
You have an appropriate constructor already, and you're initializing within a loop... 1 + 1 == 2, let's not try and reinvent the wheel shall we?
Try this,
for (int i = 0; i < 400; i++)
{
myVar[i]=new MyClass(y,m,d);
}
//or
for (int i = 0; i < 400; i++)
{
myVar.SetValue(new MyClass(y,m,d),i);
}
for (int i = 0; i < 400; i++)
{
myClass tempObject = new myClass(y,m,d);
myVar.SetValue(tempObject,i)
}