3

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
  • 669
  • 2
  • 15
  • 30
  • 1
    [Naming conventions](https://msdn.microsoft.com/en-us/library/ms229043(v=vs.100).aspx) are really helpful and important ;-) – Tim Schmelter May 24 '16 at 09:31

5 Answers5

4

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" });
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 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);` – peetman May 24 '16 at 10:10
  • @peetman: you have to initialize a new `TestId`-instance: `testObjet = new HardCodedDatabase.testID(); testObjet.testName = "test2"; ListForTesting.Add(testObjet);`. ` – Tim Schmelter May 24 '16 at 10:13
  • @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. – Tim Schmelter May 24 '16 at 10:18
  • @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 – MikeT May 24 '16 at 12:57
  • @MikeT: i think you're confusing VB.NET with VBA – Tim Schmelter May 24 '16 at 13:05
  • @TimSchmelter fairly sure i'm not, i've spent way too much time working in VB6 and VBA – MikeT May 24 '16 at 13:18
  • @MikeT: but OP said that he is used to VBA and afaik there is no `option strict` in VBA, is it? – Tim Schmelter May 24 '16 at 13:25
  • @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 – MikeT May 24 '16 at 13:29
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

MikeT
  • 5,398
  • 3
  • 27
  • 43
1

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);
Ashish Rajput
  • 1,489
  • 1
  • 11
  • 20
1

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);           
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
0

Also :

   HardCodedDatabase.testID testObjet = new HardCodedDatabase.testID();
    testObjet.testName = "test1";
   testObjet.manoeuvres.Add(new manoeuvresID(){manName ="man1"});
    List<HardCodedDatabase.testID> listForTesting = new List<HardCodedDatabase.testID>();
    listForTesting.Add(testObjet);
Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17
  • Error 1 Cannot implicitly convert type 'ValAppWPF.HardCodedDatabase.ManoeuvresID' to 'System.Collections.Generic.IList'. – peetman May 24 '16 at 09:43
  • not yet. it does not recognise monoeuvresID(). I have to add HardCodedDatabase.manoeuvresID() and throws the error I wrote before – peetman May 24 '16 at 09:47