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?
 
1) Read tutorials about arrays in VB.NET such as this one.

2)
VB.NET:
        Dim txtStr As String = TextBox1.Text
        For i As Integer = 0 To Len(txtStr) - 1
            MsgBox(txtStr.Chars(i))
        Next
3) The Mod function returns the remainder of a division. Try Msgbox(3 Mod 2) to see the result.
VB.NET:
    Function returnDiv(ByVal a As Integer, ByVal b As Integer) As Integer
        If Not (a Mod b) = 0 Then
            Return (a Mod b)
        Else
            Return (a / b)
        End If
    End Function

'Call as:
Dim c As Integer = returnDiv(3, 2)
Also check http://msconline.maconstate.edu/tutorials/VBNET/VBNET01/vbnet01-04.aspx.
 
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.


Answer
One way is to use a collection, like so:

VB.NET:
Dim Alphabet() As String = {"a","b","c", "d","e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

Dim Values() As String = {"a value","b value","c value", "d value","e value", "f value", "g value", "h value", "i value", "j value", "k value", "l value", "m value", "n value", "o value", "p value", "q value", "r value", "s value", "t value", "u value", "v value", "w value", "x value", "y value", "z value"}

' Then when you need the value, get the position in the array of the letter (In 'Alphabet', starting from 0), then use that number to get the value from  'Values'.

' This isn't the best way, but its the quickest one I could explain at the moment

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?
Answer
Use the StringInstance.ToCharArray function as so:

VB.NET:
        ' Sample string
        Dim X As String = "abc"

        Dim I As Integer = 0

        For I = 0 To X.ToCharArray.Length - 1

            MsgBox(X(I)) ' For every character in the string you will get a messagebox containing one of the characters in the array starting from 0 to the end of the caracter array.

        Next

Is there anyway to take a number and divide it by 2, and if its not even, give the remainder?
Answer
Yes, and its very easy

VB.NET:
        ' IEEERemainder returns the remainder
        ' Format: Math.IEEERemainder(Dividend As Double, Divisor As Double)
        Dim TheProblem As Integer = Math.IEEERemainder(4, 3)

        If TheProblem > 0 Then
             MsgBox("It did not divide equally, the remainder is: " & TheProblem.ToString)
        Else
            MsgBox("It divided equally.")
        End If

Hope that helped :)
 
1) Read tutorials about arrays in VB.NET such as this one.

2)
VB.NET:
        Dim txtStr As String = TextBox1.Text
        For i As Integer = 0 To Len(txtStr) - 1
            MsgBox(txtStr.Chars(i))
        Next
3) The Mod function returns the remainder of a division. Try Msgbox(3 Mod 2) to see the result.
VB.NET:
    Function returnDiv(ByVal a As Integer, ByVal b As Integer) As Integer
        If Not (a Mod b) = 0 Then
            Return (a Mod b)
        Else
            Return (a / b)
        End If
    End Function

'Call as:
Dim c As Integer = returnDiv(3, 2)
Also check http://msconline.maconstate.edu/tutorials/VBNET/VBNET01/vbnet01-04.aspx.

Why make your own function to return the remainder? Just use Math.IEEERemainder(Dividend, Divisor) :)

Also, I believe its a little better to use ToCharArray rather than Len(), just my personal perception of the issue.

Correct me if I'am wrong :eek:
 
Leters already have numerical values. They're called ASCII codes.
VB.NET:
Dim myString As String = "Hello World"

'Get the characters from the Chars property of the string.
For Each ch As Char in myString
    MessageBox.Show("{0} = {1}", ch, Convert.ToInt32(ch))
Next ch

'Convert the string to a Char array and get the characters from that.
For Each ch As Char In myString.ToCharArray()
    MessageBox.Show("{0} = {1}", ch, Convert.ToInt32(ch))
Next ch
Note that upper case and lower case letters have different ASCII codes so if you want to only use one set of values then call ToUpper or ToLower on the original string

Note also that 'Mod' and '\' are complementary operators:
VB.NET:
5 / 2 = the Double value 2.5
5 \ 2 = the Integer value 2
5 Mod 2 = the Integer value 1
If you want both the quotient and remainder then you can use Math.DivRem:
VB.NET:
Dim remainder As Integer
Dim quotient As Integer = Math.DivRem(5, 2, remainder)
After that quotient will contain 5 and remainder will contain 1. If you don't need both then just use the appropriate operator rather than invoking any function at all.
 
wow, thanks a bunch everyone for your help. Now I have another question for you guys. So now i got my remainder. I have taken multiple numbers (atleast 2+) and gotten the remainders. Now, I would like to take those remainders(even if it is 0) and put them into a string in reverse order. How would I go about doing this?
 
Here's an example:
VB.NET:
        Dim a As Integer = 5
        Dim b As Integer = 6
        Dim c As Integer = 0

        Dim str As String

        str = a.ToString & b.ToString & c.ToString
        str = Microsoft.VisualBasic.StrReverse(str)
        MsgBox(str)


I am unaware if there is (I assume there is) String reverse function in VB.NET so I used the VB6 one. If anyone knows it please post it so I can find out also :)

Why a function? I dunno just wrote it on the spot! I guess you guys are right the ToCharArray might have been better to use... but hey they all do the work! Thanks for the comments :)
 
I just found out that for vb.net the code for reverse of a string is:

VB.NET:
Reverse(<string>)

someone please tell me if this is correct for vb.net 2003
 
Instead of creating a string and reversing it try creating it in reverse order in the first place:
VB.NET:
Dim originalString As String = "Hello World"
Dim newString As New System.Text.StringBuilder

For i As Integer originalString.Length - 1 To 0 Step -1
    newString.Append((Convert.ToInt32(originalString(i)) Mod 2).ToString())
Next i

MessageBox.Show(newString.ToString())
 
I just found out that for vb.net the code for reverse of a string is:

VB.NET:
Reverse(<string>)
someone please tell me if this is correct for vb.net 2003
I don't know who told you that but it's a crock. That's not valid in any version of VB.NET. There is no way to specifically reverse the characters in a string in the .NET Framework. You can use the VB Runtime function StrReverse as suggested or you can use the Array.Reverse method like so:
VB.NET:
Dim myString As String = "Hello World"
Dim myChars As Char() = myString.ToCharArray()

Array.Reverse(myChars)
myString = New String(myChars)
MessageBox.Show(myString)
Note that after this myString refers to a different String object than it did to start with.

Edit: Let me qualify that by saying that the Microsoft.VisualBasic namespace is part of the Framework, but I meant under the System namespace.
 
i think it was vbcodeproject.com but i am not 100% positive.
My guess is that they defined a method named Reverse and implemented it somewhat like this:
VB.NET:
Private Sub Reverse(ByRef str As String)
    Dim chars As Char() = str.ToCharArray()

    Array.Reverse(chars)
    str = New String(chars)
End Sub
You could then call that method as you have shown.
 
ok, so far everything that people have given me (code and help wise) has helped. Now, I am stuck on the biggest problem. When text is inputted into the textbox like I said, then it would be transferred into values and put into strings, I dont know how many letters are gonna be used so I cant set a certain amount of strings for them. What do i do now?
 
Haven't we already covered that? You convert the string to an Char array. You can then loop through the arary to touch each Char, create another array of the same size to store numbers, etc.
 
yeah, we started to cover it. we got everything covered, but I got lost when it came to how the loop would work for the text. Is there anyway you could clean that area up for me so I know how to do it then? thanks in advance!
 
Back
Top