locked
Create a Function that gives an Array as Result. RRS feed

  • Question

  • Dear Friends,

    I want to create a function with two Arguments (such as 'SunLongitude' as Double & 'MoonLongitude' as Double) that gives an Array (of Integer) as result. The function should do the following calculations;

     Dim PhaseLongitude As Double
     Dim PhaseStartLongitude, PhaseEndLongitude As Integer
     
         If SunLongitude < MoonLongitude Then
            PhaseLongitude = MoonLongitude - SunLongitude
         If SunLongitude > MoonLongitude Then
            PhaseLongitude =  SunLongitude - MoonLongitude
            End If
    
    
            If PhaseLongitude >= 0 And PhaseLongitude <= 12 Then
                PhaseStartLongitude = 0
                PhaseEndLongitude = 12
            ElseIf PhaseLongitude >= 12 And PhaseLongitude <= 24 Then            
                PhaseStartLongitude = 12
                PhaseEndLongitude = 23
            ElseIf PhaseLongitude >= 24 And PhaseLongitude <= 36 Then
                PhaseStartLongitude = 24
                PhaseEndLongitude = 36
            ElseIf PhaseLongitude >= 36 And PhaseLongitude <= 48 Then
                   ....................................
                   ....................................
            ElseIf PhaseLongitude >= 168 And PhaseLongitude <= 180 Then
                PhaseStartLongitude = 168
                PhaseEndLongitude = 180
            End If
    

    I want "PhaseStartLongitude" & PhaseEndLongitude" as the result.

    [I understand that the "Result" of this Function should be an Array with two elements eg: Result(0) = PhaseStartLongitude & Result(1) = PhaseEndLongitude. Am I correct?]

    Please let me know how to write this Function.

    Thanks

    Regards

    VKSBK


    A Real Novice Programmer !

    Wednesday, June 10, 2020 1:18 PM

Answers

  • Hi

    Here is some code that may be what you want. I chose to use a Select..Case block,but if you really want to use If .... Then statements just change accordingly.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        Dim PhaseStartLongitude As Integer = getResults(88, 122)(0)
        Dim PhaseEndLongitude As Integer = getResults(88, 122)(1)
    
      End Sub
      Function getResults(SunLongitude As Double, MoonLongitude As Double) As Integer()
        Dim Phases(1) As Integer
        Dim PhaseLongitude As Double
        If SunLongitude < MoonLongitude Then
          PhaseLongitude = MoonLongitude - SunLongitude
          If SunLongitude > MoonLongitude Then
            PhaseLongitude = SunLongitude - MoonLongitude
          End If
        End If
        Select Case PhaseLongitude
          Case <= 12
            Phases(0) = 0
            Phases(1) = 12
          Case <= 24
            Phases(0) = 12
            Phases(1) = 23
          Case <= 36
            Phases(0) = 24
            Phases(1) = 36
            '
            '
            '
          Case <= 180
            Phases(0) = 168
            Phases(1) = 180
        End Select
    
        Return Phases
    
      End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, June 10, 2020 2:46 PM
    • Marked as answer by V.K.S.B.K Thursday, June 11, 2020 9:21 AM
    Wednesday, June 10, 2020 2:45 PM
  • Hi,
    try following console demo:

    Module Module1
    
      Sub Main()
        Try
          Call (New Demo).Execute()
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
      Friend Class Demo
    
        Friend Sub Execute()
          Try
            For i = 1 To 180 Step 13
              Dim result = CalculateLongitude(i)
              Console.WriteLine($"PhaseLongitude: {i}, PhaseStartLongitude: {result(0)}, PhaseEndLongitude: {result(1)}")
            Next
          Catch ex As Exception
            Console.WriteLine(ex.Message)
          End Try
        End Sub
    
        Private Function CalculateLongitude(PhaseLongitude As Double) As Integer()
          Dim result(1) As Integer
          result(0) = CInt(Math.Floor(PhaseLongitude / 12) * 12)
          result(1) = CInt(Math.Ceiling(PhaseLongitude / 12) * 12)
          Return result
        End Function
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Wednesday, June 10, 2020 3:00 PM
    • Marked as answer by V.K.S.B.K Thursday, June 11, 2020 9:21 AM
    Wednesday, June 10, 2020 3:00 PM
  • In modern VB you can also use tuples in this manner:

    Function Calculate(SunLongitude As Double, MoonLongitude As Double) As (PhaseStartLongitude As Integer, PhaseEndLongitude As Integer)

        Dim PhaseStartLongitude, PhaseEndLongitude As Integer

        ' Calculate PhaseStartLongitude and PhaseEndLongitude

        . . .

        Return (PhaseStartLongitude, PhaseEndLongitude)

    End Function


    Usage:

    Dim result = Calculate(-114, 172)

    Console.WriteLine(result.PhaseStartLongitude)

    Console.WriteLine(result.PhaseEndLongitude)


    • Edited by Viorel_MVP Wednesday, June 10, 2020 4:55 PM
    • Marked as answer by V.K.S.B.K Thursday, June 11, 2020 9:22 AM
    Wednesday, June 10, 2020 4:45 PM

