Results 1 to 19 of 19

Thread: [RESOLVED] Problems having a function return two values

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] Problems having a function return two values

    Hello, I'm having trouble with a function returning two values. Each value contains a string, how can I return these values and have each one of them be stored in one independent variable.

    How can I do this? This is my code:

    vb.net Code:
    1. Public Shared Function SHA256StringHash(ByVal text As String, Optional ByVal salt As Boolean = False) As String
    2.             Try
    3.                 Select Case salt
    4.                     Case True
    5.                         Dim textBytes As Byte() = _encoding.GetBytes(text)
    6.                         Dim myrandom As New Random
    7.                         Dim rng As New RNGCryptoServiceProvider
    8.                         Dim randomSalt As Byte() = New Byte(myrandom.Next(4, 8)) {}
    9.                         rng.GetNonZeroBytes(randomSalt)
    10.                         Dim SaltedText_Byte As Byte() = New Byte((textBytes.Length + randomSalt.Length) - 1) {}
    11.                         SaltedText_Byte = textBytes.Concat(randomSalt).ToArray
    12.                         Dim SaltedHash As Byte() = hash.ComputeHash(SaltedText_Byte)
    13.                         Return Convert.ToBase64String(SaltedHash) _
    14.                         AndAlso Convert.ToBase64String(randomSalt)
    15.                         Return Nothing
    16.                     Case Else
    17.                         Dim textBytes As Byte() = _encoding.GetBytes(text)
    18.                         Dim textHash As Byte() = hash.ComputeHash(textBytes)
    19.                         Return Convert.ToBase64String(textHash)
    20.                 End Select
    21.             Catch ex As Exception
    22.                 Return ex.Message
    23.             End Try
    24.         End Function

    I'm trying to return the hashed text with the salt and the generated salt, how can I do this?

    Thanks in advance!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Problems having a function return two values

    In general to return more than one value from a function you can either pass the variables to the function and declare them ByRef rather than ByVal....
    OR...
    create a structure that contains both values and return the structure.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Problems having a function return two values

    In this case I think I can't use ByRef because I'm only passing 1 variable to the function and I'm returning 2. If I were to go with option 2, how would I "get them"?

    I have never used structures before, that's why I'm asking...
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4
    New Member
    Join Date
    Jul 2007
    Posts
    6

    Re: Problems having a function return two values

    Hi tassa,

    kebo is correct, you can either use structure or, you can use class for returing the values.

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Problems having a function return two values

    in most cases you can do it byref. Byref means you are passing a reference to the variable to the function... not simply the value of the variable. If the variable changes in the function, it also changes outside the function. If you do use byref, there is also no need for a function.. you can use a sub instead...

    Code:
        Dim var1
        Dim var2
    
        call mysub(var1,var2)
    
        'now the returned vars are in var1 and var2
    
        Private Sub mysub(ByVal var1, ByVal var2)
    
            'do stuff
            'assign the first return variable to var1
            'assign the second to var2
        End Sub
    using a structure you would use something like this....

    Code:
      Private Structure MyDataStructure
            Public var1 As Integer
            Public var2 As Integer
        End Structure
    
        Dim myvars As MyDataStructure = MyFunction()
        var1 = myvars.var1
        var2 = myvars.var2
    
        Private Function MyFunction() As MyDataStructure
    
            'do some stuff
            'to create the to things that need to be returned...
            Dim ReturnStruct As MyDataStructure
            ReturnStruct.var1 = 'the first variable to be returned
            ReturnStruct.var2 = 'the second variable to be returned
    
            Return ReturnStruct
    
        End Function
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Problems having a function return two values

    Thanks! I've got it!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Problems having a function return two values

    First up, that method has a serious, SERIOUS problem. There's absolutely no way you should be returning an error message. If an exception is thrown in that method, how is the caller supposed to know it? As it is the caller would simply use the error message as though it was a valid result. Very, VERY bad! That method shouldn't be handling any exceptions at all. If an exception is thrown it should be up to the caller of that method to handle it and then it knows that an error occurred and it can decide what to do about it.

    Secondly, why would you use a Select Case statement to test a Boolean value? The point of Select Case is that it can test multiple cases. If there are only two cases then you should just be using an If...Else statement.

    As for the question, you should really be overloading that method and have one with two parameters and the other with one:
    vb.net Code:
    1. Public Shared Function SHA256StringHash(ByVal text As String, ByRef salt As String) As String
    2.     '...
    3. End Function
    4.  
    5. Public Shared Function SHA256StringHash(ByVal text As String) As String
    6.     '...
    7. End Function
    In one you generate a salt and assign it to the parameter and in the other you don't. If there's common code then either one overload can call the other or else you can define a third method that does the common work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [RESOLVED] Problems having a function return two values

    OK, I fixed the first point and the second point. I don't understand why I should be using two statements if I can do it in one.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Problems having a function return two values

    Quote Originally Posted by tassa View Post
    OK, I fixed the first point and the second point. I don't understand why I should be using two statements if I can do it in one.
    There's no rule that says one method is better than two. Clarity is important and in this case two methods is clearer than one. As far as the caller is concerned it makes no difference. As it stands you've got an optional parameter so the user can call the method with one argument or two. If they pass one then no salt is generated and if they pass two it is. The same goes for my code. The caller would use the same method name with one argument or two.

    Your code simply won't pass back the generated salt as is. In order to do so you'd have to add a third parameter that would pass the salt back ByRef, but that would make no sense if the user specified False for generating salt so the code becomes nonsensical. Two methods is clearer and cleaner so you should use two methods, or three if appropriate.

    Never let writing fewer methods be something you strive for. You write as many methods as are appropriate for the circumstances.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [RESOLVED] Problems having a function return two values

    It seems logical to write down two or more methods, however, in this case, I don't think it'll help me, since I'm not generating a salt value from a given string, I'm generating a random salt value (depending if the boolean value = true) and then returning the generated salt.

    Nevertheless, I'm going to explore what you've recommended and see what happens.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Problems having a function return two values

    Quote Originally Posted by tassa View Post
    It seems logical to write down two or more methods, however, in this case, I don't think it'll help me, since I'm not generating a salt value from a given string, I'm generating a random salt value (depending if the boolean value = true) and then returning the generated salt.

    Nevertheless, I'm going to explore what you've recommended and see what happens.
    Of course it will help. With two methods the user either passes a salt argument by reference or they don't. If they do then a value is generated and passed back:
    vb.net Code:
    1. Dim salt As Byte()
    2. Dim hash As String = SHA256StringHash(someText, salt)
    After that you've got the hash and the salt. If you don't want salt then you don't pass the second parameter:
    vb.net Code:
    1. Dim hash As String = SHA256StringHash(someText)
    No salt argument is passed so no salt is generated and no salt is passed back. In this case it's the presence of the second argument, not its value, that decides whether salt is generated and used or not. Converting your original code would yield:
    vb.net Code:
    1. Public Overloads Shared Function SHA256StringHash(ByVal text As String, ByRef salt As Byte()) As String
    2.     Dim textBytes As Byte() = _encoding.GetBytes(text)
    3.     Dim myrandom As New Random
    4.     Dim rng As New RNGCryptoServiceProvider
    5.  
    6.     salt = New Byte(myrandom.Next(4, 8)) {}
    7.     rng.GetNonZeroBytes(salt)
    8.  
    9.     Dim SaltedText_Byte As Byte() = New Byte((textBytes.Length + salt.Length) - 1) {}
    10.  
    11.     SaltedText_Byte = textBytes.Concat(salt).ToArray
    12.  
    13.     Dim SaltedHash As Byte() = hash.ComputeHash(SaltedText_Byte)
    14.  
    15.     Return Convert.ToBase64String(SaltedHash)
    16. End Function
    17.  
    18. Public Overloads Shared Function SHA256StringHash(ByVal text As String) As String
    19.     Dim textBytes As Byte() = _encoding.GetBytes(text)
    20.     Dim textHash As Byte() = hash.ComputeHash(textBytes)
    21.  
    22.     Return Convert.ToBase64String(textHash)
    23. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Problems having a function return two values

    Here's the three method version, although I haven't tested it:
    vb.net Code:
    1. Public Overloads Shared Function SHA256StringHash(ByVal text As String, ByRef salt As Byte()) As String
    2.     Dim textBytes As Byte() = _encoding.GetBytes(text)
    3.     Dim myrandom As New Random
    4.     Dim rng As New RNGCryptoServiceProvider
    5.  
    6.     salt = New Byte(myrandom.Next(4, 8)) {}
    7.     rng.GetNonZeroBytes(salt)
    8.  
    9.     Dim SaltedText_Byte As Byte() = New Byte((textBytes.Length + salt.Length) - 1) {}
    10.  
    11.     SaltedText_Byte = textBytes.Concat(salt).ToArray
    12.  
    13.     Return SHA256StringHash(SaltedText_Byte)
    14. End Function
    15.  
    16. Public Overloads Shared Function SHA256StringHash(ByVal text As String) As String
    17.     Dim textBytes As Byte() = _encoding.GetBytes(text)
    18.  
    19.     Return SHA256StringHash(textBytes)
    20. End Function
    21.  
    22. Public Overloads Shared Function SHA256StringHash(ByVal data As Byte()) As String
    23.     Dim textHash As Byte() = hash.ComputeHash(data)
    24.  
    25.     Return Convert.ToBase64String(textHash)
    26. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [RESOLVED] Problems having a function return two values

    WOW! It is clearer! Hadn't you showed me that modification, I wouldn't have been able to come up with something like that. That's something I'll keep in mind from now on!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [RESOLVED] Problems having a function return two values

    Why do you return like this:

    Code:
    Return SHA256StringHash(textBytes)
    In the three method version?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Problems having a function return two values

    Quote Originally Posted by tassa View Post
    Why do you return like this:

    Code:
    Return SHA256StringHash(textBytes)
    In the three method version?
    The first and second overloads both call the third overload to actually create the hash. The first two methods get the Bytes to be hashed and then the common functionality, i.e. hashing those Bytes, is performed in the third overload. Each of the first two overloads calls the third, pass it the data and returns the result.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [RESOLVED] Problems having a function return two values

    Ok, cool. Wow, thanks for your help! Really, really helpful !
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [RESOLVED] Problems having a function return two values

    How would I return the salt? Should I create a structure and return the whole structure? I'm eager wanting to return the salt since I'm creating a class and I want to be able to get the generated salt and be able to save it or whatever.

    EDIT:

    Disregard my question. It was a dumb one, I blame my lack of sleep. Since it's ByRef the orignal value is "edited". So there's my answer .

    EDIT 2:

    In case anyone wants to know more about Overloads, it helped me understand what it is and does.
    Last edited by tassa; Aug 19th, 2009 at 02:23 AM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  18. #18
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Problems having a function return two values

    Just in case you ever want to use a structure for this (I like using structures better), it would look like this. The function would return an instance of your structure, instead of a string directly.

    The following function returns a structure with a Name, LastName and Age property; effectively returning three values:
    vb.net Code:
    1. Public Structure Person
    2.    Public Name As String
    3.    Public LastName As String
    4.    Public Age As Integer
    5. End Structure
    6.  
    7. Public Function GetPerson() As Person
    8.    Dim p As New Person With {.Name = "Nick", _
    9.                  .LastName = "Thissen", _
    10.                  .Age = 20}
    11.    Return p
    12. End Function
    13.  
    14. Private Sub SomeMethod()
    15.    'This is the method you call the function in
    16.    Dim p As Person = GetPerson()
    17.    MessageBox.Show(String.Format("{0} {1}, {2}", _
    18.                  p.Name, _
    19.                  p.LastName, _
    20.                  p.Age))
    21. End Sub

    As you may see, structures behave more or less the same as classes (except, they are value types unlike classes)

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Problems having a function return two values

    Quote Originally Posted by NickThissen View Post
    Just in case you ever want to use a structure for this (I like using structures better), it would look like this. The function would return an instance of your structure, instead of a string directly.

    The following function returns a structure with a Name, LastName and Age property; effectively returning three values:
    vb.net Code:
    1. Public Structure Person
    2.    Public Name As String
    3.    Public LastName As String
    4.    Public Age As Integer
    5. End Structure
    6.  
    7. Public Function GetPerson() As Person
    8.    Dim p As New Person With {.Name = "Nick", _
    9.                  .LastName = "Thissen", _
    10.                  .Age = 20}
    11.    Return p
    12. End Function
    13.  
    14. Private Sub SomeMethod()
    15.    'This is the method you call the function in
    16.    Dim p As Person = GetPerson()
    17.    MessageBox.Show(String.Format("{0} {1}, {2}", _
    18.                  p.Name, _
    19.                  p.LastName, _
    20.                  p.Age))
    21. End Sub

    As you may see, structures behave more or less the same as classes (except, they are value types unlike classes)
    I'm not a big fan of defining a type solely for the purpose of being returned by one method. If it has other uses then fair enough but I'd tend not to go with it here. Also, by using a ByRef parameter you allow the return type of the function to remain the same whether salt is added or not. You certainly could create a new type but I don't see it as the best option in this case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width