Object Initialization Expressions in C# problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon

    Object Initialization Expressions in C# problem

    I have problems with object initialization. I have tried the procedure
    described on:


    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<Testtest =
    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


  • Duggi

    #2
    Re: Object Initialization Expressions in C# problem

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

    Thanks
    -Srinivas.


    Simon wrote:
    I have problems with object initialization. I have tried the procedure
    described on:

    >
    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<Testtest =
    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

    Comment

    • Simon

      #3
      Re: Object Initialization Expressions in C# problem

      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" <DuggiSrinivasa Rao@gmail.comwr ote in message
      news:1166957520 .625438.327130@ h40g2000cwb.goo glegroups.com.. .
      are you sure VS 2005 is compiling the code with C#3.0 compiler???
      >
      Thanks
      -Srinivas.
      >
      >
      Simon wrote:
      >I have problems with object initialization. I have tried the procedure
      >described on:
      >http://davidhayden.com/blog/dave/arc...pressions.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<Testtest =
      > 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
      >

      Comment

      • Bruce Wood

        #4
        Re: Object Initialization Expressions in C# problem

        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.

        Simon wrote:
        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" <DuggiSrinivasa Rao@gmail.comwr ote in message
        news:1166957520 .625438.327130@ h40g2000cwb.goo glegroups.com.. .
        are you sure VS 2005 is compiling the code with C#3.0 compiler???

        Thanks
        -Srinivas.


        Simon wrote:
        I have problems with object initialization. I have tried the procedure
        described on:

        >
        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<Testtest =
        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

        Comment

        • Peter Huang [MSFT]

          #5
          Re: Object Initialization Expressions in C# problem

          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

          9f04-70eb5e3dceea&di splaylang=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.

          Comment

          Working...