Question Beginner help with converting VBA

bar891

New member
Joined
Nov 19, 2016
Messages
2
Programming Experience
Beginner
Can some point me in the right direction in converting VBA Module into C#. If been working on this all weekend with no luck.

Public Function fRandomPword(intStrLen As Integer, booUpperCase As Boolean)
Dim sngStopNum As Single
Dim sngStartNum As Single
Dim intLooper As Integer
Dim strNewPword As String
Dim intRndAscii As Integer


If Nz(intStrLen, 0) = 0 Then GoTo JumpOut:
'97 - 122 = lower case alphabet
'64 - 90 = upper case alphabet, change as needed
'determine if use upper or lower case
If booUpperCase = True Then
sngStopNum = 90
sngStartNum = 64
Else
sngStopNum = 122
sngStartNum = 97
End If


For intLooper = 1 To intStrLen


TryAgain:
Randomize
intRndAscii = (sngStopNum * Rnd) + 1
If (intRndAscii < (sngStartNum + 1)) Or (intRndAscii > sngStopNum) Then
GoTo TryAgain
Else
strNewPword = strNewPword & Chr(intRndAscii)
End If


Next intLooper


fRandomPword = strNewPword


JumpOut:
End Function
 
The first step is to not try to translate the code. The second step is to analyse what the existing code does. It's a good idea to write that out in plain English (or your choice of language). You can then proceed to create C# code that implements that functionality as though the VBA code never existed. There's every chance that the best C# code to do what you want to do is quite different to that VBA code but if you try to translate directly from VBA to C# then you'll miss that.
 
i gave up translating very early in the piece. i have been trying to rewrite it in C# but fail to understand it all (i understand what the code is doing) but don't understand the equivalent in C#
 
i gave up translating very early in the piece. i have been trying to rewrite it in C# but fail to understand it all (i understand what the code is doing) but don't understand the equivalent in C#

If that's the case then why have you even shown us the VBA code? It's completely irrelevant. You say that you know what you're trying to achieve and that you have started writing C# code. That's what's relevant. Tell us what you're trying to achieve, show us how you're trying to achieve it and explain exactly what your issue is. That's what's relevant.

As with all programming problems, break it down into the smallest steps you can and then look at implementing each step in isolation. It may be that you end up with ten steps and you can implement seven of them for yourself. You then only need to ask us about the three steps that you're specifically having issues with. If you attack a problem in this manner then you don't have to implement the steps in the order they must be performed. You can implement the last step first if that's the easiest to do, because all you have to know is what inputs you need, not how to get them. There's more to software development than just writing code.
 
Back
Top Bottom