Object Initialization Expressions in C# problem

S

Simon

I have problems with object initialization. I have tried the procedure
described on:
http://davidhayden.com/blog/dave/archive/2006/12/04/ObjectInitializationExpressions.aspx

class Test
{
public short testId;
public string testStr;

public short Id
{
get { return testId; }
set { testId = value; }
}
public string Name
{
get { return testStr; }
set { testStr = value; }
}
}

But when I try this:

Test test = new Test{Id = 1, Name = "Name"};

I get error "A new expression requires () or [] after type".

I would also like to do this:

List<Test> test =
new List<Test> {
{ Id = 1, Name="Name" },
};

But I get the same error - "A new expression requires () or [] after type".

This new feature should work in C# 3.0 and I am using VS 2005. What am I
missing?

Thank you

Simon
 
S

Simon

I do not know?

What I would like to do is something like this in C:

struct {
int id;
char name[50];
} table[] = {{5,"AA"}, {6,"BB"}};

And this was the closest thing. If this will not work I would like to know
how can I do that the shortest way?

Simon

Duggi said:
are you sure VS 2005 is compiling the code with C#3.0 compiler???

Thanks
-Srinivas.

I have problems with object initialization. I have tried the procedure
described on:
http://davidhayden.com/blog/dave/archive/2006/12/04/ObjectInitializationExpressions.aspx

class Test
{
public short testId;
public string testStr;

public short Id
{
get { return testId; }
set { testId = value; }
}
public string Name
{
get { return testStr; }
set { testStr = value; }
}
}

But when I try this:

Test test = new Test{Id = 1, Name = "Name"};

I get error "A new expression requires () or [] after type".

I would also like to do this:

List<Test> test =
new List<Test> {
{ Id = 1, Name="Name" },
};

But I get the same error - "A new expression requires () or [] after
type".

This new feature should work in C# 3.0 and I am using VS 2005. What am I
missing?

Thank you

Simon
 
B

Bruce Wood

VS 2005 comes out-of-the-box with C# 2.0.

C# 3.0 is an add-on on top of C# 2.0. If you didn't download and
install C# 3.0 then you don't have it.
I do not know?

What I would like to do is something like this in C:

struct {
int id;
char name[50];
} table[] = {{5,"AA"}, {6,"BB"}};

And this was the closest thing. If this will not work I would like to know
how can I do that the shortest way?

Simon

Duggi said:
are you sure VS 2005 is compiling the code with C#3.0 compiler???

Thanks
-Srinivas.

I have problems with object initialization. I have tried the procedure
described on:
http://davidhayden.com/blog/dave/archive/2006/12/04/ObjectInitializationExpressions.aspx

class Test
{
public short testId;
public string testStr;

public short Id
{
get { return testId; }
set { testId = value; }
}
public string Name
{
get { return testStr; }
set { testStr = value; }
}
}

But when I try this:

Test test = new Test{Id = 1, Name = "Name"};

I get error "A new expression requires () or [] after type".

I would also like to do this:

List<Test> test =
new List<Test> {
{ Id = 1, Name="Name" },
};

But I get the same error - "A new expression requires () or [] after
type".

This new feature should work in C# 3.0 and I am using VS 2005. What am I
missing?

Thank you

Simon
 
P

Peter Huang [MSFT]

Hi Simon,

Based on my test, after in install the extension below, the code in your
referred page should work.
You may have a try and let me know the result.(BTW: I have install .NET FW
3.0)

Microsoft Visual Studio Code Name ¡°Orcas¡± Language-Integrated Query, May
2006 Community Technology Preview
http://www.microsoft.com/downloads/details.aspx?familyid=1e902c21-340c-4d13-
9f04-70eb5e3dceea&displaylang=en


Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top