|
Hey,
I'm trying to save a file with the SaveAs function under the Word.Document... However, if I have a file of the same name, it simply overwrites without giving me a prompt to do so.. Is there any way around this? I checked the list of parameters, and none of them give like, a boolean for 'SaveOverride' or whatever. Thanks.
---
reegan41
|
|
|
|
|
According to the SaveAs help documentation the default behaviour is to overwrite without prompting. What you can do is test to see if the file your are going to save as already exists, and then do something to select another name. In the code example below, if the file "c:\test.doc" exists the while loop will try for "c:\test_1.doc". If that file exists, it will then try for "c:\test_2.doc" and so on.
<br />
Dim I As Long<br />
Dim SaveAsFileName As String<br />
Dim NewSaveAsFileName As String<br />
<br />
SaveAsFileName = "c:\Test.doc"<br />
NewSaveAsFileName = SaveAsFileName<br />
<br />
While Dir(NewSaveAsFileName, vbNormal) <> ""<br />
<br />
I = I + 1<br />
NewSaveAsFileName = Left(SaveAsFileName, Len(SaveAsFileName) - 4) & "_" & i & _<br />
Right(SaveAsFileName, 4)<br />
<br />
Wend<br />
|
|
|
|
|
Hi There,
My Propblem is quite straight forward, but after searching for an answer, I am now more confused than before.
When using reference types as arguments, should the Argument be declared By Value (Byval) or By Refrence (Byref), to obtain the best performance.
ie:
Private Sub DoStuff (ByVal Connection as SQLConnection)
or
Private Sub DoStuff (ByRef Connection as SQLConnection)
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. 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. But Byval only Boxes it once, therefore Byval would be faster.
Can anyone give me some clarity on this?
Thanks in advance
Rob
|
|
|
|
|
Wow - another way that VB totally sucks and shoots you in the foot ?
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi,
This is how I understand it:
SQLConnection is a class, hence a reference type. What gets passed along in
case of a reference type is always an address:
the address of the object when ByVal,
the address of the address of the object when ByRef.
(I am purposely avoiding the word "reference")
There is no data copying, and hence there wont be much performance difference,
there is however a functional difference:
A reference type passed ByVal can be operated on, but it can not be replaced
by the callee in such a way that upon return the caller suddenly holds
another object;
caller will always hold its original object, possibly in an evolved state
(since callee could execute its methods, and access its properties).
A reference type passed ByRef can be operated on (as is the case in ByVal), but
it can also be replaced by callee in such a way that the caller also gets
the new object (or Nothing) upon return of the callee.
Hope this helps
|
|
|
|
|
OK, you've got a bad example. Boxing/Unboxing only applies to Value types being treated as objects. Your example doesn't involve boxing at all.
Reference types are anything that is defined by Class, Interface, Delegates, ... These objects are allocated on the heap and a variable holds a reference, or address, to that instance of the object.
Value types, like Integer, Long, Date, Char, Boolean, Structure, Enum, or anything else that decends from System.ValueType, are allocated on the thread's stack, not on the heap.
Boxing occurs when a Value type is treated as an Object, or reference type. When a value is cast to a reference type, most often as an System.Object, a heap allocation is made for a new Object type and the value data is copied into this object. Now you have two copies of the data, one on the stack as a value type and one on the heap as a reference type.
Unboxing is, of course, the reverse of boxing. The data in the "box" is copied back into the thread's stack where it will be treated as a value again.
For example, when you pass a value type to a method that takes an Object parameter, then use the Object as a value in the method, two copy operations occur. One for boxing the value into an Object and another when the value is unboxed back into a value type. These copy operations are expensive! (Ignore the ByVal pass for now...)
...
Dim i As Integer = 5
' Since the parameter for Method is Object, the
' value 5 will be boxed into an Object.
Method(i)
...
Private Sub Method(ByVal x As Object)
' this line causes an UnBox of the value...
Dim y As Integer = x + 5
End Sub
Now, throw collections into this mix and you've the potential for a serious problem. Notice that all the Collection classes, like ArrayList, take an Object as their Item member data type?? In the ArrayList example, look at the Item property documentation and it's return type, Object. Now imagine storing 10,000 Integers in an ArrayList. You just boxed 10,000 Integers into Object types by doing that. And every time you access one of those boxed Integers, say changing one, you unbox the Integer onto the stack, change the value, then rebox the new value back into an Object on the heap! Yeow!
These rules apply equal to VB.NET AND C# (Christian )
OK, ByRef and ByVal is a completely seperate issue... in the next post..
|
|
|
|
|
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.
|
|
|
|
|