C# - Add data to object inside object

12,403

Solution 1

Initialize the list(better via constructor) and then add an object:

testObjet.Manoeuvres = new List<ManoeuvresId>();
ManoeuvresId id = new ManoeuvresId();
id.ManId = 1;
id.ManName = "man1";
testObjet.Manoeuvres.Add(id);

Here's a possible constructor implementation(also fixed the naming issues).

public class TestId
{
    public TestId() : this(-1, null) { }
    public TestId(int id) : this(id, null) { }
    public TestId(string name) : this(-1, name) { }
    public TestId(int id, string name)
    {
        this.ID = id;
        this.TestName = name;
        this.Manoeuvres = new List<ManoeuvresId>();
    }

    public string TestName { get; set; }
    public int ID { get; set; }
    public IList<ManoeuvresId> Manoeuvres { get; set; }
}

Then it's ensured that the list is always initialized and you can simplify it to:

TestId testObjet = new TestId(1, "test1");
testObjet.Manoeuvres.Add(new ManoeuvresId { ManId = 1, ManName = "man1" });

Solution 2

if you look at IList you will see is is a List, which has an add function

so you would do

testObjet.manoeuvres.Add(new manoeuvresID(){manName ="Something"});

note that you need to set manoeuvres to a new List (or other class that implements IList) first

Solution 3

Define a list

testObjet.manoeuvres = new list<HardCodedDatabase.manoeuvresID>();   

Create an object for manoeuvresId

    var anotherTestData = new HardCodedDatabase.manoeuvresID();
        anotherTestData .manName  = "something";

Add this object to testObj

testObjet.manoeuvres.Add(anotherTestData);

That's it.

So your code will look like this

 HardCodedDatabase.testID testObjet = new HardCodedDatabase.testID();
        testObjet.testName = "test1";
        testObjet.manoeuvres = "man1";
testObjet.manoeuvres = new list<HardCodedDatabase.manoeuvresID>(); 
testObjet.manoeuvres.Add(anotherTestData); 

var anotherTestData = new HardCodedDatabase.manoeuvresID();
            anotherTestData .manName  = "something"; 

        List<HardCodedDatabase.testID> listForTesting = new List<HardCodedDatabase.testID>();
        listForTesting.Add(testObjet);

Solution 4

Please try this. You need to add List<manoeuvresID> in testID object.

testID testObjet = new testID();
testObjet.testName = "test1";

manoeuvresID man = new manoeuvresID();
man.manID = 1;
man.manName = "Mairaj";

testObjet.manoeuvres = new List<manoeuvresID>() { man };

List<testID> listForTesting = new List<testID>();
listForTesting.Add(testObjet);           
Share:
12,403
peetman
Author by

peetman

Updated on June 26, 2022

Comments

  • peetman
    peetman almost 2 years

    I have the following classes

    public class manoeuvresID
    {
        public string manName { get; set; }
        public int manID { get; set; }
    }
    
    public class testID
    {
        public string testName { get; set; }
        public int ID { get; set; }
        public IList<manoeuvresID> manoeuvres { get; set; }
    }
    

    and want to add data into a new list of type testID

            HardCodedDatabase.testID testObjet = new HardCodedDatabase.testID();
            testObjet.testName = "test1";
            testObjet.manoeuvres = "man1";
    
            List<HardCodedDatabase.testID> listForTesting = new List<HardCodedDatabase.testID>();
            listForTesting.Add(testObjet);
    

    the problem is that I don't know how to add values to the testobjet.manoeuvres

  • peetman
    peetman almost 8 years
    Error 1 Cannot implicitly convert type 'ValAppWPF.HardCodedDatabase.ManoeuvresID' to 'System.Collections.Generic.IList<ValAppWPF.HardCodedDatabas‌​e.ManoeuvresID>'.
  • peetman
    peetman almost 8 years
    not yet. it does not recognise monoeuvresID(). I have to add HardCodedDatabase.manoeuvresID() and throws the error I wrote before
  • peetman
    peetman almost 8 years
    how can I add another TestObjet to the ListForTesting? TestObjet.testName = "test1"; ListForTesting.Add(TestObjet); TestObjet.testName = "test2"; ListForTesting.Add(TestObjet); combobox_testType.Items.Add(ListForTesting[0].testName); combobox_testType.Items.Add(ListForTesting[1].testName);
  • Tim Schmelter
    Tim Schmelter almost 8 years
    @peetman: you have to initialize a new TestId-instance: testObjet = new HardCodedDatabase.testID(); testObjet.testName = "test2"; ListForTesting.Add(testObjet);. `
  • Tim Schmelter
    Tim Schmelter almost 8 years
    @peetman: That's basic OOP. You need to understand the difference between a class, an instance of a class and a variable. If you have two test-objects you want two instances of this class.
  • MikeT
    MikeT almost 8 years
    @peetman, VBA is very lazy and lets you get away with doing some stupid things. because c# will not allow you to make those mistakes then it is much easier to write good programs. if you turn on option strict and explicit in VBA. then it will be much the same
  • Tim Schmelter
    Tim Schmelter almost 8 years
    @MikeT: i think you're confusing VB.NET with VBA
  • MikeT
    MikeT almost 8 years
    @TimSchmelter fairly sure i'm not, i've spent way too much time working in VB6 and VBA
  • Tim Schmelter
    Tim Schmelter almost 8 years
    @MikeT: but OP said that he is used to VBA and afaik there is no option strict in VBA, is it?
  • MikeT
    MikeT almost 8 years
    @TimSchmelter ah see where you are coming from, i always mentally pair the 2 together so that i always check them no matter what version of VB I'm being subjected to ;) better to check for one that's not there than forget to turn it on when it is