none
Syntax for calling a method with a return value of double array[] RRS feed

  • Question

  • I have the following code which I am receiving a syntax error. Have a method CalcStormTarget that is expecting 4 doubles to be passed in and array of doubles to be returned. Correct Syntax would be appreciated.

      double[] LatLongs = CalcStormTarget(Lathold, Longhold, stormHeading, StormSpeedhold);


    Thanks in advance - Dan


    • Edited by DDL19 Friday, August 18, 2017 5:19 PM
    Friday, August 18, 2017 5:19 PM

Answers

All replies

  • I have the following code which I am receiving a syntax error.


    Show the actual code, and the precise error message.
    Friday, August 18, 2017 6:49 PM
  •            
                double[] LatLongs = CalcStormTarget(Lathold, Longhold, stormHeading, StormSpeedhold);
    2017-08-18T19:03:48.972 run.csx(62,34): error CS0029: Cannot implicitly convert type 'double' to 'double[]'


    Friday, August 18, 2017 7:04 PM
  • The error indicates that you're trying to store a double into a double[]. The method signature will indicate the issue. It could be a parameter or return value causing the issue. Given that it is in column 34 I'd lean toward it being one of the argument you're passing is a double but the method is expecting a double[] as a parameter. Assuming the 4 arguments you're passing in are doubles the method signature would need to lool like this.

    public double[] CalcStormTarget ( double latHold, double, longHold, double stormHeader, double stormSpeedHold )
    Verify each of your arguments is of the correct type.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Friday, August 18, 2017 7:28 PM
    Moderator
  • This is my method call....

                double[] LatLongs = CalcStormTarget(Lathold, Longhold, stormHeading, StormSpeedhold);

    the error message:

    run.csx(62,34): error CS0120: An object reference is required for the non-static field, method, or property 'CalcStormTarget(double, double, double, double)'

    the method:

    public double[] CalcStormTarget(double lat, double longitude, double stormHeading, double stormSpeed)

    Friday, August 18, 2017 8:49 PM
  • That isn't the error you reported initially. The error you're getting now is because you're trying to call the method CalcStormTarget which is an instance method in (probably) a static method of the same class.

    public class MyType
    {
       public static void MyMethod ()
       {
          //compiler error - cannot access an instance method
          //inside a static member
          var LatLongs = CalcStormTarget(...);
       }
    
       public double[] CalcStormTarget (... )
       {
       }
    }

    Static members can only access other static members. Given your method names and whatnot it appears that maybe you're creating a class that contains all static members. I don't generally think that is a good idea so you should probably consider making all the members instances (remove the static) and then create an instance of the class in the code that ultimately uses this stuff. 

    public class MyType
    {
       public void MyMethod ()
       {
          var LatLongs = CalcStormTarget(...);
       }
    
       public double[] CalcStormTarget (... )
       {
       }
    }
    
    //Later in your code
    var myData = new MyType();
    ...
    myData.MyMethod();

    However if you really want the members to be static then mark the CalcStormTarget method as static as well. However anything it references also needs to be static. If all the members of the type are static then mark the class as static so the compiler catches this more easily.

    public static class MyType
    {
       public static void MyMethod ()
       {
          var LatLongs = CalcStormTarget(...);
       }
    
       public static double[] CalcStormTarget (... )
       {
       }
    }

    Michael Taylor

    http://www.michaeltaylorp3.net

    Friday, August 18, 2017 9:13 PM
    Moderator
  • I don't have any as static.......
    Friday, August 18, 2017 9:20 PM
  • my code:

      double[] LatLongs = CalcStormTarget(Lathold, Longhold, stormHeading, StormSpeedhold);

    error:

    run.csx(62,34): error CS0120: An object reference is required for the non-static field, method, or property 'CalcStormTarget(double, double, double, double)'
    method:

    public double[] CalcStormTarget(double lat, double longitude, double stormHeading, double stormSpeed)

    return:

                double[] DestinationLatLong;      
                return DestinationLatLong;

    Friday, August 18, 2017 9:33 PM
  • Can you post the complete type definition (method signatures included but not the bodies) of the type that contains the code that is failing to compile? Also post the exact exception you're getting and the line it is occurring on.

    One thing that I'm concerned about here is that you're posting in the C# forum but your problem isn't C#. You're using .csx which is C# script. This really isn't the forum for that. But Azure Functions use csx to run the C# code you post so I can see where that might be coming from.


    Also post the body of the method that is failing.
    Friday, August 18, 2017 9:33 PM
    Moderator
  • just did above....
    Friday, August 18, 2017 9:40 PM
  • No. You're providing piecemeal code. This isn't going to help us figure out why your code is failing. Please copy the entire file that contains this code. The only portion of the code to leave out is the method bodies as they aren't relevant. But we need to see the entire method body that contains the call to your CalcStormtarget because something before or after it could be failing. The last bit of code you posted shouldn't even compile as you're creating an uninitialized object and then immediately trying to return it. That is a compiler error.
    Friday, August 18, 2017 9:49 PM
    Moderator
  • my code:

      double[] LatLongs = CalcStormTarget(Lathold, Longhold, stormHeading, StormSpeedhold);

    error:

    run.csx(62,34): error CS0120: An object reference is required for the non-static field, method, or property 'CalcStormTarget(double, double, double, double)'

    It is telling you "CalcStormTarget is a instance function. Why did you not give me the instance to call it on? I am a computer, not a psychic damit!"

    And oddly enough we are not psychic either.

    What you show us indicates that you either have a serious issue to understand our requests for information or do not understand basic level C# coding. Running a 4 parameter, double returning Function seems way beyond your skill level.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Sunday, August 20, 2017 12:51 AM
  • Sorry I am not a genius like you. Got the answer in 2 minutes in the Azure Functions forum. Needed the called routine to be static. 

    Have a good day and thank you for your constructive criticism. 

    Sunday, August 20, 2017 2:58 PM
  • @DDL, on Friday @CoolDadTx had suggested that you might need to make the CalcCustomStorm() method static. I guess you never tried that?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by DDL19 Sunday, August 20, 2017 4:24 PM
    Sunday, August 20, 2017 4:07 PM
    Moderator
  • I must not have tried the right combination. Thank you all for your help.
    Sunday, August 20, 2017 4:33 PM