Hej Hej igen
kan ikke lige se hvad der er galt i denne kode.
using System;
using System.Collections;
using System.Text;
namespace ArrayListeksempel
{
class VareCollectionDemo
{
static void Main(string[] args)
{
VareCollection varer = new VareCollection();
Vare v1 = new Vare(1, "bil", 100000.00);
varer.Tilfoj(v1);
Vare v2 = new Vare(1, "hus", 2000000.00);
varer.Tilfoj(v2);
varer.PrintAlle();
varer.Slet(v2);
varer.PrintAlle();
}
}
public class Vare
{
public Vare(int a, string v, double p)
{
antal = a;
varetype = v;
pris = p;
}
private int antal;
public void SetAntal(int a)
{
antal = a;
}
private string varetype;
public void SetVaretype(string v)
{
varetype = v;
}
private double pris;
public void SetPris(double p)
{
pris = p;
}
public int GetAntal()
{
return antal;
}
public string GetVaretype()
{
return varetype;
}
public double GetPris()
{
return pris;
}
}
public class VareCollection
{
//attributter
private ArrayList v;
//constructor
public VareCollection()
{
v = new ArrayList();
}
public void Tilfoj(Vare av)
{
v.Add(av);
}
public void Slet(Vare av)
{
v.Remove(av);
}
public void PrintAlle()
{
foreach (string s in v)
Console.WriteLine(s.ToString());
}
}
}
Håber at der er en der kan se og få den til at kunne kører
Fejl som VS2005 kommer med er:
System.InvalidCastException was unhandled
Message="Et objekt af typen 'ArrayListeksempel.Vare' kan ikke konverteres til typen 'System.String'."
Source="ConsoleSkole1"
StackTrace:
ved ArrayListeksempel.VareCollection.PrintAlle() i C:\\c#\\ConsoleSkole1\\ConsoleSkole1\\Program.cs:linje 79
ved ArrayListeksempel.VareCollectionDemo.Main(String[] args) i C:\\c#\\ConsoleSkole1\\ConsoleSkole1\\Program.cs:linje 17
ved System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
ved System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
ved Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
ved System.Threading.ThreadHelper.ThreadStart_Context(Object state)
ved System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
ved System.Threading.ThreadHelper.ThreadStart()
InnerException:
Indlæg senest redigeret d. 10.03.2008 21:25 af Bruger #4316