probleem met overloading methods in C#

Status
Niet open voor verdere reacties.

AMBERTJE

Gebruiker
Lid geworden
27 aug 2009
Berichten
121
Goedemiddag,

Ik heb een probleempje met het overloaden van een method, in de classe Location wil ik een container kunnen toevoegen.
De originele method noemt public void ContAdd(Container container)
en in de Main roep ik deze zo op en dit werkt perfect:

l1.ContAdd(c1);

Nu heb ik een classe Lane en daar worden Locations aan toegevoegd, ik heb nu de functie ContAdd(Container container, Location location) als overload

Hoe voeg ik nu een Container toe aan een Location aan een Lane door de overload method op te roepen in Main?

Groetjes,
Ambertje
 
Ik weet niet of ik al te laat ben:

Maar zou je een snippet van je code kunnen laten zien?
 
Ik weet niet of ik al te laat ben:

Maar zou je een snippet van je code kunnen laten zien?

Tuurlijk, dit zijn de methodes
/// <summary>
/// Deze functie berekent of een locatie al dan niet vol is
/// Als het aantal opgetelde containers gelijk is aantalloc(20)
/// dan is de Lane vol.
/// </summary>
/// <returns>true or false</returns>
public bool IsFull()
{
return _myLocations.Count == AantalLoc;
}

public void LocAdd(Location l)
{
if (IsFull())
{
throw new TooManyContainersInLocationException();
}
//een cont kan pas toegevoegd worden aan een locatie in een lane als het gewicht niet hoger is dan toegelaten in de loc.
if (CurrentWeightInLaneKg + l.CurrentWeightInLocationKg > _maxWeightLane)
{
throw new WeightLimitExceededInLocationException();
}
this.MyLocations.Add(l);
}

/// <summary>
/// Deze functie voegt een container toe als aan de volgende voorwaarden voldaan wordt:
/// 1)het maximum gewicht in de locatie niet bereikt is
/// 2)of de locatie niet vol is adhv de functie IsFull die hier wordt opgeroepen
/// Als aan deze voorwaarden niet voldaan wordt dan krijgt men een boodschap te zien
/// </summary>
/// <param name="location">het toevoegen van een container in een locatie op een Lane</param>
public void ContAdd(Container c)
{
if (IsFull())
{
throw new TooManyContainersInLocationException();
}
if (CurrentWeightInLaneKg + c.ContWeightKg >
_maxWeightLane)
{
throw new WeightLimitExceededInLocationException();
}
this._myLocations.Add(c);
}

/// <summary>
/// Functie om containers toe te voegen met 2 parameters (locatie en container)
/// Deze functie voegt een container toe als aan de volgende voorwaarden voldaan wordt:
/// 1)het maximum gewicht in de locatie niet bereikt is
/// 2)of de locatie niet vol is adhv de functie IsFull die hier wordt opgeroepen
/// Als aan deze voorwaarden niet voldaan wordt dan krijgt men een boodschap te zien.
/// </summary>
/// <param name="l">locatie van het type Location</param>
/// <param name="c">container van het type Container</param>
public void ContAdd(Location l,Container c)
{
if (IsFull())
{
throw new TooManyContainersInLocationException();
}
if (CurrentWeightInLaneKg + (l.LocMaxWeightKg + c.ContWeightKg) >
_maxWeightLane)
{
throw new WeightLimitExceededInLocationException();
}
//_myLocations.Add(l.MijnContainers.Add(c));
l.MijnContainers.Add(c);
}


Dit is de Main method waar ik de objecten aanmaak en toon in de console applicatie:
public static void Main()
{
Container c1 = new Container(0, "Rommel", DateTime.Now, 1000.0, "Vuilbak", "CVO");
Container c2 = new Container(0, "Electronische apparaten", DateTime.Now, 1000, "Hong Kong", "Chung");
Container c3 = new Container(0, "Meubelen", DateTime.Now, 1000, "Zweden", "IKEA");
Container c4 = new Container(0, "Toys", DateTime.Now, 4000, "Nederland", "Bart Smit");
Container c5 = new Container(0, "Books", DateTime.Now, 1000, "England", "De Standaard Uitgeverij");
Container c6 = new Container(0, "Fietsen", DateTime.Now, 2000, "Duitsland", "Gazelle");

Location l1 = new Location(3, 1, 5000);
Location l2 = new Location(3, 2, 5000);
Location l3 = new Location(3, 3, 5000);

Lane lane1 = new Lane(100000, 1, 20);

Terminals t = new Terminals(500000);

l1.ContAdd(c1);
l1.ContAdd(c2);
l1.ContAdd(c3);
l2.ContAdd(c4);
l2.ContAdd(c5);
l1.ContRemove(1);
l3.ContAdd(c6);

//lane1.ContAdd(c1);
//lane1.LocAdd(l1);
//lane1.ContAdd(l1, c2);
//lane1.ContAdd(l1, c1);


//Hier vraag ik aan Lane om in elke locatie te kijken welke containers er staan en print die op het scherm
foreach (Location l in lane1.MyLocations)
{
foreach (Container c in l.MijnContainers)
{
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine(c);
}
}
Console.WriteLine(lane1);
Console.ReadLine();

//Hier vraag ik om het aantal containers dat in een lane zit op het scherm te tonen.
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine("The nbr of containers in lane is: " + lane1.GetNrOfContainers());
Console.ReadLine();
}

