CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: Object/dynamic Function breaks

Open docfresh opened this issue 4 years ago • 1 comments

Another case of a VB "object" needing to be converted to "dynamic". Not sure if there is a foolproof way to do this.

VB.Net input code

    Public Function IsPointWithinBoundaryBox(ByVal dblLat As Double, dblLon As Double, ByVal boundbox As Object) As Boolean
        If boundbox IsNot Nothing Then
            Dim boolInLatBounds As Boolean = (dblLat <= boundbox.north) And (dblLat >= boundbox.south) 'Less then highest (northmost) lat, AND more than lowest (southmost) lat
            Dim boolInLonBounds As Boolean = (dblLon >= boundbox.west) And (dblLon <= boundbox.east) 'More than lowest (westmost) lat, AND less than highest (eastmost) lon
            Return boolInLatBounds And boolInLonBounds
        Else
            'Throw New Exception("boundbox is null.")
            WriteToLog("boundbox is null", , eventType.etCritical, 0, 0, 0)
        End If

    End Function

Erroneous output

        public static bool IsPointWithinBoundaryBox(double dblLat, double dblLon, object boundbox)
        {
            if (boundbox is object)
            {
                bool boolInLatBounds = Conversions.ToBoolean(Operators.AndObject(Operators.ConditionalCompareObjectLessEqual(dblLat, boundbox.north, false), Operators.ConditionalCompareObjectGreaterEqual(dblLat, boundbox.south, false))); // Less then highest (northmost) lat, AND more than lowest (southmost) lat
                bool boolInLonBounds = Conversions.ToBoolean(Operators.AndObject(Operators.ConditionalCompareObjectGreaterEqual(dblLon, boundbox.west, false), Operators.ConditionalCompareObjectLessEqual(dblLon, boundbox.east, false))); // More than lowest (westmost) lat, AND less than highest (eastmost) lon
                return boolInLatBounds & boolInLonBounds;
            }
            else
            {
                // Throw New Exception("boundbox is null.")
                Master.WriteToLog("boundbox is null", intEventType: (int)eventType.etCritical, intUserID: 0, intCityID: 0, intIssueID: 0);
            }

            return default;
        }

Expected output

public bool IsPointWithinBoundaryBox(double dblLat, double dblLon, dynamic boundbox)
{
    if (boundbox != null)
    {
        bool boolInLatBounds = (dblLat <= boundbox.north) & (dblLat >= boundbox.south); // Less then highest (northmost) lat, AND more than lowest (southmost) lat
        bool boolInLonBounds = (dblLon >= boundbox.west) & (dblLon <= boundbox.east); // More than lowest (westmost) lat, AND less than highest (eastmost) lon
        return boolInLatBounds & boolInLonBounds;
    }
    else
        // Throw New Exception("boundbox is null.")
        WriteToLog("boundbox is null", null/* Conversion error: Set to default value for this argument */, eventType.etCritical, 0, 0, 0);
}

Details

CC 8.41

docfresh avatar Nov 04 '21 02:11 docfresh

I like the idea of using dynamic to cover VB's "late binding" concept. Rather than change the existing public signature, I think I'd create a local variable for it as dynamic and use that everywhere. Will need some thought though

GrahamTheCoder avatar Nov 04 '21 08:11 GrahamTheCoder