locked
A null reference exception RRS feed

  • Question

  •  
    
     
    
    
    I have tried some solutions 1 error goes another appears 
    
    Variable 'Key' is used before it has been assigned a value. A null reference exception could result at runtime.
    
    Public Function Key() As Object
    
    Const Take1 As String = "zyxwvutsrqponmlkjihgfedcba"
    
    Const Take2 As String = "ZYXWVUTSRQPONMLKJIHGFEDCBA"
    
    Const RoGueTrooPer42 As String = "0987654321"
    
    Dim totalequivalent As String
    
    Dim i As Short
    
    totalequivalent = Take1 & Take2 & RoGueTrooPer42
    
    For i = 1 To CInt("25") 
    
    Key = Key & Mid(totalequivalent, Int((Rnd() * Len(totalequivalent)) + 1), 1)
    
    Next i 
    
    End Function
    

     

    Monday, November 8, 2010 12:43 PM

Answers

  • Usually when used such as this

    Dim Key as Object = nothing

    will remove that warning and that is what it is a warning not an error . Used as you have it though might be a problem . You have constants defined in a function which I believe is bad practice and you use Short which I believe is an integer in .net . Most of the code looks pretty sloppy to me . Rnd maybe should be replace with the random class . Why convert the string "25" to an integer why not just use an integer by removing the quotes ?  Why use object and not string ? Also do not double post .

    http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/c84293c5-71c0-49aa-b12d-1cedcb2cff9f


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Monday, November 8, 2010 1:02 PM

All replies

  • Usually when used such as this

    Dim Key as Object = nothing

    will remove that warning and that is what it is a warning not an error . Used as you have it though might be a problem . You have constants defined in a function which I believe is bad practice and you use Short which I believe is an integer in .net . Most of the code looks pretty sloppy to me . Rnd maybe should be replace with the random class . Why convert the string "25" to an integer why not just use an integer by removing the quotes ?  Why use object and not string ? Also do not double post .

    http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/c84293c5-71c0-49aa-b12d-1cedcb2cff9f


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Monday, November 8, 2010 1:02 PM
  • ty for your reply and let me appologise im new and learning i wont double post again your help is much appreciated
    Monday, November 8, 2010 1:09 PM