All replies

  • Hi

    Here is some code that may be what you want. I chose to use a Select..Case block,but if you really want to use If .... Then statements just change accordingly.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        Dim PhaseStartLongitude As Integer = getResults(88, 122)(0)
        Dim PhaseEndLongitude As Integer = getResults(88, 122)(1)
    
      End Sub
      Function getResults(SunLongitude As Double, MoonLongitude As Double) As Integer()
        Dim Phases(1) As Integer
        Dim PhaseLongitude As Double
        If SunLongitude < MoonLongitude Then
          PhaseLongitude = MoonLongitude - SunLongitude
          If SunLongitude > MoonLongitude Then
            PhaseLongitude = SunLongitude - MoonLongitude
          End If
        End If
        Select Case PhaseLongitude
          Case <= 12
            Phases(0) = 0
            Phases(1) = 12
          Case <= 24
            Phases(0) = 12
            Phases(1) = 23
          Case <= 36
            Phases(0) = 24
            Phases(1) = 36
            '
            '
            '
          Case <= 180
            Phases(0) = 168
            Phases(1) = 180
        End Select
    
        Return Phases
    
      End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, June 10, 2020 2:46 PM
    • Marked as answer by V.K.S.B.K Thursday, June 11, 2020 9:21 AM
    Wednesday, June 10, 2020 2:45 PM
  • Hi,
    try following console demo:

    Module Module1
    
      Sub Main()
        Try
          Call (New Demo).Execute()
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
      Friend Class Demo
    
        Friend Sub Execute()
          Try
            For i = 1 To 180 Step 13
              Dim result = CalculateLongitude(i)
              Console.WriteLine($"PhaseLongitude: {i}, PhaseStartLongitude: {result(0)}, PhaseEndLongitude: {result(1)}")
            Next
          Catch ex As Exception
            Console.WriteLine(ex.Message)
          End Try
        End Sub
    
        Private Function CalculateLongitude(PhaseLongitude As Double) As Integer()
          Dim result(1) As Integer
          result(0) = CInt(Math.Floor(PhaseLongitude / 12) * 12)
          result(1) = CInt(Math.Ceiling(PhaseLongitude / 12) * 12)
          Return result
        End Function
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    • Edited by Peter Fleischer Wednesday, June 10, 2020 3:00 PM
    • Marked as answer by V.K.S.B.K Thursday, June 11, 2020 9:21 AM
    Wednesday, June 10, 2020 3:00 PM
  • In modern VB you can also use tuples in this manner:

    Function Calculate(SunLongitude As Double, MoonLongitude As Double) As (PhaseStartLongitude As Integer, PhaseEndLongitude As Integer)

        Dim PhaseStartLongitude, PhaseEndLongitude As Integer

        ' Calculate PhaseStartLongitude and PhaseEndLongitude

        . . .

        Return (PhaseStartLongitude, PhaseEndLongitude)

    End Function


    Usage:

    Dim result = Calculate(-114, 172)

    Console.WriteLine(result.PhaseStartLongitude)

    Console.WriteLine(result.PhaseEndLongitude)


    • Edited by Viorel_MVP Wednesday, June 10, 2020 4:55 PM
    • Marked as answer by V.K.S.B.K Thursday, June 11, 2020 9:22 AM
    Wednesday, June 10, 2020 4:45 PM
  • Dear  leshay, Peter Fleischer,  Viorel_,

    Thank you very much for the help.

    Thanks

    Regards

    VKSBK


    A Real Novice Programmer !

    Thursday, June 11, 2020 9:24 AM