quote:
Originally posted by DragonX
What is the $ used for? I've never used it in a replace() before.
Almost every intristic string function in VB (note again: I'm talking about VB, not VB.NET!! It might be different in VB.NET) has two possible ways of use.
1) Firstly (and this is the one mostly used because people don't know any better) most of such functions are able to return a Variant data type.
2) Second, it can directly return a String data type.
Now... A Variant is a special kind of VB data type. It can actually hold all types of data types. In some cases, and used properly at given times, this can be very handy. But mostly, this just slows down your application _and_ is also the cause of many "hidden" errors in applications.
It is much slower because VB constantly needs to check what actual data type the variant is holding and it constantly needs to convert from variant to that data type in calculations, procedures, etc. The variant also uses extra memory (as it needs to hold extra information to identify the actual data type). Furthermore, a variant data type which is holding a number, actually has two data types assigned to it: firstly the number data type (can be integer, long, whatever) and second a string representing that number. Again extra memory and more slowness...
Important: If you have the bad habbit of not declaring variables, VB doesn't know what type you exactly are going to use in your program for that variable and thus reverts back to a variant data type.
The next code is perfect legal in VB:
code:
A = "Hello world"
MsgBox A
A = 1234
MsgBox A
First you assign a string to the variable A, next you assign a number. Since you didn't declare the variable A and thus didn't told VB what data type it is going to be, VB makes it a variant. And thus, that's why that code is valid.... but dead slow...
code:
Dim A As String
A = "Hello world"
MsgBox A
A = 1234
MsgBox A
This will result in an error on the line
'A = 1234' because you explicitly declared variable A as a String. A string can't be a number...
Now that you know what a Variant is and why you should avoid it at all costs, we come to those intristic string functions like Left(), Right(), Mid(), Format(), etc...
As said they have the possebility to return a Variant or directly return a String data type. If you attach $ (the symbol for a String data type) to the function's name, VB knows it should return a String instead of the default Variant data type.
The benefit is that VB doesn't need to convert the Variant (which it returns by default) to a String in calculations, procedures, assignments, etc...
Consider the next code:
code:
Dim A As String
Dim B As String
A = Mid("Hello World", 7, 1)
B = Mid$("Hello World", 7, 1)
VB performs the next things in the A assignment:
1) create the string "Hello World" in memory
2) jump to the 7th character and copy 1 character from that point on
3) create a new variant data type in memory and assign the previous 1 character string to it
4) try to convert that string to a numerical value and assign that also to the variant in memory
5) convert the variant data type to a string data type again
6) assign that new string to the string variable A
VB performs the next things in the B assignment:
1) create the string "Hello World" in memory
2) jump to the 7th character and copy 1 character from that point on
x) create a new variant data type in memory and assign the previous 1 character string to it
x) try to convert that string to a numerical value and assign that also to the variant in memory
x) convert the variant data type to a string data type again
3) assign that new string to the string variable A
This said, I just found out that the string function Replace() will actually always return a string, wether you use $ or not. At least that is what the syntax help tells me. I need to look deeper into this though, as I find this rather curious... So Mike, for now, you were actually wrong
(and me too for saying you were totally correct
)
-----------------------------------------
quote:
Originally posted by DragonX
quote:
Originally posted by CookieRevised
code:
Private Declare Function ShellExecute Lib "SHELL32" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Must i use the API ? Did it that way anyways, for now
In VB
(again, I'm not speaking about VB.NET), yes... VB also knows a Shell() function, but this is a very simplified version of the ShellExecute API and can only run real executables or associations. It can't run protocols like "http://"...
quote:
Originally posted by DragonX
code:
ShellExecute(0&, vbNullString, URLString, vbNullString, vbNullString, vbNormalNoFocus)
What is '0&' for ?
As I explaned in the previous post, when you call the ShellExecute API the first parameter is the handle to the window which will be the parent of the process you're going to start. If you set this parameter the process you're going to start is going to be a child process. In normal use of this API (to start some programs for example), this isn't needed and can even be not wanted. This means you must set this handle parameter to NULL (NULL = not used/nothing/not applicable/no valid data). But VB doesn't know the constant NULL
(at least not for this purpose! Important: Do not use vbNull for this!!!!!!! vbNull is _not_ NULL, it is a constant to indicate a variant with no valid data and will actually return the value 1!!!!). In fact what is NULL? NULL is zero, so we set that parameter to 0. The & at the end is the sign for the Long data type. Again, so VB doesn't need to convert the Variant 0 to the Long data type but directly makes it a Long instead.
quote:
Originally posted by DragonX
Edit2: Alright, after some digging, i found out the KeyPress was redefined in .NET. And to make it work, u need code that looks like this[/code]Well, you asked for VB help, you got VB help not VB.NET help. Those two languages are different, as you just experienced
quote:
Originally posted by DragonX
code:
(...)
ElseIf KeyAscii = 27 Then
End
End If
Ohooooh.... Aiaiai... this is an oh-so-frequent error to make... Although it works, try to avoid crashing against the wall to stop the car!!! In plain English : do not use the End statement! 'End' immediatly stops your application without the decent cleaning up. This can cause serious errors like memory leaks, crashing, freezes and what not, if you don't know exactly what you're doing and you're using 'advanced' stuff as classes and objects... Instead, UNLOAD your main form _after_ you unloaded all your other (child) forms and have cleared all the used objects and have cleared all the used classes... This clearing up can best be done in the Unload event of a form. And to end your application, simply call/execute the unload event by unloading the main form.
As long as you don't use any asyncronical functions like DoEvents(), you actually do not need to use the End statement at all!!! And in most cases DoEvents is also something which can be avoided and is often a sign of a bad programming scheme. In case you do use DoEvents and thus the End statement, you must know exactly what you're doing. Golden rule: avoid DoEvents, End, (and variants) at all costs...
quote:
Originally posted by DragonX
And second, it doesn't the launch my browser. Maybe i just overlooked something
The example of the ShellExecute API is for VB only, not for VB.NET. In VB.NET the syntax to declare API's is much different.
quote:
Originally posted by DragonX
Edit: Woohoo, i got it wo work by bypassing the API too Here's how i did itcode:
System.Diagnostics.Process.Start(URLString)
As you noticed, there are many major differences in VB and VB.NET. VB doesn't know a method to 'execute' an URL, VB.NET does apparently (so indeed, no need for the API there)... (PS: this is indeed the benefit of looking things up by yourself, in the end you'll learn a lot more that way )
PS: but the general tips I gave in my previous post (and in this post about variants vs. strings and about the End statemant) are also valid for VB.NET...