Witam. Wie ktoś jak przekazać tablice do funkcji w taki sposób aby nie była traktowana jako referencja?
Z tego co wyczytałem to domyślnie przekazywane argumenty powinny być traktowane jako wartość czyli jak rozumiem wewnątrz funkcji ytest\xtest powinna być utworzona lokalna kopia przekazanego argumentu. Chyba że coś pokręciłem.
Dlatego nie rozumiem dlaczego w poniższym przykładzie wartość przekazywana do funkcji xtest jest traktowana jako referencja a dla ytest jako wartość. Mógłby mi ktoś wyjaśnić co robię źle?
static void Main()
{
int y = 0;
Console.WriteLine(Ytest(y)); // = 2
Console.WriteLine(y); // =0
int[] x = new int[3];
Console.WriteLine((Xtest(x))); // = 2
Console.WriteLine(x[0]); // =2
Console.ReadKey();
}
static public int Ytest( int argument)
{
argument = 2;
return argument;
}
static public int Xtest(int[] argument)
{
argument[0] = 2;
return argument[0];
}
}
Sub Main()
Dim y As Integer = 0
Console.WriteLine(Ytest(y)) '= 2
Console.WriteLine(y) '=0
Dim x(1) As Integer
Console.WriteLine((Xtest(x))) '= 2
Console.WriteLine(x(0)) '=2
Console.ReadKey()
End Sub
Function Ytest(ByVal argument As Integer) As Integer
argument = 2
Return argument
End Function
Function Xtest(ByVal argument() As Integer) As Integer
argument(0) = 2
Return argument(0)
End Function