Blooze Amazon
Blooze Amazon

Reputation: 13

How to add random numbers between two sets of text vb.net

Okay so I have been trying to make a junk code generator for C++ (new to vb.net just trying to get somewhat familiar with different languages) and I am stuck at being able to call random numbers between two lines of text currently I am stuck at

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles 
Button4.Click
Dim value As Integer = CInt(Int((10000 * Rnd()) + 9999999))
Me.RichTextBox1.Text = (
" float pJunkcode = " + value + ";" +
" if (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
"If(pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" If (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" If (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" If (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" If (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";")`

The error I am getting is

Conversion from string "pJunkcode" to type 'Double' is not valid.

Upvotes: 1

Views: 69

Answers (1)

Pikoh
Pikoh

Reputation: 7703

The problem with your code is hard to notice when you come from C-languages. In VB the + operator has different behaviours depending on the types of the expresions involved. In this case, one is a string and the other is a number, so the + operator is trying to first parse the String to a Double, to finally add the 2 numbers. You've got all this information here: + Operator

One expression is a numeric data type and the other is a string:

If Option Strict is On, then generate a compiler error.

If Option Strict is Off, then implicitly convert the String to Double and add.

If the String cannot be converted to Double, then throw an InvalidCastException exception.

In your case, you can solve the problem by using the string concatenation operator & Operator:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles 
 Button4.Click
   Dim value As Integer = CInt(Int((10000 * Rnd()) + 9999999))
    Me.RichTextBox1.Text = (
    " float pJunkcode = " & value & ";" &
    " if (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    "If(pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " If (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " If (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " If (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " If (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";")

Upvotes: 3

Related Questions