Click here to Skip to main content
15,885,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone,

Can any one explain why the following code doesn't compile?
C#
int a = 7;
int b = 5;

dynamic oa = a;
dynamic ob = b;

int c = oa - ob;

Thanks in advance.
Posted
Updated 26-Feb-11 2:31am
v3

Are you using VS2010? If not, then dynamic is not available (it was added at V4.0)

Otherwise, what error do you get - it compiles clean and works for me...
"am using VS2010.
Getting same compilation Errors: Unexpected character'-'
Invalid expression term "
; expected
; expected
"

Have you included your code inside a method?
C#
using System;

namespace ConsoleTester
    {
    class Program
        {
        static void Main(string[] args)
            {
            int a = 7;
            int b = 5;

            dynamic oa = a;
            dynamic ob = b;

            int c = oa - ob;
            Console.WriteLine(c);
            }
        }
    }
 
Share this answer
 
v2
Comments
satyagrahi_2010 26-Feb-11 5:49am    
am using VS2010.
Getting same compilation Errors: Unexpected character'-'
Invalid expression term "
; expected
; expected
OriginalGriff 26-Feb-11 5:54am    
Answer updated
Espen Harlinn 26-Feb-11 5:58am    
Right :)
satyagrahi_2010 26-Feb-11 6:04am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyPractice
{
class Program
{
static void Main(string[] args)
{

int a = 7;
int b = 5;
dynamic oa = (dynamic)a;
dynamic ob = (dynamic)b;
int c = oa – ob;

Console.WriteLine(" C Value is : "+c);

Console.ReadKey();
}
}

}
The above code is in my class file. i saw that project properties Target Framework is .NET Framework 4
satyagrahi_2010 26-Feb-11 6:18am    
It's working fine in another system. thanks for helping me.
OriginalGriff is right, this compiles and runs nicely:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleTestCS
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 7; 
            int b = 5; 
            dynamic oa = a; 
            dynamic ob = b; 
            int c = oa - ob;
            Console.WriteLine(c);
        }
    }
}


So open your project properties and check that you are building for the .Net 4 runtime

Regards
Espen Harlinn
 
Share this answer
 
Comments
satyagrahi_2010 26-Feb-11 6:19am    
Its working fine thanks for your help.
You need explicit type conversion here.

Try:
C#
int a = 7;    
int b = 5;        
dynamic oa = (dynamic)a;    
dynamic ob = (dynamic)b;       
int c = oa - ob;


If needed, look here[^] for more clarity.

UPDATE:
1. As already said by OriginalGriff, your code would work fine if you have .NET framework 4.0
2. Based on the error, it's clear dynamic is not being considered as a keyword and hence an error
3. Look for the missing reference. Probably you are not using .NET framework 4.0, which would be needed here for dynamic keyword.
 
Share this answer
 
v2
Comments
OriginalGriff 26-Feb-11 5:30am    
Actually, you don't: a default console project with his original lines of code compiles cleanly and runs ok in VS2010, I checked. Nasty thing to do, though - reminds me too much of VB var...
Sandeep Mewara 26-Feb-11 5:32am    
Oh Ok. Thanks for telling me that. Good to know. :thumbs up:

Surely it is a bad idea to use something which will pass compile time and be checked only at runtime. Dangerous to use dynamic until unless really needed.
OriginalGriff 26-Feb-11 5:33am    
Totally agree! It's a good way to hide errors: I can understand why MS included it, but I do wish they hadn't...
Sandeep Mewara 26-Feb-11 5:37am    
It just gives few notorious coders to play around with if they like to. :P :)
satyagrahi_2010 26-Feb-11 5:48am    
Sorry to say, explicit type conversion is not working.

Getting same compilation Errors: Unexpected character'-'
Invalid expression term "
; expected
; expected

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900