Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Trying to understand some C# code on the net:

 

 

if ((string1 = string2) != null) {

 

 

}

 

 

What is this saying?

 

Translators recommend:

 

if not (string1=string2) is nothing then

 

end if

 

Which is incorrect as string1=string2 is boolean.

 

I therefore thought it might be:

 

if string1<>string2 then

 

but it seems to be doing more than this. Any ideas?

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Posted

If (Not (string1 = string2)) Then
     'some code here...
End If

 

Trying to understand some C# code on the net:

 

 

if ((string1 = string2) != null) {

 

 

}

 

 

What is this saying?

 

Translators recommend:

 

if not (string1=string2) is nothing then

 

end if

 

Which is incorrect as string1=string2 is boolean.

 

I therefore thought it might be:

 

if string1<>string2 then

 

but it seems to be doing more than this. Any ideas?

 

:)

Me = 49% Linux, 49% Windows, 2% Hot gas.

 

...Bite me.

 

My Site: www.RedPierSystems.net

-.net, php, AutoCAD VBA, Graphics Design

Posted

What the difference between this:

 

If (Not (string1 = string2)) Then

'some code here...

End If

 

and

 

If (string1 <> string2) Then

'some code here...

End If

 

?

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Posted

absolutly nothing. The <> method is just less code.

 

What the difference between this:

 

If (Not (string1 = string2)) Then

'some code here...

End If

 

and

 

If (string1 <> string2) Then

'some code here...

End If

 

?

Me = 49% Linux, 49% Windows, 2% Hot gas.

 

...Bite me.

 

My Site: www.RedPierSystems.net

-.net, php, AutoCAD VBA, Graphics Design

Posted
The correct translation of

if ((string1 = string2) != null)
{
   //....
}

is:

string1 = string2
If Not string1 Is Nothing Then
   '...
End If

 

Thats right. = is not the same as == (duh! ) :D

Me = 49% Linux, 49% Windows, 2% Hot gas.

 

...Bite me.

 

My Site: www.RedPierSystems.net

-.net, php, AutoCAD VBA, Graphics Design

Posted
Which is incorrect as string1=string2 is boolean.
one of the many inadequacies of VB.NOT (context sensitive operators). . .

 

In c#, the assignment falls through so you can initialize things like:

 

 

string s, s1;
string s2 = s1 = s = "foobar";
int i, i1;
int i2 = i1 = i = 1;
MyObject mo, mo1; 
MyObject mo2 = mo1 = mo = new MyObject(); 

 

with those statements:

s, s1, and s2 all contain "Foobar". . .

i, i1, and i2 all contain 1. . .

 

and

mo, mo1 and mo2 all contain a reference to the same instance of a MyObject.

 

that being said. . .

 

if ((string1 = string2) != null)
{
//....
}

 

reads:

 

If, after assigning string2 to string1, the value assigned to string1 is not null then. . .

 

in vb it would be:

 

string1 = string2

if ( not (string1 is nothing) ) then

 

end if. . .

 

Also keep in mind -

 

dim mystring as String = nothing

dim test as bool = mystring = string.Empty

 

The value of test is false.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...