2

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.

5
  • Are you trying to add a new reservation to your array? Is i outside the bounds of that array? Commented Jun 20, 2011 at 20:52
  • i is zero, but after I assign it, i is some long number I have no idea where it came from. Commented Jun 20, 2011 at 20:53
  • An array is not a dictionary where you can assign reservations.Reservation[i] = new Reservation(); Commented Jun 20, 2011 at 20:56
  • Post all the code if You want answer. Commented Jun 20, 2011 at 20:56
  • I will update the post with the vb code and put the c# code I did instead. Commented Jun 20, 2011 at 20:58

5 Answers 5

2

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); 
Sign up to request clarification or add additional context in comments.

6 Comments

I possibly could be doing it wrong. This was originally in vb and done like this: reservations.Reservation(i) = New Reservation()
reservations is instantiated in the beginning.
I would look into List, but I am stuck right now with some crazy code. When instantiating in c#, I also have to start at 1 correct instead of 0 for vb when dealing with arrays
Ok, i will update by post with extra code. Let me know if it makes sense.
Updated code. Thanks for pointing me in the right direction so far.
|
1

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.

Comments

1

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().

Comments

1

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.