Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
VB
If role = "Administrator" Then
   MultiLevelMenu.Visible = True
Else
   MultiLevelMenu.Visible = False
End If

or
VB
MultiLevelMenu.Visible = role = "Administrator"


What I have tried:

Is it just readability?
Posted
Updated 30-Jan-17 22:48pm
v3

Use the first: the second is confusing to read as it implies that it should read as two assignments:
VB
role = "Administrator"
MultiLevelMenu.Visible = role
(This is one of the reasons C# is a better language than VB: the assignment operator "=" is not the same as the equality test operator "==")

If you feel you must write the second form, then at least use brackets to make it clearer:
VB
MultiLevelMenu.Visible = (role = "Administrator")
But just because you can do something, doesn't mean you should...
 
Share this answer
 
I am not from VB.Net background but if I can relate this to C#, either you can try the first one i.e,
VB
If role = "Administrator" Then
   MultiLevelMenu.Visible = True
Else
   MultiLevelMenu.Visible = False
End If

Or you can use Ternary Operator
VB
'MultiLevelMenu.Visible = (role == "Administrator"? true : false) //in c#
MultiLevelMenu.Visible = If(role = "Administrator", true, false)


From performance point of view both should be same as these statements will be translated to MSIL and should same at that time.

Hope, it helps :)
 
Share this answer
 
Both codes are doing the same and they compiled to same code too.
First form is usually preferred by beginners because they find it easier to read.
Second form is usually preferred by experimented users because it is more compact and as easy to read.
I prefer
VB
MultiLevelMenu.Visible = ( role = "Administrator" )

because some language allow cascade assignment and seconf form translate to:
VB
role = "Administrator"
MultiLevelMenu.Visible = role
 
Share this answer
 
Comments
Member 10103170 31-Jan-17 10:09am    
Thanks, was not sure if it was more efficient, but if it compiles to same code then a more easy to read segment will be fine.
Patrice T 31-Jan-17 11:32am    
Modern compilers are optimizers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900