0

I declared a string s like this

Dim s As String=""

And I am trying to build a keylogger that e-mails String "s" by concatenating keystrokes. A sample code snippet is as follows

Dim hotkey5 As Boolean
        hotkey5 = GetAsyncKeyState(Keys.E)
        If hotkey5 = True Then
            String.Concat(s, "E")
        End If

But nothing is getting concatenated to String s. Why is that so?

Doug Glancy
  • 27,214
  • 6
  • 67
  • 115
Sparrow
  • 195
  • 2
  • 5
  • 15

2 Answers2

2

In .net Strings are immutable. So you need a new string to hold the results of the the concatenation - which is the concatenated string.

    Dim hotkey5 As Boolean
    hotkey5 = GetAsyncKeyState(Keys.E)
    If hotkey5 = True Then
        s= String.Concat(s, "E")
    End If
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
-1
Dim hotkey5 As Boolean
hotkey5 = GetAsyncKeyState(Keys.E)
If hotkey5 = True Then
    s+="E"
End If

Synopsis

VB.Net provides an ability to add or concatenate two strings using + operator. It also can be used many way. VB.Net provides an ability to add or concatenate two strings using + operator. It also can be used many way. Let Dim Valu1 as String=”1” and Dim Valu2 as String=”2”. Then Dim concatenate = Valu1+Valu2. So the value will be ”12”. Or we can, Dim concatenate = Valu2+Valu1. Then the value will be ”21”. So it is pretty simple as that

  • This way will duplicate the content of the string – SysDragon Apr 16 '14 at 07:19
  • Aaaand... this will not compile. In VB.NET the concatenator operator for strings is `&`, not `+`. I think you wanted to write this: `s &= "E"` – SysDragon Apr 16 '14 at 07:21
  • Pal SysDragon, the operator `&` and `+` is **almost similar in VB.Net not in C#**. So that it can concatenate the string but **`-` operator does not work with strings**. – user3539722 Apr 16 '14 at 14:02
  • 1
    Also GetAsyncKeyState will return a short, not a boolean. – Dominic B. Apr 16 '14 at 14:09
  • Dominic Trade, me job to show that concatenate can be made using `+` operator not what ` GetAsyncKeyState ` function returns – user3539722 Apr 16 '14 at 14:24
  • Then it will compile and you can do it, but you [should not](http://www.lavanguardia.com/ciencia/20140416/54405869699/cientificos-barco-vela-titan-estudiar-mares.html) use `+` for concatenation, as it can be confused with addition operation. And, as @DominicTrade said, you're performing and implicit conversion with `GetAsyncKeyState` to `Boolean` – SysDragon Apr 16 '14 at 14:27
  • SysDragon, don't you think a centralized form is very much easier to remember?? If yes, then prefer `+` to addition of String and Integer both purpose. If no, all depends on you. But the way is not wrong as the modern system is developed to simplifies the process of writing codes. – user3539722 Apr 16 '14 at 14:31
  • It is a debatible issue: [&vs+for-concatenating-strings-in-vbnet](http://stackoverflow.com/questions/3006153/ampersand-vs-plus-for-concatenating-strings-in-vb-net). But it's safer and recommended to use `&` instead – SysDragon Apr 16 '14 at 14:32
  • 1
    I just said that this is a wrong conversion. Get Option Strict On! – Dominic B. Apr 16 '14 at 14:33
  • Both **String+String can be concatenate** using that but **String+Integer is not supported** – user3539722 Apr 16 '14 at 14:35