Resolved Runtime Message Issued Error : Found;

DarkSystemCD

New member
Joined
May 1, 2022
Messages
3
Programming Experience
10+
Hello CSharp | C# Developer Community

[^] found a runtime message issue
Operator '<' cannot be applied to operands of type 'int' and 'object[]'

[X] fixed with the following specification;:
if(MyVariable.GetType()==typeof(System.Boolean)&&
   MyVariable==false) return;

line 1 had to be added and there where no where to be found the runtime error specification
Operator '<' cannot be applied to operands of type 'int' and 'object[]'

which was written like this; there was no such '<' operator:
if(MyVariable==false) return;

the rest of the code is irrelevant which everything works;
that where integers and object arrays but,--> only this line issued the mispelled information for you to report to the community;
which MyVariable could be int or boolean from checking values from object array;
 
Are you sure that is a runtime error? Sounds like a compile time error when you were trying to use:
C#:
if (MyVariable == false)
    return;
and MyVariale is not a bool.

Can you show us how MyVariable was declared?

Also, it's unclear how you are comparing types int and object[] with a < operator when the sample code you are presenting is trying to use the == to compare some variable of some type with a constant bool.
 
Variable Declaration:
dynamic MyVariable=null;

/* ...code checking types; by indexed int and doubles... */
/* which MyVariable could be updated with int or bool */
/* at code checking MyVariable was set to a default value false; */

if(MyVariable.GetType()==typeof(System.Boolean)&& //[?] added specification
   MyVariable==false) return; //[?] issued error message happened here;

there was no comparison with '<' the compiler is what displayed the issued message;
thus resulting in attention overflows by adding the specification to MyVariable;
-> if you enconter such message the error could be something else like displayed on the forum post;
 
I just tested your code with setting MyVariable to an int, bool and string[] and it worked in all three cases. I tried it against .NET Framework 4.8 and .NET 6. I suggest that you try a new Console app with just that code and see what happens. What version of VS are you using? It's worth noting that, in newer versions of C#, this:
C#:
if (MyVariable.GetType() == typeof(System.Boolean) && MyVariable == false) return;
could be written like this:
C#:
if (MyVariable is false) return;
 
Visual Studio Code 1.66.2
.NET 6.0.4
SDK 6.0.202
CSharp 9.0 | C# (Omnisharp) v1.24.4
Windows Presentation Foundation
Microsoft Windows 10 | 21H2

dynamic variable updated single from case results <-
local lambda expressions from swith case's with type results from int\double indexed <-
dynamic object array int and double values from switch <-

happened at the end of the process
if(MyVariable.GetType()==typeof(System.Boolean)&&MyVariable==false) return;
from
if(MyVariable==false) return;

mispelled runtime error message
Operator '<' cannot be applied to operands of type 'int' and 'object[]'

the program was compiled and run; until encontered the issue at runtime process MyVariable;

thanks;
C# .NET Developer Community Notification; "" resulting in attention overflows; ""
 
dynamic variable updated single from case results <-
local lambda expressions from swith case's with type results from int\double indexed <-
dynamic object array
Please show us this code instead of just describing it. I suspect that you are using a term one way, and we're are understanding that term another way. By showing us your code, or a minimal repro case, we will be looking at the same thing even though you call it one thing, and we call it something else.
 
I can't reproduce your problem. I'm using the following code:
C#:
using System;

namespace TestDynamic
{
    class Program
    {
        static void Main(string[] args)
        {
            var myObjs = new object [] { false, 1.0, "2.0", new Uri("https://google.com"), new object[] { } };

            foreach(var myObj in myObjs)
            {
                try
                {
                    dynamic myVariable = myObj;
                    if (myVariable == false)
                    {
                        Console.WriteLine("The expression (myVariable == false) is true");
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine($"({myObj} == false) threw an exception: {ex}");
                }
            }
        }
    }
}

But all the exceptions I'm getting are with regards to the == operator when trying to compare against bool:
C#:
The expression (myVariable == false) is true
(1 == false) threw an exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Operator '==' cannot be applied to operands of type 'double' and 'bool'
   at CallSite.Target(Closure , CallSite , Object , Boolean )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at TestDynamic.Program.Main(String[] args) in d:\z\Test\SimpleCS\SimpleCSCore\Program.cs:line 16
(2.0 == false) threw an exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Operator '==' cannot be applied to operands of type 'string' and 'bool'
   at CallSite.Target(Closure , CallSite , Object , Boolean )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at TestDynamic.Program.Main(String[] args) in d:\z\Test\SimpleCS\SimpleCSCore\Program.cs:line 16
(https://google.com/ == false) threw an exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Operator '==' cannot be applied to operands of type 'System.Uri' and 'bool'
   at CallSite.Target(Closure , CallSite , Object , Boolean )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at TestDynamic.Program.Main(String[] args) in d:\z\Test\SimpleCS\SimpleCSCore\Program.cs:line 16
(System.Object[] == false) threw an exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Operator '==' cannot be applied to operands of type 'object[]' and 'bool'
   at CallSite.Target(Closure , CallSite , Object , Boolean )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at TestDynamic.Program.Main(String[] args) in d:\z\Test\SimpleCS\SimpleCSCore\Program.cs:line 16
 
Back
Top Bottom