arrays and string

vegeta_man111

Member
Joined
Sep 18, 2006
Messages
8
Programming Experience
Beginner
Ok, I am wokring on a project for a class. I need to know a few things for this to get done.

1.) I need to take the alphabet (a-z obviously) and give each letter a value. How would I go about declaring each letter's vale seperate? I thought array, but I am not sure how arrays work in vb.net.

2.) Is there anyway to take text from a textbox and break it down (like if I put 'abcd' into a textbox, then clicked a button, it would take that 'abcd' and know whats in there like a, b, c, and d, instead of knowing a value of 'abcd' is in there?

3.) Is there anyway to take a number and divide it by 2, and if its not even, give the remainder?
 
VB.NET:
Dim myString As String = "Hello World"
Dim myChars As Char() = myString.ToCharArray()

For Each ch As Char In myChars
    MessageBox.Show(ch)
Next ch
Or:
VB.NET:
For i As Integer = 0 To myChars.GetUpperBound(0) Step 1
    MessageBox.Show(myChars(i))
Next i
 
ok, i got that part. I am not sure if you are understanding on what I tried saying, but what I am doing is:

1. Inputting text into a textbox
2. Pressing a button to take that textbox and put it to a string
3. Taking the string and checking for what chars are there and then assigning eacvh char a value
4. taking each value and then putting it into binary
5. taking that binary and outputting it into a label.

Hope this helps you understand my recent question.
 
What I am providing you with is an example of how to get every character from a string, which is exactly what you want to do. It doesn't matter where that string comes from. I've hard-coded the string into my code example, you're getting your's from a TextBox. Who cares? A string is a string. I've shown you how to access every character in that string. I've displayed those characters in message boxes, you want to get a numerical value for the character. You've already been shown how to do that to. It's time to take the pieces of information you've been provided with and put them together and apply them to your situation. You already have the knowledge to accomplish steps 1, 2, 3 and 5. If you want to create a binary representation of a number you use Convert.ToString(myNumber, 2). Now you hve the knowledge to accomplish the whole thing. It's up to you to apply that knowledge.
 
Let me be a bit more explicit:
VB.NET:
Dim myInputChars As Char() = myTextBox.Text.ToCharArray()
Dim myOutputString As String = String.Empty

For Each ch As Char In myInputChars
    '1. Convert the Char to a number.
    '2. Convert the number to a binary string representation.
    '3. Add the binary string to the output.
Next ch

'4. Display the output in the Label.
You have already been shown how to do 1 and 2 and I would guess that you already know how to do 3 and 4. You can reduce steps 1 to 3 to a single line of code if you want. Note also that you don't even need the Char array because you can get the Chars directly from the string, which you've also already been shown.
 
ok, I understand it now. sorry if i sounded like I was yelling, I didnt mean to come across like that. Thank you for your help, and I will let you know if I need any more help.
 
Back
Top