C# syntax to initialize custom class/objects through constructor params in array?

86,298

Solution 1

Try adding square brackets after new MyClass and a semi-colon at the end

    MyClass[] testobjlist = new MyClass[] 
        {
         new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
         new MyClass(1002,2345,"Text xx", "bla bla", "dong"),
         new MyClass(1003,8653,"Text yy", "blah blah even more", "bamm!")
        };

Solution 2

Shorthand for the win:

var myClassList = new[]
{
    new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
    new MyClass(1002,2345,"Text xx", "bla bla", "dong")
};

Solution 3

this will also work without a need to create a constructure

new MyClass [] { new MyClass { Field1 = "aa", Field2 = 1 } } 

Solution 4

You want:

MyClass[] testobjlist = new MyClass[] { ... }

You were missing the brackets toward the end.

Solution 5

MyClass[] testobjlist = new MyClass[noOfObjects];
for(int i = 0; i < testobjlist.Length; i++) { testobjlist[i] = new MyClass(); }
Share:
86,298
BerggreenDK
Author by

BerggreenDK

Started programming as very young on a Commodore VIC-20 and 64. First in BASIC, then moved to 8-bit assembler/machinecode to make things run more smooth. Ended up building a game for it with two friends which we sold and earned Amiga 500 computers. This lead me to the assembler of Motorola 680x0 series CPU. Great CPU btw. Took an IT-education, learned C/C++ and used some Pascal. Build my first networking application for the school I was on, as we needed a cross location chat-tool in DOS. Made it upon Netware/IPX protocol. Then I went and became in love with C++ and its possiblities. Got brainwashed at school, came out saying: Windows, Windows, Windows... so I started to look at DirectX for direct computer power, usable for further multimedia/game development. I've composed a some computer-music too, back in the old C64 days, but thats another story. Later on, I got employed in different companies, including IT-consulting and support. Then worked with advertising/commercial companies as "Multimedia architect/systemdeveloper". I've been all the way through the different browserplatforms and "wars", been building cross platform websites with dynamic HTML from the very early days with Netscape LAYER tags and MSIE DIV, makeing sure that things ALSO worked on Macintosh platoform, so cross platform development has been my goal since 1997. I design and implement SQL-server structures too, so the backend is ready for the frontend. Today I run my own small company, working mostly with .NET and C#, but XHTML/CSS/XML/JQUERY + some Flash Actionscript 3.0 is also part of my work from time to time. Overall - the company is focusing on making the internet easier to learn and use, therefore somethings might be hard to develop, but as long as it will help the user to get respons/results faster and easier, we still go for the "hard way" of developing stuff. Better think before errors happends. I dont have a "language" religion, I dont care what platform we use, as long as its the best for the purpose and we dont have to pay gazillions for licenses. I dont mind paying for quality, but dang - there is really a lot of wanna-be crap out there, so I do check upon things before investing time and money. And no, I dont go for the easy way. I go for the most valueable way, for the customer.

Updated on July 04, 2021

Comments

  • BerggreenDK
    BerggreenDK almost 3 years

    I have a class with minimum 4 variables and I have made a constructor for the class so that I can initialize it with

    MyClass testobj = new MyClass(1234,56789,"test text", "something else", "foo");
    

    Works fine.

    Then I have an array of these, that I need to parse in a loop, so I would like to get some static data into this array.

    My approach was:

    MyClass[] testobjlist = new MyClass 
    {
         new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
         new MyClass(1002,2345,"Text xx", "bla bla", "dong"),
         new MyClass(1003,8653,"Text yy", "blah blah even more", "bamm!")
    }
    

    but somehow this gives me a weird error about me needing an extra } ???

    I don't know if I should mention this, but I use it for webpages using Razor-engine 2. But I think this is an ordinary C# question?

    My workaround is currently to initialize the array with a size, then adding the elements one by one through index, but I would rather prefere the above solution as I might have to move the items up and down in order when testing and I have a lot more than 3 in the real data.

    What I am missing in the above code?

  • BerggreenDK
    BerggreenDK almost 11 years
    aaah, well I only forgot that in my example here. They are in the real version and it still gives me an error. So strange.
  • BerggreenDK
    BerggreenDK almost 11 years
    Strange, I tried to place the exact same code into a separate BAL class, and now everything works as expected. But if I place the code inside the @{} code-strip in the .cshtml file I get the error about missing }. Probarbly a Razor engine bug. Thanks for your help anyways.