-1

ive looked at duplicate errors like this but i havent found a solution

here is my method im implementing in the business logic

  public  void UpdateBooking(BookingView model)
    {
        using (var booking = new BookingRepository())
        {
            var user = new ApplicationUser();
            Booking book = booking.GetById(model.BookingId);

            if (book != null)
            {

                book.BookingId = model.BookingId;
                book.BookingDate = model.BookingDate;
                book.BookingTime = model.BookingTime;
                book.Location = model.Location;
                //book.Status = DefaultStatus();
                //book.TreatmentName = book.TreatmentName;
                //book.AddInfo = model.AddInfo;

                booking.Update(book);
            }
        }
    }

but my error is on my booking controller method

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult postponeBooking([Bind(Include = "location,BookingDate,BookingTime")]BookingView model)
    {

        if (ModelState.IsValid)
        {

            BookingBusiness.UpdateBooking(model);

        }
        return RedirectToAction("BookingDetails", TempData["alertMessage"] = "<script> alert('Booking details saved!!');</script>");
    }

exactly on this line

            BookingBusiness.UpdateBooking(model);

im completely clueless i tried adding static to my methods but still no luck.maybe im not understanding the error properly i need abit of guidance thank you

minenhle
  • 57
  • 1
  • 9
  • 4
    If BookingBusiness is not a static class, you need an instance of it (i.e. do a `new BookingBusiness()` somewhere) and use that instance for the `UpdateBooking` method. You cannot call classes magically like that, unless they're static. – Pierre-Luc Pineault Jul 08 '15 at 19:39
  • @Pierre-LucPineault ... looks like an answer :) – Johnie Karr Jul 08 '15 at 19:41
  • 2
    @JohnieKarr Looks like a duplicate, I'll flag it instead. – Pierre-Luc Pineault Jul 08 '15 at 19:41
  • `An object reference is required for the non-static field, method, or property` the error is pretty explicit and means exactly what it says. `UpdateBooking` is an *instance* method and so you need an *instance* of `BookingBusiness` to call it. Now since your method doesn't appear to actually touch any members of the `BookingBusiness` class at all, you could just mark it static. If you whole class is like that, then probably the class should be static. Also note, if you make the a method static, it won't be able to call other instance methods either unless you give it an instance to work with. – Matt Burland Jul 08 '15 at 19:44
  • @Pierre-LucPineault: The class doesn't *have* to be static. You could just make that method static since it doesn't seem to be accessing any variables outside it's own scope. – Matt Burland Jul 08 '15 at 19:46
  • possible duplicate of [Error: "an object reference is required for the non-static field, method or property..."](http://stackoverflow.com/questions/2505181/error-an-object-reference-is-required-for-the-non-static-field-method-or-prop) – Matt Burland Jul 08 '15 at 19:47

1 Answers1

0

To use your UpdateBooking method, either your BookingBusiness class or UpdateBooking method must be static (you won't refer to individual instance objects) OR you must instantiate an object of type BookingBusiness like so (obviously depending on how you intend on implementing these):

BookingBusiness yourObject = new BookingBusiness(/*constructor args*/);  

Then you can call your method via yourObject.UpdateBooking(model);

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
  • thank you for pointing that out.i actually had instantiated an object of type bookingbusiness but i then forgot to use it to call my updatebooking method and used my bookingbusiness clss directly which is wrong.the help is appreciated – minenhle Jul 08 '15 at 20:11
  • @minenhle, glad I could help. It's always funny when our issues are caused by such small mistakes :) – Broots Waymb Jul 08 '15 at 20:32
  • @DangerZone - It's not always funny! Sometimes it's downright maddening! :P – Chris Dunaway Jul 08 '15 at 21:31
  • @ChrisDunaway, very true! At least this one gave an error. Can't tell you how many programs I've wasted hours on because I did something stupid like `myClass.ToString();` and expecting console output or `someString.Replace("X","O");` and expecting a different string from `someString` much later! – Broots Waymb Jul 08 '15 at 21:35