love code

love code

  • NA
  • 33
  • 2.4k

How to solve 'namespace' and The name .. does not exit

Feb 13 2018 1:34 PM

How do i solve the following coding errors ? See bold .class code below :-)

  1. GetAvailableOffers() / a namespace cannot contain member sutch as fields or methods (code error)
  2. foreach (Article article in articles) / The name articles does not exit in the currenct context /(code error)

// code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sauna
{
class SaunaApp
{
private List<Staffmember> staff;
private List<Article> articles;

public SaunaApp()
{
staff = new List<Staffmember>();
articles = new List<Article>();
FillWithData();
}

public void FillWithData()
{
articles.Add(new Meat("Steak", 3.2m, DateTime.Now));
articles.Add(new Meat("Grill", 3.9m, DateTime.Now.AddDays(3)));

articles.Add(new Fish("Salamon", 8.3m, DateTime.Now.AddDays(1)));
articles.Add(new Fish("rab", 8.3m, DateTime.Now.AddDays(2)));

articles.Add(new Towel("Large", 2.5m));
articles.Add(new Towel("Small", 2.5m));

articles.Add(new Fish("Salamon", 8.3m, DateTime.Now.AddDays(1)));
articles.Add(new Fish("Krab", 8.3m, DateTime.Now.AddDays(2)));

articles.Add(new Towel("Large", 8.3m));
articles.Add(new Towel("Small", 8.3m));

articles.Add(new Relaxmassage("Relax", 8.3m));
articles.Add(new Relaxmassage("Sport", 8.3m));

}

public IReadOnlyList<Staffmember> ShowAllStaffmembers()
{
return staff.AsReadOnly();
}

public bool AddStaffmember(String name, int age)
{
foreach (Staffmember staffmember in staff)
{
if (staffmember.Name == name)
{
return false;
}
}
Staffmember newStaffmember = new Staffmember(name, age);
staff.Add(newStaffmember);
return true;
}

public bool RemoveStaffmember(string name)
{
foreach (Staffmember staffmember in staff)
{
if (staffmember.Name == name)
{
staff.Remove(staffmember);
return true;
}
}
return false;
}

public IReadOnlyList<Article> ShowAllArticles(bool sortByPrice)
{
if (!sortByPrice)
{
articles.Sort();
}
else
{
articles.Sort(new ArticleCompare());
}
return articles.AsReadOnly();
}
public void ExportArticles(string filename)
{
using (FileStream stream = new FileStream(filename, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
int counter = 0;
foreach (Article article in articles)
{
writer.Write(article.ToString() + ";");
}
if (counter == 3)
{
writer.Write(Environment.NewLine);
counter = 0;
}
}
}
}
}


public IReadOnlyList<IAvailable> GetAvailableOffers()
{
List<IAvailable> offers = new List<IAvailable>();
foreach (Article article in articles)
{
if (article is IAvailable)

offers.Add(article as IAvailable);
}
return offers.AsReadOnly();

}
}


Answers (4)