|
This has nothing at all to do with boxing and unboxing.
When passing parameters, passing by value is the default. Passing by reference should only be used if you need to change the variable that you are passing in from inside the method. That is rarely needed, so you should rarely ever use ByRef.
You have to separate "passing by refererence" from "reference type". They are not connected at all. Just because they have similar words in their name doesn't mean that they work best together.
This is how it goes:
:: If you pass a value type by value, a copy of the value is sent.
:: If you pass a value type by reference, a reference to the variable is sent.
:: If you pass a reference type by value, a copy of the reference is sent.
:: If you pass a reference type by value, a reference to the reference is sent.
For both types, passing by value is the most convenient, that's why it's the default.
hrodberaht wrote: On the one hand it feels like ByRef would be better, since the address of the instance would be used, rather than a copy of the instance.
No, ByVal is better, as it uses a copy of the reference to the instance instead of a reference to the reference to the instance.
hrodberaht wrote: But then I read an article that states VB.net when using ByRef actually creates a copy of the instance, uses it as the argument, then copies it back to the original instance.
In .NET a copy of an object is never created automatically. If you need a copy of an object, you have to explicitly create it.
---
single minded; short sighted; long gone;
|
|
|
|
|
ByVal and ByRef passing of Value types...
Passing a Value type ByVal is easy. It simply copies the value onto the call stack. You now have two copies of the value, the original and the one the method parameter is holding.
Passing a Value type ByRef causes a reference to the variable to be passed to the method. This means there is a single copy of the value, the original, and two variables, the other being the one in the method code, have a reference to it. If either variable makes a change to the value, the other variable will also be looking at the same changed value.
Passing a Reference type ByVal is a bit different than what you might expect. A variable holds the the reference, or a pointer, to the object on the heap, so passing a reference ByVal causes a copy of the reference to be passed. This means that both the original variable and the variable in the method both point to the exact same object on the heap. If one variable make a change to the object, the other variable will be able to see the change.
Passing a Reference type ByRef causes a reference to the referencing variable to be passed. Confused? How about this. The method variable holds a reference to the original variable that holds the reference to the object. In C++ terms, that's a pointer to a pointer to an object.
So what's the difference between passing a Reference type ByVal and ByRef? There really isn't one. VB.NET handles de-referencing the "pointer to a pointer" for you. The only real difference is a small performance hit because of the dereferencing. You should really stick to passing Reference types ByVal and not ByRef.
[EDIT] About the only time you would pass a Reference type ByRef is if the method being called should be able to set the original reference to Nothing (null in C#).
-- modified at 14:26 Thursday 12th July, 2007
|
|
|
|
|
Sounds like for better performance, when dealing with Reference Types, it's better to use ByVal if you do not functions/subs to replace the entire object.
Thanks for all the replies, it's much appreciated. I've got a better idea how these types fit togeter now.
Thanks
Rob
|
|
|
|
|
In my application, i need to download multiple files from an ftp server every day, but sometimes, some files may be missing. I want to be able to check the existence of a file.
I searched a bit and found library's that offered those kind of actions, but if i use these library's, i have to trash all the code i already wrote.
I would like to know if someone knows how to check the existence of a file on a ftp server by using ftpwebrequest or something similar.
here is some code i already did, i didint have time to optimise it yet so if you can help in increasing the speed of the dopwnloads, i would like it alot. Oh and im using backgroundworker.
-----------------------------
Private Sub bgworker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgworker1.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
For Y = 1 To numsites
remoteFile = "/cors/rinex/" + year + "/" + day + "/" + sites(Y) + "/" + sites(Y) + day + "0." + VB.Right(year, 2) + "o.gz"
localFile = My.Application.Info.DirectoryPath + "\Zips\" + sites(Y) + day + "0." + VB.Right(year, 2) + "o.gz"
URI = host & remoteFile
ftp = CType(Net.FtpWebRequest.Create(URI), Net.FtpWebRequest)
ftp.Credentials = New _
System.Net.NetworkCredential(username, password)
ftp.KeepAlive = False
ftp.UseBinary = True
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Dim dwltimer As New Stopwatch
If Directory.Exists(My.Application.Info.DirectoryPath + "\Zips\") Then
Else
Directory.CreateDirectory(My.Application.Info.DirectoryPath + "\Zips\")
End If
Dim response As System.Net.FtpWebResponse = _
CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using response
Dim responsestream As IO.Stream = response.GetResponseStream
Using responsestream
dwltimer.Start()
'loop to read & write to file
Dim fs As New IO.FileStream(localFile, IO.FileMode.Create)
Using fs
Dim buffer(10000) As Byte 'plus c'est grand plus ca va vite (4096)
Dim read As Integer = 0
Do
If Me.bgworker1.CancellationPending Then
CType(bgworker1, BackgroundWorker).ReportProgress(0)
Exit Do
End If
read = responsestream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
dwltimer.Stop()
fs.Flush()
fs.Close()
End Using
responsestream.Close()
End Using
response.Close()
End Using
If Me.bgworker1.CancellationPending Then Exit Sub
progress = progress + 1
dwltime = dwltimer.ElapsedMilliseconds / 1000
CType(bgworker1, BackgroundWorker).ReportProgress(progress)
Next Y
MsgBox("downloads completed")
End Sub
|
|
|
|
|
You are restricted by the capabilities provided by the FTP server. If the FTP server doesn't support something like a "Exists?" command, then you'll have to just try and download the file and catch any errors if it doesn't exist.
Alternately, you can get the directory listing and see if the file you're looking for is in that list. That is, of course, if you have access to the irectory listing.
|
|
|
|
|
well, nevermind, i got a flash and found out a way to catch the exception but, thank you anyways, for looking at the thread
|
|
|
|
|
|
I am having issues while gathering user logged in from windows in Visual basic.... Please help!!
|
|
|
|
|
Ajit Sandhu wrote: I am having issues while gathering user logged in from windows in Visual basic
And those issues would be ....?
|
|
|
|
|
how do i use windows scripting host object to get the the username
|
|
|
|
|
You can either use the ExpandEnvironmentStrings[^] method of a WshShell object and user the environment variable USERNAME , or
You can use the UserName[^] property of a WshNetwork object.
|
|
|
|
|
Ajit Sandhu wrote: issues
You haven't mentioned any?
|
|
|
|
|
|
Write your own code. The CodeProject is not a "hand over the code" site. If you want this code, write it yourself. We're here to help you develop your own code, NOT to help you turn in someones elses work as your own.
|
|
|
|
|
Dave Kreskowiak wrote: hand over the code
|
|
|
|
|
Please read the forum guidelines. Some of the highlights:
This forum is not for just handing you some code. You need to code to learn. we are not going to do your homework/job for you. If you are having trouble with a specific peice of code, feel free to post that and we will help.
If you are not willing to learn, get out of coding now.
______________________
stuff + cats = awesome
|
|
|
|
|
|
try modifying the teacher starter kit of the visual studio website.
Posted by The ANZAC : "WWBD, What Would Buffy Do?" : "I don't know man, she's stronger than me"
|
|
|
|
|
No soliciting.
The initiative must be from you and CP veterans can lend you a helping hand if you hit a hard wall.
|
|
|
|
|
how do i get my app's taskbar item on the left most position on the start menu taskbar if there are several items on the taskbar.
|
|
|
|
|
Hi,
there are two ways I know of:
- close all other applications;
- more seriously: task bar is some kind of list control, it can be found if
you know the name of all the windows in its upward hierarchy; then you can
enumerate all the items, recognize the one you are interested in, and move it;
all this requires a lot of P/Invoke to call several Win32 functions.
I havent done it for task bar, I do know how to do it for the notification area,
and for desktop items; it is all similar (and quite different in the details).
Are you sure it is worth the trouble ?
|
|
|
|
|
Don't. You are guaranteed to annoy your users.
|
|
|
|
|
im building a application witch must be always open and for users with lots of app's open its easyer to have it leftmost on the taskbar
|
|
|
|
|
Public Class ControlStaffDemographics
Inherits DevExpress.XtraEditors.XtraUserControl
#Region " -- Member Class: ClassGroupListItem -- "
Public Class ClassGroupListItem
Public ReadOnly Group_PK As Guid
Public ReadOnly GroupName As String
Public Sub New(ByVal group As DataRow)
Me.Group_PK = DirectCast(group.Item("Group_PK"), Guid)
Me.GroupName = group.Item("GroupName").ToString
End Sub 'New(ByVal group As DataRow)
Public Overrides Function ToString() As String
Return Me.GroupName
End Function 'ToString()
End Class 'ClassGroupListItem
#End Region '-- Member Class: ClassGroupListItem --
Private Sub Save()
Dim signatureStream As System.IO.Stream = New System.IO.MemoryStream()
Me.picSignature.Image.Save(signatureStream, System.Drawing.Imaging.ImageFormat.Bmp)
'Other code removed
End Sub 'Save()
I have removed the excess code from this class for ease of reading.
My problem: The second line in the Save method executes and jumps to the overridden ToString() function in the member class. There is no reference to the class or an instance of the class. Why would the execution jump to that point? Additionally, an exception is thrown when trying to execute the ToString method.
Any help or ideas would be appreciated.
David Zorn
|
|
|
|
|
I've never seen anything like that. I'd try closing Visual Studio, then reopen your project, recompile it in Debug mode to regenerate the symbols database, then try it again.
|
|
|
|