Create List using a custom class

  • Thread starter Thread starter Rich P
  • Start date Start date
R

Rich P

public class xyz
{
...
}

List<xyz> mylist = new List<xyz>();

my question is if I want to add data to xyz thru the constructor or thru
properties?

xyz a = new xyz("aa","bb","cc");
mylist.add(a);

or

xyz a = new xyz();
a.fld1 = "aa";
a.fld2 = "bb";
a.fld3 = "cc";
mylist.add(a)

or how would I create my class so I could do this?

xyz a = new xyz(){"aa","bb","cc"};
mylist.add(a)

Thanks

Rich
 
public class xyz
{
..
}

List<xyz> mylist = new List<xyz>();

my question is if I want to add data to xyz thru the constructor or thru
properties?

Strictly speaking, that's not a question. It's the phrase "if I want to
add data to xyz thru [sic] the constructor or thru [sic] properties"
followed by a question mark.

And it's hard to infer a proper question from the phrase, since if we take
the phrase literally and assume you want to know how you can initialize an
instance of "xyz" using a constructor or properties, the answer is simply
"write the necessary constructor or properties in the class".

But surely that's not the answer you're looking for, is it?
xyz a = new xyz("aa","bb","cc");
mylist.add(a);

or

xyz a = new xyz();
a.fld1 = "aa";
a.fld2 = "bb";
a.fld3 = "cc";
mylist.add(a)

or how would I create my class so I could do this?

To do what? To what does "this" refer? The code above the question? Or
below?
xyz a = new xyz(){"aa","bb","cc"};
mylist.add(a)

This syntax isn't legal. You can provide property initializers for a
class, but in that case the properties must be public (just as they would
in your second code example), and you would need to provide the name for
each property in the initializer syntax. For example:

xyz a= new xyz() { fld1 = "aa", fld2 = "bb", fld3 = "cc" };

I'm trying my best to comprehend an answerable question from your post,
and hopefully the above is helpful. But if your question hasn't been
answered, you need to take a little more time and construct a post that
contains a clear, specific, answerable question.

Pete
 
Back
Top