12 Replies - 6501 Views - Last Post: 24 April 2011 - 01:30 PM Rate Topic: -----

#1 BabyShark011  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 20
  • Joined: 10-July 10

C# using out in method parameter list with array as argument

Posted 24 April 2011 - 05:08 AM

I am trying to implement a method that uses out in the parameter list for an array.
Example: "static public int GetMax(int[] tstArr, out int[] maxValInd)"

I keep getting the following errors:
Error 1 Use of unassigned out parameter 'maxValInd'
Error 2 The out parameter 'maxValInd' must be assigned to before control leaves the current method

I have tried everything to get this accepted by the compiler. The only thing I can make work is just passing an integer as an experiment.

The code is as follows:

static void Main(string[] args)
        {
            Arrays arrays = new Arrays();
            int[] testArray = { 4, 7, 1, 4, 2, 1, 8, 7 };
            int[] maxValIndices = new int[8];

            Console.WriteLine(" Index\tData\tHistogram");
            Console.WriteLine();
            for (int i = 0; i < testArray.Length; i ++ )
            {
                Console.Write("   {0}\t{1}\t", i, testArray[i]);
                for(int ii = 0; ii < testArray[i]; ii++)
                    Console.Write("*");
                Console.WriteLine();
            }
            Console.WriteLine("\nMaximum value = " + 
                "{0} found at element indices: {1}", testArray.Max(), 
                Array.BinarySearch(testArray, testArray.Max()));

            testArray.CopyTo(maxValIndices, 0);
            GetMax(testArray, out maxValIndices);
            
            Console.WriteLine();
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }

        static public int GetMax(int[] tstArr, out int[] maxValInd)
        {
            int myRetVal = 1;
            tstArr.CopyTo(maxValInd, 0);
            return myRetVal;
        }



The error is at: tstArr.CopyTo(maxValInd, 0);

Any help on this would be appreciated! Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: C# using out in method parameter list with array as argument

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 05:24 AM

Hi,

You need to assign a value to the out parameter, by initialising it to a new int array:

a) Before you use it in the method (using it in the CopyTo() method counts as 'using it').

You have assigned an instance to it before you pass it into the method, but in the method, it is initially treated as an unassigned local variable, so you have to 're-assign' it in the method.

b ) Before you return from the method (which should really be a consequence of the first point really)


Also note that calling methods like CopyTo() is an indefinite, indirect assignment. You need to actually use the '=' operator to assign the value when using 'out' parameters.

This post has been edited by CodingSup3rnatur@l-360: 24 April 2011 - 06:23 AM

Was This Post Helpful? 1
  • +
  • -

#3 BabyShark011  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 20
  • Joined: 10-July 10

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 06:01 AM

This a class lab. The instructor is trying to demonstrate how to us 'out'. To CodingSup3rnatur@l-360, hi, thanks for the input. Could you include a small snipet of code that illustrates your point. Much thanks.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 06:09 AM

Read the manual!
Was This Post Helpful? 0
  • +
  • -

#5 BabyShark011  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 20
  • Joined: 10-July 10

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 09:00 AM

Thank you to jackofalltrades. Your answer was so helpful. I see you have been in the system for a long time. You explain things in a consise and well worded way. Thank you again. By the way this is c# and you directed me to c and c++ manuals. I have three manuals and none of them covers passing an array by using out. I have looked on the web extensively. But you are a bright shiny example of help here.
Thank you again for your incredible assistance!
Was This Post Helpful? -4
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 09:51 AM

The link goes to C# code, dude. With an exact example and everything. That's insufficient for you?
Was This Post Helpful? 0
  • +
  • -

#7 BabyShark011  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 20
  • Joined: 10-July 10

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 09:57 AM

To jackofalltrades: Please do not reply to any of my posts. I really don't need your type of help. Thank you. I have reported you to the moderator.
Was This Post Helpful? -4
  • +
  • -

#8 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 10:49 AM

"Your kind of help"? Dude...he linked you to working source code. What more do you want?! And he is a moderator...he knows what he's doing. I'm sorry if you found it rude, but truly, he's trying to show you a valid, good source of information. MSDN IS the definitive resource for Microsoft-based languages.
Was This Post Helpful? 2
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,857
  • Joined: 27-December 08

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 11:26 AM

I'm pretty sure at this point, this applies:
Posted Image
Was This Post Helpful? 1
  • +
  • -

#10 BabyShark011  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 20
  • Joined: 10-July 10

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 11:36 AM

You people have no class! So long. I wonder whether any of you have real jobs.
Was This Post Helpful? -2
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 11:41 AM

What do you want from really want from us?

We've provided resources which answer your issue, yet you're not satisfied.
Do you want us to do all your course work for you, so you can sit back and jerk off around?
Was This Post Helpful? 0
  • +
  • -

#12 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 11:45 AM

@macosxnerd101 ,

WTF is that :D

@OP

Quote

Thank you to jackofalltrades. Your answer was so helpful. I see you have been in the system for a long time. You explain things in a consise and well worded way. Thank you again. By the way this is c# and you directed me to c and c++ manuals. I have three manuals and none of them covers passing an array by using out. I have looked on the web extensively. But you are a bright shiny example of help here.
Thank you again for your incredible assistance!


Well that is C# , if you dont know that , then you should not be studying the out keyword now ..

Anyway to make this topic somewhat useful i will give the answer

/* Some method you have */
void Get()
{
 
int sum ;//Declare DataType

Add(10,12,out sum);//This will return sum 

Console.Write(sum);//This will now contain a value of 22

}

void Add(int x, int i,out sum)//Sum will now  be returned.
{
 //assign value to int because it was empty
  
  i = x + i;//Returns i 

}


BTW:

This is not needed in this case
it can easily be written..

int Add(int x , int i)
{
  return x + i;
}



The only reason you want to use "out" is when you want to return more that 1 value

This post has been edited by marinus: 24 April 2011 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: C# using out in method parameter list with array as argument

Posted 24 April 2011 - 01:30 PM

View PostDogstopper, on 24 April 2011 - 01:49 PM, said:

I'm sorry if you found it rude, but truly, he's trying to show you a valid, good source of information. MSDN IS the definitive resource for Microsoft-based languages.


THIS. I wanted to point you to the most valuable treasure trove of information available on the web for C# and Microsoft development: the Microsoft Developers' Network or MSDN. Everything you could want to know about C# is right there for the taking. Every Windows developer SHOULD know about MSDN, because every developer should be as self-sufficient as possible and adept at using the vast amount of information available to him.

I use so many different tools and languages in my "real job" (yes, I do have one, thank you very much) on a daily basis that I live on Google or MSDN. Knowing how to find what you need is as important as knowing how to write the code.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1