|
-
Jan 17th, 2007, 04:51 PM
#1
Thread Starter
PowerPoster
"As Any" declaration???
In VB6, there apparently is this "As Any" clause being used in declaring API calls. This "As Any" is part of the function signature. Has anyone ever heard of this. I'm only bringing this up because I am in the process of converting VB6 code to .Net. I don't know what the .Net equivalent is.
Thanks,
-
Jan 17th, 2007, 05:18 PM
#2
Re: "As Any" declaration???
hmm Ive never heard of that, but if you declare something as Object then you can set it to any type of object.
-
Jan 17th, 2007, 05:30 PM
#3
Thread Starter
PowerPoster
Re: "As Any" declaration???
Aethiest,
That seemed to work for now. I will know for sure when I start testing!
Thanks,
-
Jan 17th, 2007, 06:13 PM
#4
Re: "As Any" declaration???
As Any can generally be replaced with a general Object type, I have done it before... However, I think that if you know what object it will be returning and put that type into it (whether its some other API structure or whatever) it should work as well... as long as you always know what type it will return for what you are using it for...
-
Jan 18th, 2007, 04:16 AM
#5
Re: "As Any" declaration???
You should never use As Any in VB6 because it it denies the compiler the ability to do strong type checking on your code.
In VB.Net you should create one overloaded declaration for each parameter type you can pass.
-
Jan 18th, 2007, 10:20 AM
#6
Thread Starter
PowerPoster
Re: "As Any" declaration???
I never created the statement. This was a downloaded procedure to read and retrieve INI info.
I don't understand what you mean on your last statement by overloaded declaration statement.
-
Jan 18th, 2007, 10:25 AM
#7
Re: "As Any" declaration???
He means that if your argument can take multiple types then you should declare multiple overloads, one for each type. It's quite possible to declare many API functions with multiple signatures. For instance, where a handle is required you could declare an Integer or an IntPtr.
-
Jan 18th, 2007, 10:31 AM
#8
Thread Starter
PowerPoster
Re: "As Any" declaration???
-
Jan 18th, 2007, 10:41 AM
#9
Re: "As Any" declaration???
For example try writing this in VB:
VB Code:
Dim sr as new StreamReader(
Intellisense will show this:
As you can see, there are 10 overloaded "New" subroutines of the StreamReaders. That means that there are 10 "New" subroutines that does the same thing, with different arguments that can be passed. If you press up and down the arrows you can see the different overloads.
Bah..this explanation was pretty crap, hope you get the point anyways.
-
Jan 18th, 2007, 10:53 AM
#10
Re: "As Any" declaration???
If you post up the VB6 declaration you want to convert we'll give you an example using that declaration...
-
Jan 18th, 2007, 11:27 AM
#11
Thread Starter
PowerPoster
Re: "As Any" declaration???
Here is the API call that contains the "As Any"
VB Code:
Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As any, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
-
Jan 18th, 2007, 11:29 AM
#12
Re: "As Any" declaration???
When you call that API what do you pass as arguments on the "as any" arguments?
-
Jan 18th, 2007, 11:40 AM
#13
Thread Starter
PowerPoster
Re: "As Any" declaration???
That call came from a generic module that opened and retrieved INI values. The program that used that module never made a call to it.
-
Jan 18th, 2007, 01:11 PM
#14
Re: "As Any" declaration???
Looks like all should just be strings, and the Long changed to Int32... like below:
VB Code:
Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Int32
In the code sample in this MSDN Link, they were passing in strings...
-
Jan 18th, 2007, 01:29 PM
#15
Re: "As Any" declaration???
According to the MSDN[ documentation there are two ways of using this function.
One uses string parameters the other wants NULL.
To cater for this in VB.Net you write two overloaded declarations thus:-
VB Code:
Private Overloads Declare Function WritePrivateProfileString Lib "KERNEL32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Int32
Private Overloads Declare Function WritePrivateProfileString Lib "KERNEL32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As IntPtr, _
ByVal lpKeyName As IntPtr, _
ByVal lpString As IntPtr, _
ByVal lpFileName As String) As Int32
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|