Hiya,

A short while ago, I posted the following code snippet in reply to someone who was looking to make a simple encoded password... See here for a full explanation of how this works -->http://www.daniweb.com/forums/post1470261.html#post1470261.

While this method is a little cumbersome, it does work and can be encoded and decoded the same on any PC so long as the program version is the same.

Well, that's all fine, but what if you wanted to grease the cogs a bit and stop it from churning through hundreds of textboxes everytime it has to go converting a letter to something else.

What if instead of having two groups of textboxes, I simply declare the whole lot to memory instead. So I will have FixTex1 through to FixTex36 for my fixed alphabet and MixTex1 through to MixTex36 for my mixed/scrambled alphabet.

How then do I modify the code below to say 'For Each FixTex in memory' or 'For Each FixTex in DimGroupFixed', blah, blah, blah?

And a s sub-question to all of this, declaring it into memory: Will it really grease the cogs to make it run faster or will I just sap even more system resource to hold the data in place?

Many thanks in advance,

Rob.

1:          For Each FixedTBox In Me.grpFixedAlpha.Controls
                If TypeOf FixedTBox Is TextBox Then
                    Dim EncodeLetter As String
                    EncodeLetter = Mid(txtInput.Text, txtSelectChar.Text, 1)
                    If EncodeLetter = "" Then
                        MsgBox("Your message has been Encoded.")
                        Exit Sub
                    Else
                        If Mid(FixedTBox.text, 3, 1) = EncodeLetter Then
                            Dim TBoxRef
                            TBoxRef = Mid(FixedTBox.Text, 1, 2)
                            For Each DynamicTBox In Me.grpDynamicAlpha.Controls
                                If TypeOf DynamicTBox Is TextBox Then
                                    If TBoxRef = Mid(DynamicTBox.Text, 1, 2) Then
                                        Dim Output As String
                                        Output = txtOutput.Text
                                        txtOutput.Text = Output & Mid(DynamicTBox.text, 3, 1)
                                        txtSelectChar.Text = txtSelectChar.Text + 1
                                    End If
                                End If
                            Next DynamicTBox
                        End If
                    End If
                End If
            Next FixedTBox
            GoTo 1

See if this helps.

Public Class Form1
    Private FixTex(35) As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FixTex(0) = "something"
        FixTex(5) = "something else"
        FixTex(29) = "and some something more or less"
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 0 To FixTex.Length - 1
            If Not FixTex(i) = Nothing Then
                MsgBox(FixTex(i))
            End If
        Next
    End Sub
End Class

Having 100 Strings instead of 100 TextBoxes should enhance the performance of your application quite a bit.

First of all, the text does not have to load in the TextBoxes and have your application draw the text in them.
Second, not really sure what other type of stuff goes on as to checking if any event has to fire when adding/changing text, etc.. I have yet not ventured to finding out how the .Net Framework responds.
And finally, that is 100 less controls with Properties that are not added to your application.

So I will have FixTex1 through to FixTex36 for my fixed alphabet and MixTex1 through to MixTex36 for my mixed/scrambled alphabet.

If you just need to loop through letters in the alphabet, scrambled or not, let me know. There is a much simpler solution for such.

Edited 5 Years Ago by codeorder: n/a

Thank you for your reply and for a new bit of code to have a hemmorage over.

I'll take a good look at that this afternoon and see if I can integrate it in. Before I do, I can see you have a "Private FixTex(35) As String". Am I right to assume that is declaring all 36 FixTex values in go? And since you have a "(0)" in the next bit, it's another of those silly thing that starts at 0 instead of 1.

And secondly, in the Form Load, you've stated three of the FixTex's, I presume that's because to type all 36 would have been a ludicrous waste of time for an example and that I should declare all 36 there.

And lastly, you have me intrigued with your closing comment. If you have another way to step through a fixed alphabet and then substitute it for a scrambled one that will work in reverse as well, I'd love to see it in action.

This little project has become something of an obsession and I'm keen to make it as efficient and as good as it possibly can be. And I'm still new enough to all of this to know never to pass up an opportunity to learn how to do something differently because the chances are, it'll be sleeker and less verbose than what I came up with myself.

Cheers,

Rob.

>...I can see you have a "Private FixTex(35) As String". Am I right to assume that is declaring all 36 FixTex values in go?...
You are correct.
Instead of declaring FixTex1, FixTex2,FixTex3 As String, you declare all 36 Strings with FixTex(35) in one shot, as a String Array.

>...since you have a "(0)" in the next bit, it's another of those silly thing that starts at 0 instead of 1.
Index based always starts at "0", not "1" like .Length or .Count.

>...in the Form Load, you've stated three of the FixTex's, I presume that's because to type all 36...
That is just a way to add values to the declarations and if needed, a separate Sub just for that purpose could be of use. A For/Next loop would be even better.

>...it'll be sleeker and less verbose than what I came up with myself.
Even if I came up with something myself, the more I use vb.net, the more "sleeker" ways I can figure out how to enhance something that I have previously enhanced. Makes me feel like a newbie sometimes, which is always a good thing.

About the:
>...another way to step through a fixed alphabet and then substitute it for a scrambled one that will work in reverse as well,...
See if this helps.
1 Button

Public Class Form1
    Private myChars() As Char = "ABCDE"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 0 To myChars.Length - 1 '// loop thru all char.s.
            MsgBox("fwd loop: " & myChars(i)) '// get char. by Index.
        Next
        For i As Integer = myChars.Length - 1 To 0 Step -1 '// loop backwards thru all char.s.
            MsgBox("reverse loop: " & myChars(i)) '// get char. by Index.
        Next
        '// sTemp adds a char. if not there, chrTemp is used to store a random char., rnd is for randomizing.
        Dim sTemp As String = "", chrTemp As Char, rnd As New Random
repeat: For i As Integer = 0 To myChars.Length - 1 '// loop thru all char.s.
            chrTemp = myChars(rnd.Next(0, myChars.Length)) '// select a random Indexed char..
            If Not sTemp.Contains(chrTemp) Then '// check if char. has not already been added.
                sTemp &= chrTemp '// add char. to a temp String.
                MsgBox("random char. loop: " & chrTemp)
                If sTemp.Length = myChars.Length Then Exit Sub '// Exit Sub since all char.s have been randomly located.
            End If
        Next
        If Not sTemp.Length = myChars.Length Then GoTo repeat '// if not your temp String is the same length as your char.s, repeat process.
    End Sub
End Class

I've not had a chance to look at any of this yet this afternoon but fully intend to start putting it to work soon.

I'm not going to get a chance to do anything this weekend and am out the office for a couple of day beginning of next week so just wanted to say 'thanks' now rather than not post any kind of reply for five days and come accross as an ungrateful sod.

Thank you ever so much for all this and I look forward to seeing how it all goes when I get it implemented. Sadly, I've just found a whole bunch of other things that require re-working so have some heavy coding to do when I return.

Hopefully though, I can sneak a load of this in at the same time and all will be well. I'll hold off marking as resolved for the time being, but will do so and also gush praise and thanks in due course.

Many thanks again,

Rob.

This article has been dead for over six months. Start a new discussion instead.