Is his project targetting .NET 3.5? If not, that error would be thrown on the x.Add(new MyClass line because the new class doesn't have a constructor or indexer specified.
Your code works flawlessly for me. Are you sure the project is being compiled to the 3.5 framework? Select the project properties from the Project menu, go to the Application tab, and look at the Target Framework dropdown.
It gives me bad compile message in my PC
Try this
x.Add(new MyClass()
{
Property1 = "MyValue",
Property2 = "Another Value"
});
Notice, there is another bracket after MyClass class creation.
Are you sure you're using the exact same code?
The code you supplied doesn't compile, because it lacks a declaration of MyClass
. Please supply us with a full compiland, not just a code snippet. Then send that file to your friend to make sure you still see the different behavior on your machine.
The error message points to a line & column, right? Tell us where.
Are you both at the same .NET service pack level? I got bitten today because one machine was .NET 3.5 RTM, the other was .NET 3.5 SP1. In .NET 3.5 SP1 (which also installs .NET 2.0 SP2), they introduced a new overload to the System.Web.Caching.Cache.Insert method which I used on my dev box and thenfailed on an intermediate pre-build environment:
public void Insert(
string key,
Object value,
CacheDependency dependencies,
DateTime absoluteExpiration,
TimeSpan slidingExpiration,
CacheItemUpdateCallback onUpdateCallback
)
Took me a few mins to workout why this was broken on one machine but not the other...
Update: the error message 'A new expression requires () or [] after type' often means you've missed a constructor parens. Are you sure you haven't missed a () off of the line:
List<MyClass> x = new List<MyClass>();
Or somewhere near by?
Update Again: I have built the following:
using System;
using System.Collections.Generic;
namespace Test
{
public class MyClass
{
public MyClass() { }
public string Property1 { get; set; }
public string Property2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<MyClass> x = new List<MyClass>();
x.Add(new MyClass
{
Property1 = "Kev",
Property2 = "Kev 2"
});
}
}
}
VS2008 SP1 Targetting 3.5 - Compiles ok
VS2008 SP1 Targetting 3.0 - Compiles ok
VS2008 SP1 Targetting 2.0 - Compiles ok
VS2008 RTM - Targetting 3.5 - Compiles ok
VS2008 RTM - Targetting 3.0 - Compiles ok
VS2008 RTM - Targetting 2.0 - Compiles ok
VS2005 - 8.0.50727.867 (on machine with VS2008/3.5 SP1) - Fails with:
A new expression requires () or [] after type
'Test.MyClass.Property1.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property1.set' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.set' must declare a body because it is not marked abstract or extern
VS2005 - 8.0.50727.762 (on machine with VS2008/3.5 RTM) - Fails with:
A new expression requires () or [] after type
'Test.MyClass.Property1.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property1.set' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.set' must declare a body because it is not marked abstract or extern
I am inclined to think the problem is not with the code you are presenting unless a screen shot is supplied with an example of the compile error using the above code or similar. Any chance of boiling things down to something simple to eliminate red herrings?
Cheers
Kev
Here's what seems to be the only similar, but not exactly the same, error available in VS.2008:
Compiler Error CS1526 : A new expression requires (), [], or {} after type
Note those {}
in error message, which are part of c# 3.0 syntax. This is not related to framework version, but to the version of the language.
My bet is that somehow a different version of compiler was used.
Added: this looks like a likely issue with ASP.Net. Place to check is in .config file(s), element
configuration\system.codedom\compilers\compiler @language="c#..."
there should be
<providerOption name="CompilerVersion" value="v3.5"/>
Add "()" after new MyClass in the code example, or remove the empty constructor from MyClass.
Guys I am sorry to but in, but just to deviate a little from the above example. I am really battling with some coding that's giving me the same error message. I'm basically trying to create a new instance of excel with C# code. The error message occurs in the line that I have commented. Can anyone perhaps help with a possible solution?
ApplicationClass excelApplication = null;
Workbook newWorkbook = null;
Worksheet targetSheet = null;
Range dataRange = null;
string paramWorkbookPath = @"<Path>\Calculated Columns.xlsx";
object paramMissing = Type.Missing;
//excelApplication = new ApplicationClass
newWorkbook = excelApplication.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
targetSheet = (Worksheet)(newWorkbook.Worksheets[1]);
targetSheet.Name = "Calculated Columns";