If the statement is always true with an enumeration in comparison

I have a problem. I am making a utility for creating procedural maps. I have a pool in the room and each room is located in the dining room. I have a way to connect the whole room together, which goes on the table and connects the neighboring rooms.

I have an enum that contains the type of rooms:

 public enum RoomType { Default = 0, Building, Boss, Item, Standard, Start, } 

In the connection method, I check the surroundings to find out which room it is in:

 if (neighbourhood[2, 1] != null) { if (firstLevel.isOn) { if (neighbourhood[2,1].TypeOfRoom == RoomType.Start) { roomGrid[x, y].AddConnection(neighbourhood[2, 1], Location.RIGHT) } } else if (neighbourhood[2,1].TypeOfRoom != RoomType.Boss) roomGrid[x, y].AddConnection(neighbourhood[2, 1], Location.RIGHT); } 

But when I check if there is a Start room type, it is always true and the connection is added.

imgimg2

I do not know why this is happening.

where i install TypeOfRoom: img3

+5
source share
3 answers

The problem is most likely related to the state of the race. You can easily check if this matches the following:

 if (neighbourhood[2, 1] != null) { if (firstLevel.isOn) { var typeOfRoom = neighbourhood[2,1].TypeOfRoom; //store type in a local variable if (typeOfRoom == RoomType.Start) //check against local copy { roomGrid[x, y].AddConnection(neighbourhood[2, 1], Location.RIGHT) } } ... } 

Now you will see that the if condition works fine, but the neighbourhood[2,1].TypeOfRoom will not equal typeOfRoom , which means that it changes in another thread.

If you don’t know how and where your objects change in other threads, then you have problems that you need to solve more, because you do not seem to understand the code or structure you are using.

If or when you understand why and when this happens, you will need to implement some kind of synchronization mechanism or direct (preferably) to immutable implementations.

+4
source

What you show in the screenshot and what you describe is impossible. When this happens, it often happens because the application is multithreaded. This is not possible in a single thread. But when another thread is executed, the values ​​change.

At the same time, you can also inadvertently pass references to the same object to several functions.

But definitely check something related to threads, tasks, or calls to parallel functions. Or, if it is running in a web environment, see if multiple requests can access shared data.

+3
source

For RoomType.Standard, a connection will always be added - as you find:

  if (firstLevel.isOn) { if (neighbourhood[2,1].TypeOfRoom == RoomType.Start) // FALSE { // add connetion } } else if (neighbourhood[2,1].TypeOfRoom != RoomType.Boss) // TRUE { // add connection } 

The standard is not a Boss, so a connection is added anyway.

With enumerations, you get fewer errors with switch statements:

  switch (neighbourhood[2,1].TypeOfRoom) { case RoomType.Start: // add connection break; case RoomType.Boss: // something break; case RoomType.Standard: // something else break; default: // all other room types break; } 
-1
source

Source: https://habr.com/ru/post/1246634/


All Articles