VS 2008 Returning value from Function-VBForums
Results 1 to 6 of 6

Thread: Returning value from Function

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,827

    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:
    1. Public Function UserAccounts() As String
    2.         Dim userskey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
    3.         For Each keyname As String In userskey.GetSubKeyNames()
    4.             Using key As RegistryKey = userskey.OpenSubKey(keyname)
    5.                 UserAccounts = DirectCast(key.GetValue("ProfileImagePath"), String)
    6.             End Using
    7.         Next
    8.     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:
    1. For Each userPath As String In UserAccounts()
    2.      If System.IO.Directory.Exists(userPath) Then
    3.            ListBox1.Items.Add(userPath)
    4.      End If
    5. 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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,656

    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?

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,827

    Re: Returning value from Function

    Oh. Right. Well, that makes sense. So, I modified the code to this:

    VB.NET Code:
    1. Dim userskey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
    2.         For Each keyname As String In userskey.GetSubKeyNames()
    3.             Using key As RegistryKey = userskey.OpenSubKey(keyname)
    4.                 Dim userPath As String = DirectCast(key.GetValue("ProfileImagePath"), String)
    5.                 Dim UserAccounts As String = System.IO.Path.GetFullPath(userPath)
    6.                 ListBox1.Items.Add(UserAccounts)
    7.             End Using
    8.         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:
    1. Public Function UserAccounts() As String
    2.         Dim userskey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
    3.         For Each keyname As String In userskey.GetSubKeyNames()
    4.             Using key As RegistryKey = userskey.OpenSubKey(keyname)
    5.                 Dim userPath As String = DirectCast(key.GetValue("ProfileImagePath"), String)
    6.                 UserAccounts = System.IO.Path.GetFullPath(userPath)
    7.             End Using
    8.         Next
    9.     End Function

    VB.NET Code:
    1. For Each _User As String In UserAccounts()
    2.             ListBox1.Items.Add(_User)
    3.         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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,656

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,827

    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Returning value from Function

    Your function is doing the equivalent of this:

    vb.net Code:
    1. For Each character As String In "Some String"
    2.      'looping through each character
    3. Next

    What it should be doing, as pointed out by John, is:

    vb.net Code:
    1. For Each user As String In aCollectionOfStringsRepresentingUsers
    2.      'loop through a collection of strings, not characters
    3.      'aCollectionOfStringsRepresentingUsers = the return value of your function
    4. 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:
    1. Public Function DoesNotReturnAValueOnAllPaths(ByVal obj As Object) As String
    2.     If obj IsNot Nothing Then
    3.         Return obj.ToString()
    4.     End If
    5.     'The code has two paths, how will the code execute if 'obj' is nothing
    6.     'and what value is returned on the second path? By default it is nothing.
    7.     'This is what the IDE is telling you will happen if the second path gets run.
    8. 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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.