none
Arrays as parameters (Newbie question) RRS feed

  • Question

  • I'll apologise in advance asking so many beginner questions to the forum. So I'm working my way through my C# book and I'm a little stuck on one particular part. The book says the following:

    However, I get this error:

    I'm not sure why the method is flagged as an error.

    I've also tried putting 

     "int[] myArray = { 1, 2, 3, 4, 5 };
      PrintFirstElement(myArray);"

    in MyClass instead, but I still get an error. Can anyone shed some light or point to something obvious that I've missed? Thanks in advance.

               
    Friday, October 6, 2017 10:08 PM

Answers

  • Hello,

    You are attempting to call PrintFirstElement method of the class MyClass. You need to create an instance of MyClass e.g. var mc = new MyClass() then call the method e.g. mc.PrintFirstElement(myArray)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites





    Friday, October 6, 2017 10:24 PM
    Moderator
  • Don't know what book you are talking about nor do I read books on programming so I can't say.

    Bottom line is each book is put together in the mind set of the author. Best thing I can say is a book is but part of the learning process, you need to hone skills on your own (sadly to say).

    Any ways you did the right thing by asking while you should not post code as an image if you expect now or in another question for someone to possibly copy your code, correct it and present the fixed version of the code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Clydey2Times Friday, October 6, 2017 10:51 PM
    Friday, October 6, 2017 10:44 PM
    Moderator

All replies

  • Hello,

    You are attempting to call PrintFirstElement method of the class MyClass. You need to create an instance of MyClass e.g. var mc = new MyClass() then call the method e.g. mc.PrintFirstElement(myArray)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites





    Friday, October 6, 2017 10:24 PM
    Moderator
  • I actually thought I'd tried a version of this. I changed it to a static method, but it was still flagged as an error. I forgot to put "MyClass." in front of it, it seems. Thanks for the help!

    Does the book read as unclear to you? I'm wondering if it's just me. It's a beginner's book, so I expected it to make the explanation a little clearer.

    Friday, October 6, 2017 10:35 PM
  • Don't know what book you are talking about nor do I read books on programming so I can't say.

    Bottom line is each book is put together in the mind set of the author. Best thing I can say is a book is but part of the learning process, you need to hone skills on your own (sadly to say).

    Any ways you did the right thing by asking while you should not post code as an image if you expect now or in another question for someone to possibly copy your code, correct it and present the fixed version of the code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Clydey2Times Friday, October 6, 2017 10:51 PM
    Friday, October 6, 2017 10:44 PM
    Moderator
  • I was referring to the passage of the book I posted an image of. I wanted to know if you felt that part was clear.

    Thanks for the help. It's much appreciated. I'll C&P the code next time, rather than inserting an image.

    Friday, October 6, 2017 10:51 PM