-
Jan 24th, 2010, 08:47 PM
#1
Returning value from Function
I'm attempting to create a function that will get the User Accounts from the registry. I then need to loop through those users and commit specific actions on each one.
Here's my function:
VB .NET Code:
Public Function UserAccounts() As String
Dim userskey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
For Each keyname As String In userskey.GetSubKeyNames()
Using key As RegistryKey = userskey.OpenSubKey(keyname)
UserAccounts = DirectCast(key.GetValue("ProfileImagePath"), String)
End Using
Next
End Function
I've been using a ListBox for testing purposes and when I call .Items.Add with my function it adds the Users.
I may be wrong, but I thought I need to loop through those items in order to commit actions on each account.
vb.net Code:
For Each userPath As String In UserAccounts()
If System.IO.Directory.Exists(userPath) Then
ListBox1.Items.Add(userPath)
End If
Next
When I use this, I get backward slash marks instead of the Users.
Any information is appreciated.
Thanks
CodeBank contributions: Process Manager, Temp File Cleaner
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Jan 24th, 2010, 08:55 PM
#2
Re: Returning value from Function
Let's say that I get a dog and I name him Fido:
Code:
myDog.Name = "Fido"
I then name him "Rover":
Code:
myDog.Name = "Rover"
Finally, I name him Spot:
Code:
myDog.Name = "Spot"
After that, what will be the dog's name? It's just going to be Spot, right? There's no sign of Fido or Rover, because they were simply replaced by the new name each time.
Now look at your code for UserAccounts. Do you notice any similarity? How can you get ALL the accounts when you've only got one String and you're replacing its value each iteration of the loop?
-
Jan 25th, 2010, 12:54 AM
#3
Re: Returning value from Function
Oh. Right. Well, that makes sense. So, I modified the code to this:
VB.NET Code:
Dim userskey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
For Each keyname As String In userskey.GetSubKeyNames()
Using key As RegistryKey = userskey.OpenSubKey(keyname)
Dim userPath As String = DirectCast(key.GetValue("ProfileImagePath"), String)
Dim UserAccounts As String = System.IO.Path.GetFullPath(userPath)
ListBox1.Items.Add(UserAccounts)
End Using
Next
If I use that code, I get all the Users in my ListBox. But, then I tried the similar thing with the function and loop:
VB.NET Code:
Public Function UserAccounts() As String
Dim userskey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
For Each keyname As String In userskey.GetSubKeyNames()
Using key As RegistryKey = userskey.OpenSubKey(keyname)
Dim userPath As String = DirectCast(key.GetValue("ProfileImagePath"), String)
UserAccounts = System.IO.Path.GetFullPath(userPath)
End Using
Next
End Function
VB.NET Code:
For Each _User As String In UserAccounts()
ListBox1.Items.Add(_User)
Next
Now, I get the only the last User and it adds each character as a new item.
Could be the fact that I'm not returning a value from within the Function? I get the green squiggly line under "End Function"
Function 'UserAccounts' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
But, I'm not getting a null exception, so maybe it's OK for now.
I tried returning "UserAccounts" thinking that would be okay, since that's the value I'm going for, but it gives me the "used before assigned a value" line
CodeBank contributions: Process Manager, Temp File Cleaner
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Jan 25th, 2010, 01:00 AM
#4
Re: Returning value from Function
Your code really hasn't changed. You're still simply replacing a single String each iteration of the loop . Think about what it is that you're trying to return from that function. It's multiple objects, right? So how do you think that's going to happen when the function returns a single String? How do you normally store multiple objects? In an array or collection, right? So if your function is supposed to return multiple objects, logic would dictate that it would have to return an array or a collection, not a String.
Also, assigning to the method name to return a value is holdover from VB6 for compatibility. In VB.NET you should ALWAYS be using Return statements to explicitly return a value.
-
Jan 28th, 2010, 12:32 AM
#5
Re: Returning value from Function
The reason I thought my revision would work was because it was returning all of the Users. The first one was only returning the last User in the list.
I originally attempted to create an array with that, but got an error. I'm just going to create a new variable for the array and add it to the array and see what I get.
CodeBank contributions: Process Manager, Temp File Cleaner
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Jan 28th, 2010, 01:42 AM
#6
Re: Returning value from Function
Your function is doing the equivalent of this:
vb.net Code:
For Each character As String In "Some String" 'looping through each character Next
What it should be doing, as pointed out by John, is:
vb.net Code:
For Each user As String In aCollectionOfStringsRepresentingUsers 'loop through a collection of strings, not characters 'aCollectionOfStringsRepresentingUsers = the return value of your function Next
Does that make sense?
Also, in reference to the warning you are receiving. The IDE is telling you that it is possible for the code to execute a path that does not specify a return value. Simple example:
vb.net Code:
Public Function DoesNotReturnAValueOnAllPaths(ByVal obj As Object) As String If obj IsNot Nothing Then Return obj.ToString() End If 'The code has two paths, how will the code execute if 'obj' is nothing 'and what value is returned on the second path? By default it is nothing. 'This is what the IDE is telling you will happen if the second path gets run. End Function
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
|