VBUC Documentation | Mobilize.Net
Mobilize.NetForumsBlogDocumentation Home
  • Mobilize.Net VBUC
  • Introduction
  • Install and Licenses
  • Get Started
  • Migration Guide
  • VBUC Features
  • Mappings Grammar
  • Generated Code
  • Upgrade Options
    • Data Access
    • Grids
    • Microsoft
    • Sheridan
    • Others
    • Code Conversion
    • C# Features
  • Third-party controls
  • Best Practices
  • EWIs
    • Warnings
    • Issues
    • ToDos
    • Notes
  • Issues and Troubleshooting
    • Microsoft.VisualBasic Uses
    • Safe and Unsafe Methods Layer
    • Third Party Components
    • Migration Process
    • Classic ADO Conversion to ADO.NET
    • VB6 and .NET integer division
    • VB6 On Error Statements
    • Running a .NET Core application in a machine with no Visual Studio installed
    • Databases issues
    • Unsupported assemblies on .NET Core and .NET 5
    • Icon Extraction Issues
    • HTTPS sites are not loaded in Windows XP
    • Short-Circuit Boolean Logic in C#
    • Assessment Report Issue
  • Knowledge Base
    • FAQ
      • Does the VBUC support the Sheridan VB 6.0 controls suit?
      • How effective is the Visual Basic Upgrade Companion tool at converting an application's front end?
      • What controls does the Visual Basic Upgrade Companion tool supports?
      • How does the VBUC handle VB6 Collections?
      • Can the VBUC migrate intrinsic VB6 functions and libraries?
      • Where is the source code for the support (helper) classes used by the VBUC?
      • How does the VBUC convert ADO to ADO.NET?
      • Can the Visual Basic Upgrade Companion tool be customized?
      • What are Stubs?
    • How-To
      • App.Config and Database Access
      • Avoid reflection in Hot Paths
      • Convert ESRI ArcGIS Visual Basic 6.0 applications to .NET
      • Drag and Drop Conversion Steps
      • Inserting elements in a ListView
      • String vs StringBuilder
      • Word Automation in VB6 vs .NET
      • Configure default Factory Database provider
      • GetPrivateProfileString Windows API working in migrated code
      • Upgrade projects with shared files
  • Release Notes
Powered by GitBook
On this page

Was this helpful?

  1. Issues and Troubleshooting

Short-Circuit Boolean Logic in C#

VB6 does not have short-circuit evaluation for Boolean operators. This means that all conditions of an And or Or operator will be evaluated.

In this example, both message boxes from the GetTrue and GetFalse methods will be shown.

Private Sub ShortCircuitTest()

    If GetFalse And GetTrue Then
        ...
    Else
        ...
    End If
    
    If GetTrue Or GetFalse Then
        ...
    Else
        ...
    End If
End Sub

Private Function GetTrue() As Boolean
    MsgBox "TRUE"
    GetTrue = True
End Function

Private Function GetFalse() As Boolean
    MsgBox "FALSE"
    GetFalse = False
End Function

VB.NET's Boolean logic works in the same way as it does in VB6, which means that the message boxes will be shown when migrating this code to VB.NET. However, C#'s boolean logic uses short-circuiting, which means that migrating this code will not work the same way as it does in VB6.

private void Form_Load()
{
	if (GetFalse() && GetTrue())
	{
		...
	}
	else
	{
		...
	}

	if (GetTrue() || GetFalse())
	{
		...
	}
	else
	{
		...
	}
}

private bool GetTrue()
{
	MessageBox.Show("TRUE", ...);
	return true;
}

private bool GetFalse()
{
	MessageBox.Show("FALSE", ...);
	return false;
}

Since C# short-circuits && statements, when the first method evaluates to false, it will not continue on evaluating the second metod, so only the first message box will be shown. The same will happen when the || statement is short-circuited by when the first method evaluates to true.

It is possible to remove the short-circuit in C# by using the bitwise & and | operators instead of the Boolean && and || operators.

private void Form_Load()
{
	if (GetFalse() & GetTrue())
	{
		...
	}
	else
	{
		...
	}

	if (GetTrue() | GetFalse())
	{
		...
	}
	else
	{
		...
	}
}

In this case, both message boxes will be shown because the bitwise operators will evaluate both sides of the expression regardless of their value.

The VBUC will use the standard && and || operators for Boolean logic, despite this known difference, because it is standard practice and using bitwise operators for Boolean logic may be confusing to others reviewing the code.

After migration, when encountering an instance where short-circuiting is affecting code flow, the way to fix it should be done on a case-by-case basis, using either the bitwise operators, or evaluating the conditions beforehand, as in the following example.

private void Form_Load()
{
	bool value1 = GetFalse();
	bool value2 = GetTrue();
	if (value1 && value2)
	{
		...
	}
	else
	{
		...
	}

	bool value3 = GetTrue();
	bool value4 = GetFalse();
	if (value3 || value4)
	{
		...
	}
	else
	{
		...
	}
}
PreviousHTTPS sites are not loaded in Windows XPNextAssessment Report Issue

Last updated 1 year ago

Was this helpful?