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

8

32

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 } ???

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1513: } expected

Source Error:

Line 151: }
Line 152: }
Line 153:}

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 prefer 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.

Wondering what I am missing in the above code?

Ringmaster answered 26/6, 2013 at 14:1 Comment(3)
I have no clue why this was downvoted?Ringmaster
Problem semi-solved by using a simple and more n-tier approach. Strange behaviour for Razor Engine, but I dont mind, now the code works as originally expected. That's what counts right now. I marked the answer + made a comment on how.Ringmaster
Please don't modify your question in a way such that existing answers no longer make sense. (this was years ago, but came back to it just now and had no clue why none of the answers made any sense)Unlock
W
46

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!")
        };
Watercolor answered 26/6, 2013 at 14:6 Comment(2)
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.Ringmaster
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.Ringmaster
A
9

Shorthand for the win:

var myClassList = new[]
{
    new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
    new MyClass(1002,2345,"Text xx", "bla bla", "dong")
};
Abingdon answered 13/5, 2015 at 15:44 Comment(0)
C
6

this will also work without a need to create a constructure

new MyClass [] { new MyClass { Field1 = "aa", Field2 = 1 } } 
Charterhouse answered 13/3, 2018 at 10:22 Comment(0)
U
3

You want:

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

You were missing the brackets toward the end.

Unlock answered 26/6, 2013 at 14:5 Comment(0)
G
1
MyClass[] testobjlist = 
    {
     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!")
    };
Gunyah answered 25/2, 2018 at 15:55 Comment(0)
S
1
MyClass[] testobjlist = new MyClass[noOfObjects];
for(int i = 0; i < testobjlist.Length; i++) { testobjlist[i] = new MyClass(); }
Sting answered 1/10, 2018 at 23:17 Comment(0)
N
0

You can use below code for the array:

additionalusers[] __adiitonaluser =
{
    new additionalusers()
};
__adiitonaluser[0].Email = Userpersonal.Email;
Nace answered 12/4, 2019 at 6:36 Comment(0)
L
-1
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!")
};

Or

MyClass[] testobjlist =
{
     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!")
};
Leukas answered 3/7, 2021 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.