How can I assign values to a group of variables in an iteration?

  • Thread starter Thread starter M Shafaat
  • Start date Start date
M

M Shafaat

Hi!
I want to write a method for assigning values to a group of variables in an
iteration, something like the following code:


public static void AssignValsToVars (string [] MyValues, ref params int []
MyVariables)
// The MyValues and MyVariables must have the same length.
{
for (i = 0; i < MyValues.Length; i++)
{
MyVariables = MyValues;
}
}


The code as above gives an exception sayiong that params and ref attibutes
are not allowed to use together.


The reason for I want to do this in this way is that among other advantages,
the amount of code will be short, regardless of how many variables you want
to assign.


Do you know a way to achieve this which is allowed?


Regards
M Shafaat
 
M said:
Hi!
I want to write a method for assigning values to a group of variables in an
iteration, something like the following code:


public static void AssignValsToVars (string [] MyValues, ref params int []
MyVariables)
// The MyValues and MyVariables must have the same length.
{
for (i = 0; i < MyValues.Length; i++)
{
MyVariables = MyValues;
}
}


You don't need using ref for arrays. Arrays as strings are automatically
pass to method by reference.
Are you sure that length of MyVariables and MyValues arrays are equal?
 
Back
Top