I need help converting the following JS-function to vb.net please:
function testjs(s) {
var dict = {};
var data = (s "").split("");
var currChar = data[0];
var oldPhrase = currChar;
var out = [currChar];
var code = 256;
var phrase;
for (var i=1; i<data.length; i ) {
var currCode = data[i].charCodeAt(0);
if (currCode < 256) {
phrase = data[i];
}
else {
phrase = dict[currCode] ? dict[currCode] : (oldPhrase currChar);
}
out.push(phrase);
currChar = phrase.charAt(0);
dict[code] = oldPhrase currChar;
code ;
oldPhrase = phrase;
}
return out.join("");
}
What my code looks like at the moment:
Private Function questionmarkop(ByVal ph As String, ByVal dictatcurr As String, ByVal ophoc As String) As String
Return If(ph = dictatcurr, dictatcurr, ophoc)
End Function
Private Function testvb(ByVal s As String) As String
Dim dict As New Dictionary(Of Integer, String)
Dim data As Char() = s.ToCharArray
Dim currchar As Char = data(0)
Dim oldphrase As String = currchar
Dim out As String() = {currchar}
Dim code As Integer = 256
Dim phrase As String = ""
Dim ret As String = ""
For i As Integer = 1 To data.Length - 1
Dim currcode As Integer = Convert.ToInt16(data(i))
If currcode < 256 Then
phrase = data(i)
Else
phrase = questionmarkop(phrase, dict(currcode), (oldphrase currchar))
End If
ReDim Preserve out(out.Length)
out(out.Length - 1) = phrase
currchar = phrase(0)
dict.Item(code) = oldphrase currchar
code = 1
oldphrase = phrase
Next
For Each str As String In out
ret = ret str
Next
Return ret
End Function
When inputting the following string s: thisĂaveryshorttestđringtogiĆanexamplefČđackoĆrflow
The function should return: thisisaveryshortteststringtogiveanexampleforstackoverflow
The js function does exactly that, my vb function does not sadly.
The first time (or basically everytime) the if statement is not true, the next character will be wrong. So i figure there is something wrong with the line phrase = questionmarkop(phrase, dict(currcode), (oldphrase currchar))
.
With the test string i provided everything works until this
, after that i have the first false char.
Can someone help me figure out what i am doing wrong here?
CodePudding user response:
Based on discussion in the comments, it seems like the issue was producing a VB translation for this line:
phrase = dict[currCode] ? dict[currCode] : (oldPhrase currChar);
I have only passing familiarity with Javascript, but I believe the key here is that dict[currCode]
will return a type of null or missing value if there is not already an entry in the dictionary with key currCode
. The .NET dictionary has facilities that will let you get the same effect, though the exact implementation is a little different.
The direct VB equivalent to this ternary and assignment is (I believe):
phrase = If(dict.ContainsKey(currCode), dict(currCode), oldPhrase & currChar)
You can eliminate a key lookup on the dictionary by using Dictionary.TryGetValue
:
If Not dict.TryGetValue(currCode, phrase) Then
phrase = oldPhrase & currChar
End If
I doubt the code is performance-sensitive enough to care about the difference, so I would recommend to use the alternative that you find more clear to read.