Skip to content

Functions

sancalle edited this page Oct 14, 2022 · 2 revisions

Typescript

function doFoo(): void {
}

C#

In C# those are wrapped into a static class

namespace Codeverter
{
    public static class Helper
    {
        public static void DoFoo()
        {
            return;
        }
    }
}

GO

package codeverter

func DoFoo() {
	return
}

Visual Basic

In Visual Basic those are wrapped into a Module

Namespace Codeverter
    Module Helper
        Public Sub DoFoo()
        End Sub
    End Module
End Namespace

Body

The body of the function is not converted. It is written as a comment to help with the manual migration.

function fibonacci(num: number): number {
    if (num <= 1) {
        return 1;
    }
    return fibonacci(num - 1) + fibonacci(num - 2);
}

C#

public static int Fibonacci(int num)
{
    //     if (num <= 1) {
    //         return 1;
    //     }
    //     return fibonacci(num - 1) + fibonacci(num - 2);
    return 0;
}

GO

func Fibonacci(num int) int {
	//    if (num <= 1) {
	//        return 1;
	//    }
	//    return fibonacci(num - 1) + fibonacci(num - 2);
	return 0
}

Visual Baisic

Public Function Fibonacci(num As Integer) As Integer
    ' if (num <= 1) {
    ' return 1
    ' }
    ' return fibonacci(num - 1) + fibonacci(num - 2)
    return 0
End Function

Variable/Constant declaration

The declared variables and constants are converted. If those were declared and assigned by a constant value it will be considered. If no type was provided, for example let varNum = 10; the type will be inferred (as numeric in this case).

function declareVars(): void {
    let constValue = "random string";
    let varNum = 10;
    let varBool = true;
    let varDate = new Date();
    let varArray = [10]
    let varRef = Crypto
}

C#

Variables

  • string
  • number
  • boolean
  • date => initialized with default value and the original one as a comment
  • array
  • reference => initialized with null and the original one as a comment

Constants

  • string
  • number
  • boolean
  • date => migrated as variable
  • array => migrated as variable
  • reference => migrated as variable
public static void DeclareVars()
{
    string varStr = "random string";
    int varNum = 10;
    bool varBool = true;
    DateTime varDate = new DateTime(); //new Date()
    int[] varArray = new int[] { 10 };
    Crypto varRef = null; //Crypto
    //     let varStr = "random string";
    //     let varNum = 10;
    //     let varBool = true;
    //     let varDate = new Date();
    //     let varArray = [10]
    //     let varRef = Crypto
    return;
}

GO

Variables

  • string
  • number
  • boolean
  • date => initialized with default value and the original one as a comment
  • array
  • reference => initialized with a new instance and the original one as a comment

Constants

  • string
  • number
  • boolean
  • date => migrated as variable
  • array => migrated as variable
  • reference => migrated as variable
func DeclareVars() {
	varStr := "random string"
	varNum := 10
	varBool := true
	varDate := time.Now() // new Date()
	varArray := []int{10}
	varRef := new(Crypto) // Crypto
	//    let varStr = "random string";
	//    let varNum = 10;
	//    let varBool = true;
	//    let varDate = new Date();
	//    let varArray = [10]
	//    let varRef = Crypto
	return
}

Visual Basic

Variables

  • string
  • number
  • boolean
  • date => initialized with default value and the original one as a comment
  • array
  • reference => initialized with null and the original one as a comment

Constants

  • string
  • number
  • boolean
  • date => migrated as variable
  • array => migrated as variable
  • reference => migrated as variable
Public Sub DeclareVars()
    Dim constValue As String = "random string"
    Dim varNum As Integer = 10
    Dim varBool As Boolean = true
    Dim varDate As Date = new DateTime() 'new Date()
    Dim varArray As Integer() = { 10 }
    Dim varRef As Crypto = Crypto 'Crypto
    ' let constValue = "random string"
    ' let varNum = 10
    ' let varBool = true
    ' let varDate = new Date()
    ' let varArray = [10]
    ' let varRef = Crypto
End Sub

Return

  • If no return was provided at the function signature, it can be inferred by the return statement if it is constant.
  • The return statement is assigned with a default value
function getOffset() {
    return 100;
}

C#

public static int GetOffset()
{
    //     return 100;
    return 0;
}

GO

func GetOffset() int {
	//    return 100;
	return 0
}

Visual Basic

Public Function GetOffset() As Integer
    ' return 100
    return 0
End Function

Parameters

Parameters must have a type. We cannot infer the type yet.

function sum(val1: number, val2: number): number {
    return val1 + val2;
}

C#

public static int Sum(int val1, int val2)
{
    //     return val1 + val2;
    return 0;
}

GO

func Sum(val1 int, val2 int) int {
	//    return val1 + val2;
	return 0
}

Visual Basic

Public Function Sum(val1 As Integer, val2 As Integer) As Integer
    ' return val1 + val2
    return 0
End Function

Clone this wiki locally