- Home /
c# Exists() causing error CS0135
Why does the Exist()
not work here?
if(listOfPlayers.Exists(listOfPlayers => listOfPlayers.netID == uniqueId) == false)
{
foreach(NetworkUser user in listOfPlayers)
{
if(user.active == false)
{
user.netPlayer = Network.player; // NetworkPlayer object
uniqueId = user.netID;
user.characterName = characterName;
user.characterClass = characterClass;
user.active = true;
user.netPlayer = networkPlayer;
user.characterLevel = characterLevel;
break;
}
}
}
error on line 1:
error CS0135:
listOfPlayers' conflicts with a declaration in a child block`
I have problems figuring out what this error means for me.
depends a lot on what type listOfPlayers
is. it may not have that particular method available.
having said that - from the message, i don't think that's the error...
Could you please post the full script and the full error log?
I think you are assu$$anonymous$$g too much! Post more context and the original error message.
the error is there, but there's not enough context (code) to respond further.
Answer by Gatsua · Nov 22, 2014 at 06:39 PM
sorry, it was a simple syntax issue. This is how the Exists()
should look like:
if(listOfPlayers.Exists(item => item.netID == uniqueId) == false)
{
...
}
Style suggestion
Comparing values against true
and false
screams amateur. Exists() is returning a bool. if() takes a bool. No comparison necessary.
if(!listOfPlayers.Exists(item => item.netID == uniqueId))
{
...
}
@flaviusxvii $$anonymous$$an, it doesn't really matter. Either way works.
@flaviusxvii Comparing to a bool
or using !
does not deter$$anonymous$$e if a developer are an amateur or not. Often when I decide whether to use a bool
or !
it's a matter of code clarity. eg when I use _.isUndefined()
in JavaScript I prefer to use !
because in my head I read it as "not". As far as I know, there are no performance differences. Please correct me if im wrong.
Please elaborate with why I should rather do it your way.
a lot of it is personal preference - assu$$anonymous$$g that the code is functioning properly. whether to include comparisons to true
or false
is neither here nor there - as has been stated, the compiler will (usually) generate equivalent code.
it often has more to do with the developers experience and what is a familiar coding style. whilst i prefer something more concise (less code may be less likely to contain bugs or misunderstandings if others are involved), it depends on the individual. for me, it's more important that the code is clear in its' intent (negating the need for often superfluous, inaccurate or inane commenting) and easier to maintain, regardless of that poor soul being me or somebody else.
statements such as "Comparing values against true and false screams amateur." are not exactly helpful and really not in the spirit of unity answers. flame away if somebody is outright wrong, and feel free to suggest other approaches which work, but never be that guy!
here endeth the sermon...
ps i've been that guy ;)
Literally expressing what you do does add clarity to the code and it does help me and the people I work with, along with the graphical designers who might not have as experienced eyes for codes as you and me, to better see the picture. This is not superfluous code for this reason. I'm also quite good at commenting algorithms that might not be clear to the other coders reading the code. All of this prevents someone from having to re-read the section. The other day I had to spend 30 $$anonymous$$utes just understanding another persons 20 lines long search function due to a cryptic code style without any comments inside the block and that is obviously a waste of time when adding a little comment takes about 15 seconds in total and has no effect on the outcome of the app. I find it very important to type clear codes at all times, even if you are the lone coder of the project.
If you are going to give me a style suggestion you will have to come with a valid reason why I should start doing it your way. Because things such as "it's not pro" is far from a valid reason. Neither is it humorous to call me an amateur or fool I'm afraid and it's what you can call "not professional" or !professional
, see the irony here?
I am curious and would love to know from both of you why literally and directly expressing the purpose is not more clear for you. I find the !
operator to be less clear to interprate simply because it negates a bool. For example:
1. if(!isActive) { isActive = true; }
2. if(isActive == false) { isActive = true; }
In my head I read these as:
1: "if not isActive is true, set isActive to true"
2: "if isActive is false, set isActive to true"
And number 2 makes more sense in my head. It's about saying that "something is false" then "something is not true". It feels like a more direct explanation then taking the detour of stating what it's not. Then ofcourse, this is just personal preference.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Sync with visual studio 2010 error 1 Answer
No Monobehaviour scripts in files 1 Answer
2-Dimensional Array Error [CLOSED] 1 Answer