|
The addition assignment operator += was probably just added as it is shorter and looks cooler. It's used in other languages and I read somewhere theat in C it is marginally more efficient once compiled than using x=x+n. So to see if the is a difference in vb.net I ran 500 passes through:
For i As Double = 0 To 100000000
For j As Double = 0 To 1000000000
i += rand.Next(0, 100)
j += rand.Next(0, 100)
j += rand.Next(0, 100)
i += rand.Next(0, 100)
Next j
Next i
(and then did it for the i = i + blah version)
and averaged the result.
results:
x+=n : 3.7274s
x=x+n : 3.7071s
I doubt the difference is significant, it probably needs more passes. It ran one pass through one version, then one pass through the other version, repeatedly, so the difference wasn't due to an external influence.
|
|
|
|
|
Here's my 2 cents worth:
I would think that the processing time gained by using the assignment operator is neglible if the variable is something like an integer or double, but when it becomes more complex, with database calls and long calculations etc, it will be more efficient to evaluate the variable only once as opposed to twice as in i=i+1 ?
Dave
|
|
|
|
|
There is no logical difference at all. I doubt that there is any difference in execution speed because the compilers are 'smart enough' these days to optimize the code anyway. The only difference I can see is the one of personal preference or readability. Personally I prefer a+=50 rather than a=a+50 but one could argue that the latter is more meaningful to anyone who reads the code.
...Steve
|
|
|
|
|
According to the compiler, there is no difference what-so-ever. They both compile to exactly the same IL code.
Dim a As Integer = 0
Dim b As Integer = 0
a+=50
b=b+50
The resulting IL:
.maxstack 2
.locals init ([0] int32 a,
[1] int32 b)
IL_0000: nop ' No operation
IL_0001: ldc.i4.0 ' Push a 4 byte integer (int32) value of 0 onto the eval stack.
IL_0002: stloc.0 ' Pop the top value off the eval stack into storage location 0 (a).
IL_0003: ldc.i4.0 ' Push a 4 byte integer (int32) value of 0 onto the eval stack.
IL_0004: stloc.1 ' Pop the top value off the eval stack into storage location 1 (b).
' Here is the a+=50 statement:
IL_0005: ldloc.0 ' Push the value in storage location 0 onto the top of the eval stack.
IL_0006: ldc.i4.s 50 ' Push the value 50 onto the top of the eval stack.
IL_0008: add.ovf ' Add the two values at the top of the eval stack, with overflow checking.
' This replaces the top two values of the eval stack with the result.
IL_0009: stloc.0 ' Pop the result off the eval stack into storage location 0 (a).
' Here is the b=b+50 statement:
IL_000a: ldloc.1 ' Push the value in storage location 0 onto the top of the eval stack.
IL_000b: ldc.i4.s 50 ' Push the value 50 onto the top of the eval stack.
IL_000d: add.ovf ' Add the two values at the top of the eval stack, with overflow checking.
' This replaces the top two values of the eval stack with the result.
IL_000e: stloc.1 ' Pop the result off the eval stack into storage location 1 (b).
' End of the function, return to caller.
IL_000f: nop
IL_0010: ret
As you can see in the bolded code, the 4 IL statements that make up each VB statement are identical, except, obviously, for the storage location identifiers.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-- modified at 13:19 Tuesday 11th October, 2005
|
|
|
|
|
Hi all,
i am running a funciton which will check whether the url which is pass thro this function is working or not it working fine with all url except some url, for example'http://www.getforme.com/e404.htm' it working fine when i open this url in my IE, but when i run thro my application it return me ERROOR (The remote server returned an error (404) not found. this is my code snippets
Public Function checkurl(ByVal url As String) As String
Dim req As System.Net.HttpWebRequest
Dim res As System.Net.HttpWebResponse
Dim r As System.IO.StreamReader
Dim ex As Exception 'error exeption holder
Dim pge As String 'page holder
Dim title As String
Dim strreturnstring As String
url = "http://www.getforme.com/e404.htm"
(or)
url = "http://environmentalchemistry.com"
Try
'display request url
req = req.Create(url)
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)"
req.AllowAutoRedirect = True
'get page
res = req.GetResponse()
r = New System.IO.StreamReader(res.GetResponseStream())
'pge = r.ReadToEnd
r.Close()
res.Close()
strError = "OK"
status = "True"
'Convention is First Value is the Status and Second value is Errmsg
strreturnstring = status & "~" & strError
Return strreturnstring
Catch ex
status = "False"
strError = ex.Message
strreturnstring = status & "~" & strError
Return strreturnstring
End Try
End Function
The above two url is working fine in ie but when i try to run thro my application thro error
Any suggestion
regards
cyus
-- modified at 2:38 Monday 10th October, 2005
|
|
|
|
|
It is behaving as you would expect as that url is for an error page which returns an error code.
|
|
|
|
|
|
guys.... i need a live, compiled example about creating a variable in any location in the registy....
militiaware....Faris Madi
|
|
|
|
|
In VB6 or VB.NET ? I assume VB.NET, because you say win32.registry above. If so, why didn't you try google[^] ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
msdn library?
'example - store a string holding a folder location<br />
Dim backupFolder As String<br />
'ubstantiate a RegistryKey object<br />
Dim regEntry As Microsoft.Win32.RegistryKey<br />
'set it to the location we want:<br />
regEntry = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\TestApp", True)<br />
'if this is first run then it wont exist yet so create it:<br />
If regEntry Is Nothing Then<br />
regEntry = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\TestApp")<br />
'create a subkey:<br />
regEntry.SetValue("Backup Path", Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\Backup")<br />
End If<br />
'check we have a usable value in the subkey (and that the subkey exists)<br />
If regEntry.GetValue("Backup Path").ToString = "" Or regEntry.GetValue("Backup Path").ToString = Nothing Then<br />
'set it to default path<br />
regEntry.SetValue("Backup Path", Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\Backup")<br />
End If<br />
'set backupfolder <br />
backupFolder = regEntry.GetValue("Backup Path").ToString<br />
Me.Text = "Current backup folder: " & backupFolder
Creates "HKEY_CURRENT_USE/Spftware/TestApp/Backup Path" on first run.
|
|
|
|
|
I have a textbox that the user inputs numbers into. i would like to be able to create a KeyDown event for the control that, when the user presses the UP arrow, the value raises by one. this i have accomplished. but unfortunately this does not override the action already taken (moving the cursor to the left). how to i override this?
thanks ahead of time,
stephen
|
|
|
|
|
Sounds like you could use a numericUpDown control instead.
|
|
|
|
|
tried it already. those take up more space than i have.
|
|
|
|
|
Hmm, well you intercept the key, increase the value, and say event handled. The caret won't move as you haven't asked it to.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown<br />
If (e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down) AndAlso IsNumeric(Me.TextBox1.Text) Then<br />
Dim i As Integer = CInt(Me.TextBox1.Text)<br />
'check key again, if it is up add 1, otherwise add -1<br />
i += IIf(e.KeyCode = Keys.Up, 1, -1)<br />
Me.TextBox1.Text = i.ToString<br />
'start text selection at end of text, length 0 to move caret<br />
Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length<br />
Me.TextBox1.SelectionLength = 0<br />
e.Handled = True<br />
End If<br />
<br />
End Sub
|
|
|
|
|
ah. i missed the e.handled = true part.
|
|
|
|
|
Hi all..
Im done with my coding using vb 6.0, and i would like to create an exe file that i can run my program with.
but the problem is that a message pops out telling me that the project source dose not have a binary compatibility file set.
how can i get over that problem
SS
|
|
|
|
|
Go to the Component tab of the project's properties and choose either the Project Compatibility or the No Compatibility radio button.
...Steve
|
|
|
|
|
I created a progress bar in statusbar by using the
drawitem of stausbar and fill rectangle method.
I use a loop to increase the width which eventually shows as a progress bar. But the problem is each time the statubar1.refresh is called it refreshes
and the effect is seen on the form which is not smooth enough for a smooth Progress Bar.
How can i make my progress bar smooth enough even after calling the refresh method of statusbar each time
Hi i am sumod. A beginner in VB.net
|
|
|
|
|
What do you mean by "smooth enough"? Why are you even using a loop to draw a progress bar? Are you just showing that something is happening or are you showing actual measured progress?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I'm wanting to read a news group with program. Just simple code that grabs the messages.
Any ideas?
|
|
|
|
|
I just noticed that google has some sort of RSS thing.
http://groups.google.com/group/ott.jobs/about
Is that easier?
|
|
|
|
|
This is probabally a lot more complex than i think, but here is what i'm trying to do:
Take a specific memory location of a program, and be able to change the integer value on a key press.
i just started from total scratch on this project yesterday, with no knowledge of anything of the sort. so far, i've been able to change the memory location values using artmoney, and i also learned that the address changes everytime i start a program. i've gotten that figured out, establishing a pointer which takes care of the changing address location. what i need to do now, is create a script or something, preferrably in vb, where upon a key press, it will change the value of that address:
e.g.)
*program starts
1. press F9
2. memory address 00000A80 changes from value 22.5 to 12.3, type float 4 bytes
if anyone could help me with some guidance your praise would not be short lived.
many many thanks in advance,
-stu
i need to know everything!
|
|
|
|
|
Have a look at two articles here on CodeProject which are about changing the behaviour of the game FreeCell coming with Windows. They do it by directly changing the memory of the game. Might be usefull for you.
|
|
|
|
|
nocopro wrote:
Take a specific memory location of a program, and be able to change the integer value on a key press.
Ummm... why? Sounds dangerous to me.
...Steve
|
|
|
|
|
i just want to be able to see if i can do it.
no, i do not hack games and programs. that takes all the fun out of games, and takes away from the effort people put into making programs.
it could be dangerous on the fact that the program might crash, but i have been able to pinpoint certain memory locations and know their exact functions and change the values in the memory location to get desired results, but i don't know how to script a program to do this function.
i need to know everything!
|
|
|
|