Dim InputText As String = Me.txtenter.Text
        Dim Letters = From c As Char In InputText.ToCharArray Select c & " = " & AscW(c) & " "
        Me.lblanswer.Text = String.Join("", Letters.ToArray)

The above code is to convert each letter in a word or phrase (including the space) to its own binary code (For example: the letter "A"'s binary code is 65).

I tested this code at home, and it works perfectly well. But when I come to school, I wrote the exact same thing, but it does not work. Here is what it said:

Dim Letters = From c As Char In InputText.ToCharArray Select c & " = " & AscW(c) & " "

---> "End of statement expected" (I raelly don't know what end statement it needs)

Then, the second thing in error is:

Me.lblanswer.Text = String.Join("", Letters.ToArray)

---> the word "Letters" is underlined and it said: Letters is not declared yet.

Howcome this code works well at my PC but in school it reports those 2 errors?

If there is something that needs to be fixed, can you guys show me what is is and the way to fix it?

Thank you for your time!

Recommended Answers

All 10 Replies

You have to target version 3.5 .net framework to use linq.

Please verify the .net version at your school. Actually you can't use Linq in .NET 2.0. However you can download linqbridge - which provides a LINQ implementation for Framework 2.0.

To add to what adatapost said, if you're using Visual Studio 2008 / VB 2008 Express at home, you'll have .NET 3.5. If the school is running Visual Studio 2005, it will be 2.0.

If that's the case, go back through your threads and look at my suggestions and see if you can adapt them to your needs, as I don't believe there's anything that I offered that wouldn't be available in .NET 2.0.

Edit: I've put together something here that is similar to the program you have, except compatible for 2.0, and is only 7 lines of code. More than the LINQ version, but not all that difficult. I think you'll be able to arrive at a similar destination. Think of a For Each loop and a StringBuilder object (found in the System.Text namespace).

Dim original As String = Me.txtenter.Text
        Dim temp As Char() = original.ToCharArray()
        Dim strstore As String
        strstore = AscW(temp).ToString
        Dim binarycode As String = New String(strstore)
        Me.lblanswer.Text = binarycode

I run the above code, and the label displays only the binary code for the first letter of the word I enter.

How can I make it display the binary code of the remaining letters in the word that I enter.

That's where a For Each would come in.

For Each c as Char in temp
    'do whatever you want with each character
    'I would suggest use a StringBuilder object
    'Append the character, =, and the integer value
Next

'Put the StringBuilder output in your label
Dim mystring As String = Me.txtenter.Text
        Dim chars As Char() = mystring.ToCharArray()

        For Each c As Char In chars
            Dim strstore As String
            strstore = AscW(c)
            ........stringBuilder?????
        Next c

        Me.lblanswer.Text = ????????

I get to the part AscW(c), hope it's right so far.

Then I dont know what you mean by StringBuilder. Sincerely I have never heard about this before (maybe it exists in some of my threads, but I don't realize it).

May you explain to me a little bit more about the "StringBuilder" thing, please?

StringBuilder is a class in the System.Text namespace. Reference that namespace in your code with an Imports statement, or alternately just use the fully qualified class name (meaning: use the namespace) in the declaration.

Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder() 'fully qualified declaration

Declare this outside of your loop. In your loop, you want to use the .Append(value) method. What are you appending? You really already have the code, just look at your LINQ version. The whole bit about c & " = " & ..., etc.

When you get back out of the loop, you can say something like

yourlabel.Text = yourStringBuilder.ToString()

The reason you'd want to use StringBuilder as opposed to declaring a string and then concatenating to it with each loop pass is the way .NET treats strings behind the scenes. When you say Dim myString as String = "new string" , you're creating an object in memory for that string. When you come back and say myString &= " and then some" , you're not changing the existing object in memory, but rather are creating yet another one! It's inefficient and, depending on what you are doing, costly in terms of memory to keep altering a string. StringBuilders have no such issues (*to an extent), and are ideal for scenarios where you will constantly be adding to what you will eventually want to use as a simple string.

Dim mystring As String = Me.txtenter.Text
        Dim chars As Char() = mystring.ToCharArray()

        For Each c As Char In chars
            Dim strstore As String
            strstore = AscW(c)
            Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder()
            newString.Append(c & " = " & strstore)
            Me.lblanswer.Text = newString.ToString()
        Next c

I declare the StringBuilder outside of the loop, just like you said. But in the loop, when I write "newString.Appent(...) ---> it said that "newString is not declared yet". Thus, I have to put the StringBuilder inside my Loop.

When I teset it, it only displays the binary code for the first letter of the word I enter. (I enter "ABC" and it only displays: "A = 65). How about the B and the C?

Is my code still wrong ><
Really sorry for asking you too many dumb question =,=

You're close, but the code example you provided has the StringBuilder declaration and the printing to the label inside the loop, which means you'll create a new StringBuilder object on each iteration and write to the label on each iteration, replacing what was already there but never having the full result you want.

Here's what I put together earlier as an idea for where you wanted to go, and you're about there.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim input As String = tbInput.Text
        Dim chars As Char() = input.ToCharArray()
        Dim newString As System.Text.StringBuilder = New System.Text.StringBuilder()

        For Each c As Char In chars
            newString.Append(c.ToString() & " = " & (AscW(c)).ToString() & " ")
        Next

        lblOutput.Text = newString.ToString()

    End Sub

Thank you so much for helping me out. And most important, I cant really "deeply" understand how this kind of code works without you. Again, thanks for your consistent help. Now I can do several other assignments that are similar to this one.

By the way, how can I mark these problems I've posted as "solved". I received a warning from the administrator for not doing that. O,o

You're welcome, and I hope we haven't made it too easy for you. It's good to really have to think about some of these things, but I know it can be difficult when you don't have enough information on what exactly you can do with the tools you have. Even more difficult when you have to worry about versions and compatibility between the software you use at home versus what may be available at school.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.