Bedankt alvast om hier effe naar te kijken :D
 
Even code tags eromheen maakt het wat duidelijker

Code:
/// <summary>
/// Deze functie berekent of een locatie al dan niet vol is
/// Als het aantal opgetelde containers gelijk is aantalloc(20)
/// dan is de Lane vol.
/// </summary>
/// <returns>true or false</returns>
public bool IsFull()
{
return _myLocations.Count == AantalLoc;
}

public void LocAdd(Location l)
{
if (IsFull())
{
throw new TooManyContainersInLocationException();
}
//een cont kan pas toegevoegd worden aan een locatie in een lane als het gewicht niet hoger is dan toegelaten in de loc.
if (CurrentWeightInLaneKg + l.CurrentWeightInLocationKg > _maxWeightLane)
{
throw new WeightLimitExceededInLocationException();
}
this.MyLocations.Add(l);
}

/// <summary>
/// Deze functie voegt een container toe als aan de volgende voorwaarden voldaan wordt:
/// 1)het maximum gewicht in de locatie niet bereikt is
/// 2)of de locatie niet vol is adhv de functie IsFull die hier wordt opgeroepen
/// Als aan deze voorwaarden niet voldaan wordt dan krijgt men een boodschap te zien
/// </summary>
/// <param name="location">het toevoegen van een container in een locatie op een Lane</param>
public void ContAdd(Container c)
{
if (IsFull())
{
throw new TooManyContainersInLocationException();
}
if (CurrentWeightInLaneKg + c.ContWeightKg >
_maxWeightLane)
{
throw new WeightLimitExceededInLocationException();
}
this._myLocations.Add(c);
}

/// <summary>
/// Functie om containers toe te voegen met 2 parameters (locatie en container)
/// Deze functie voegt een container toe als aan de volgende voorwaarden voldaan wordt:
/// 1)het maximum gewicht in de locatie niet bereikt is
/// 2)of de locatie niet vol is adhv de functie IsFull die hier wordt opgeroepen
/// Als aan deze voorwaarden niet voldaan wordt dan krijgt men een boodschap te zien.
/// </summary>
/// <param name="l">locatie van het type Location</param>
/// <param name="c">container van het type Container</param>
public void ContAdd(Location l,Container c)
{
if (IsFull())
{
throw new TooManyContainersInLocationException();
}
if (CurrentWeightInLaneKg + (l.LocMaxWeightKg + c.ContWeightKg) >
_maxWeightLane)
{
throw new WeightLimitExceededInLocationException();
}
//_myLocations.Add(l.MijnContainers.Add(c));
l.MijnContainers.Add(c);
}


Dit is de Main method waar ik de objecten aanmaak en toon in de console applicatie:
public static void Main()
{
Container c1 = new Container(0, "Rommel", DateTime.Now, 1000.0, "Vuilbak", "CVO");
Container c2 = new Container(0, "Electronische apparaten", DateTime.Now, 1000, "Hong Kong", "Chung");
Container c3 = new Container(0, "Meubelen", DateTime.Now, 1000, "Zweden", "IKEA");
Container c4 = new Container(0, "Toys", DateTime.Now, 4000, "Nederland", "Bart Smit");
Container c5 = new Container(0, "Books", DateTime.Now, 1000, "England", "De Standaard Uitgeverij");
Container c6 = new Container(0, "Fietsen", DateTime.Now, 2000, "Duitsland", "Gazelle");

Location l1 = new Location(3, 1, 5000);
Location l2 = new Location(3, 2, 5000);
Location l3 = new Location(3, 3, 5000);

Lane lane1 = new Lane(100000, 1, 20);

Terminals t = new Terminals(500000);

l1.ContAdd(c1);
l1.ContAdd(c2);
l1.ContAdd(c3);
l2.ContAdd(c4);
l2.ContAdd(c5);
l1.ContRemove(1);
l3.ContAdd(c6);

//lane1.ContAdd(c1);
//lane1.LocAdd(l1);
//lane1.ContAdd(l1, c2);
//lane1.ContAdd(l1, c1);


//Hier vraag ik aan Lane om in elke locatie te kijken welke containers er staan en print die op het scherm
foreach (Location l in lane1.MyLocations)
{
foreach (Container c in l.MijnContainers)
{
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine(c);
}
}
Console.WriteLine(lane1);
Console.ReadLine();

//Hier vraag ik om het aantal containers dat in een lane zit op het scherm te tonen.
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine("The nbr of containers in lane is: " + lane1.GetNrOfContainers());
Console.ReadLine();
}

En wat werkt er volgens jou nog niet naar behoren?
 
Even code tags eromheen maakt het wat duidelijker

