Xaisoft
Xaisoft

Reputation: 46591

Cannot find the method on the object instance

I have an object called Reservations. Inside Reservations, there is a another object called Reservation that is an array.

When I do something like the following:

reservations.Reservation[i] = new Reservation(); //reservations if of type Reservations

I get the following error:

Cannot find the method on the object instance

A couple things I noticed is that when I get to this point, it throws an IndexOutOfRangeError and if I hover over the reservations variable, it shows a number.

VB Code

Public Class Reservations
{
    Private RerservationField() as Reservation

    Public Property Reservation() as Reservation()
        Get
            Return Me.ReservationField
        End Get
        Set
            Me.ReservationId = value
        End Set
    End Property
}

Dim reservations As New Reservations()
ReDim reservations.Reservation(0)

Dim i as Integer = 0

reservations.Reservation(i) = New Reservation()

C# Code

public class Reservations
{
    Reservation[] reservation {get;set;}
}

Reservations reservations = new Reservations();
reservations.Reservation = new Reservation[0];
int i = 0;

The VB Code and C# Code, it is instantiating the length of the array based on an xml request. So in the vb code, it was doing this:

 ReDim reservations.Reservation(rq.Reservations.Reservation.Length - 1)

and in the c# code, it was doing this:

reservations.Reservation(rq.Reservations.Reservation.Length - 1).

Is the above my problem. I need to do:

reservations.Reservation(rq.Reservations.Reservation.Length)

rq is the request.

Upvotes: 2

Views: 4058

Answers (5)

Mike Nakis
Mike Nakis

Reputation: 61969

Visual studio will report "Cannot find the method on the object instance" if your app references a certain assembly in your solution, (which gets loaded from your app's bin directory,) and your app also uses assembly-loading which causes the same assembly to be loaded from a different bin directory. (Or if you are using some other library which, unbeknownst to you, does that.) Then, when you try to debug the loaded assembly, Visual Studio gets confused and cannot reflect the members of types of the duplicate assembly.

In the majority of cases, this is the result of misconfiguration, and can be solved by exiting Visual Studio, deleting all your output directories, deleting the .vs directory, etc.

If you are knowingly engaging in assembly-loading, then before loading an assembly you need to always check whether it has already been loaded, (by assembly name, not by dll file path,) and if so, use the already-loaded assembly instead of loading a duplicate.

Upvotes: 0

BlackCode
BlackCode

Reputation: 11

Had the same issue. Fixed the problem by going into my Solution Properties. Under the Common Properties, there is Debug Source Files. In the "Do not look for these source files" section, I had a file that was required for my debugging. Removed all the entries ... BOOM fixed.

Hope it helps ! I lost considerable time on that debug issue.

Upvotes: 1

flayn
flayn

Reputation: 5322

The reason for the IndexOutOfRangeException is most likely because the reservations.Reservation[i] Array contains less elements than i is. Check the reservations.Reservation.Count().

Upvotes: 1

Nix
Nix

Reputation: 58522

Your line:

reservations.Reservation = new Reservation[0];

Will create an array of max length 0? Which will basically be non null, but empty..

You need to instantiate the reservations.Reservation array (SIZE would be how large you want it). You do not** need to -1 from your length. If you specify new Array[1] it will be able to hold 1 element, 2 2 elements, etc.

Reservations reservations = new Reservations();
size = rq.Reservations.Reservation.Length
reservations.Reservation = new Reservation[size ];
reservations.Reservation[0] = new Reservation();

You could do it in the constructor if you want:

public class Reservations
{
  Reservation[] reservation {get;set;}
  public Reservations(int size){
    reservation  = new Reservation[size];
  }
}

Then the middle step would no longer be needed i.e.:

Reservations reservations = new Reservations(rq.Reservations.Reservation);
reservations.Reservation[0] = new Reservation();

Note you should look into using a more advanced collection List

public class Reservations
{
  List<Reservation> reservation {get;set;}
  public Reservations(){
    reservation  = new List<Reservation>();
  }
}

And if you need it as an array you can just do

 reservation.Reservations.ToArray();

and to insert it would be

 reservation.Reservations.Add(theReservation);

Upvotes: 2

Oleg Dudnyk
Oleg Dudnyk

Reputation: 1009

Use dynamic Array (List< T>):

var reservation = new List<Reservation>();
reservation.Add(new Reservation());

Array has a fixed length and if you try to add something to position which is more then this length - of course it causes IndexOutOfRangeException.

Upvotes: 1

Related Questions