Code:
/// <summary>
/// Deze functie berekent of een locatie al dan niet vol is
/// Als het aantal opgetelde containers gelijk is aantalloc(20)
/// dan is de Lane vol.
/// </summary>
/// <returns>true or false</returns>
public bool IsFull()
{
return _myLocations.Count == AantalLoc;
}

public void LocAdd(Location l)
{
if (IsFull())
{
throw new TooManyContainersInLocationException();
}
//een cont kan pas toegevoegd worden aan een locatie in een lane als het gewicht niet hoger is dan toegelaten in de loc.
if (CurrentWeightInLaneKg + l.CurrentWeightInLocationKg > _maxWeightLane)
{
throw new WeightLimitExceededInLocationException();
}
this.MyLocations.Add(l);
}

/// <summary>
/// Deze functie voegt een container toe als aan de volgende voorwaarden voldaan wordt:
/// 1)het maximum gewicht in de locatie niet bereikt is
/// 2)of de locatie niet vol is adhv de functie IsFull die hier wordt opgeroepen
/// Als aan deze voorwaarden niet voldaan wordt dan krijgt men een boodschap te zien
/// </summary>
/// <param name="location">het toevoegen van een container in een locatie op een Lane</param>
public void ContAdd(Container c)
{
if (IsFull())
{
throw new TooManyContainersInLocationException();
}
if (CurrentWeightInLaneKg + c.ContWeightKg >
_maxWeightLane)
{
throw new WeightLimitExceededInLocationException();
}
this._myLocations.Add(c);
}

/// <summary>
/// Functie om containers toe te voegen met 2 parameters (locatie en container)
/// Deze functie voegt een container toe als aan de volgende voorwaarden voldaan wordt:
/// 1)het maximum gewicht in de locatie niet bereikt is
/// 2)of de locatie niet vol is adhv de functie IsFull die hier wordt opgeroepen
/// Als aan deze voorwaarden niet voldaan wordt dan krijgt men een boodschap te zien.
/// </summary>
/// <param name="l">locatie van het type Location</param>
/// <param name="c">container van het type Container</param>
public void ContAdd(Location l,Container c)
{
if (IsFull())
{
throw new TooManyContainersInLocationException();
}
if (CurrentWeightInLaneKg + (l.LocMaxWeightKg + c.ContWeightKg) >
_maxWeightLane)
{
throw new WeightLimitExceededInLocationException();
}
//_myLocations.Add(l.MijnContainers.Add(c));
l.MijnContainers.Add(c);
}


Dit is de Main method waar ik de objecten aanmaak en toon in de console applicatie:
public static void Main()
{
Container c1 = new Container(0, "Rommel", DateTime.Now, 1000.0, "Vuilbak", "CVO");
Container c2 = new Container(0, "Electronische apparaten", DateTime.Now, 1000, "Hong Kong", "Chung");
Container c3 = new Container(0, "Meubelen", DateTime.Now, 1000, "Zweden", "IKEA");
Container c4 = new Container(0, "Toys", DateTime.Now, 4000, "Nederland", "Bart Smit");
Container c5 = new Container(0, "Books", DateTime.Now, 1000, "England", "De Standaard Uitgeverij");
Container c6 = new Container(0, "Fietsen", DateTime.Now, 2000, "Duitsland", "Gazelle");

Location l1 = new Location(3, 1, 5000);
Location l2 = new Location(3, 2, 5000);
Location l3 = new Location(3, 3, 5000);

Lane lane1 = new Lane(100000, 1, 20);

Terminals t = new Terminals(500000);

l1.ContAdd(c1);
l1.ContAdd(c2);
l1.ContAdd(c3);
l2.ContAdd(c4);
l2.ContAdd(c5);
l1.ContRemove(1);
l3.ContAdd(c6);

//lane1.ContAdd(c1);
//lane1.LocAdd(l1);
//lane1.ContAdd(l1, c2);
//lane1.ContAdd(l1, c1);


//Hier vraag ik aan Lane om in elke locatie te kijken welke containers er staan en print die op het scherm
foreach (Location l in lane1.MyLocations)
{
foreach (Container c in l.MijnContainers)
{
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine(c);
}
}
Console.WriteLine(lane1);
Console.ReadLine();

//Hier vraag ik om het aantal containers dat in een lane zit op het scherm te tonen.
Console.WriteLine("---------------------------------------------------------------------");
Console.WriteLine("The nbr of containers in lane is: " + lane1.GetNrOfContainers());
Console.ReadLine();
}

En wat werkt er volgens jou nog niet naar behoren?

Sorry, nog niet veel ervaring met de werking van een forum, zal code in de toekomst tussen tags zetten.

Ik zou in de console moeten zien welke containers met al hun attributen op welke locatie in een Lane staan.
Dit is niet het geval.
De gegevens die ik meegeef in de override ToString() methode (zoals het aantal locations dat in een lane kan, en het max.gew.dat een lane kan bevatten) zie ik maar geen informatie van welke locaties en welke containers.

Ambertje
 
Heb je al een breakpoint gezet in de foreach?
Dan single steppen en kijken wat er in de objecten aanwezig is.

Tevens zou je kunnen proberen om de ToString() aan te roepen:
PHP:
Console.WriteLine(c.ToString())
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan