5 Replies - 166 Views - Last Post: 01 January 2018 - 03:21 PM Rate Topic: -----

#1 bfharry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-December 17

Trimming a simple string VB.NET [FAST]

Posted 29 December 2017 - 10:11 AM

Hello!

I have a problem. When I add a 'trim' code to my Sub, it does not give me the correct output. But when I do not have my trim code added it gives me the correct output ??

IMAGES: Before adding the 'trim' code: (Look at the red circle around 'katmat3')
IMAGE BEFORE (katmat3)

IMAGES: After adding the 'trim' code:
IMAGE AFTER (katma3)

----
The thing I want to do is my application creates an password that is constructed by three strings.
schoolID: kat (autogenerated)
TeacherID: 3 (autogenerated)
CustomCode: mat (User Input)

password would be = schoolID + CustomCode.text + TeacherID = "katmat3"

And it is katmat3 inside my Database:
Database screenshot

It does the same with every other passwords!

The label that shows "katmat3" or "katma3" is called 'holdkode'
The thing that I am confused about; is that why in the hell does it effect holdkode.Text when I have not said that the trimming should be doing anything to that label.

Here is the code:
       Try
            'READ DATA FROM DB
            conn.Open()
            cmd.Connection = conn
            cmd.CommandText = "SELECT `holdkode`, `ID` FROM `teacher_subjects` WHERE `holdnavn` = '" & ListBox1.SelectedItem & "' AND `teacher_ID` = " & Me.ID.Text
            Dim lrd As MySqlDataReader = cmd.ExecuteReader()
            While lrd.Read()
                Dim subjectpass = Convert.ToString(lrd("holdkode"))

                secretHoldkode.Text = subjectpass
                holdkode.Text = subjectpass
                holdnavnTXTBOX.Focus()
                subjectID.Text = Convert.ToString(lrd("ID"))

                Dim schoolID_Trim As String = skole_ID.Text
                Dim teacherID_Trim As String = subjectID.Text
                Dim FullText_Trim As String = secretHoldkode.Text

                Dim process1 As String
                Dim process2 As String
                Dim result As String


                process1 = FullText_Trim
                process2 = process1.Remove(0, schoolID_Trim.Length)
                result = process2.Remove(process2.Length - teacherID_Trim.Length, teacherID_Trim.Length)

                holdkodeTXTBOX.Text = result


            End While
        Catch ex As Exception
            MessageBox.Show("Error while retrieving records on table..." & ex.Message, "Load Records")
        Finally
            conn.Close()


        End Try



Is This A Good Question/Topic? 0
  • +

Replies To: Trimming a simple string VB.NET [FAST]

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13657
  • View blog
  • Posts: 54,503
  • Joined: 12-June 08

Re: Trimming a simple string VB.NET [FAST]

Posted 29 December 2017 - 10:46 AM

I am not following - what's the issue?

Side note I am not understanding what you are saying when you are 'trimming'. What are you expecting to trim off?
Was This Post Helpful? 0
  • +
  • -

#3 bfharry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-December 17

Re: Trimming a simple string VB.NET [FAST]

Posted 29 December 2017 - 10:55 AM

When I add this piece of code inside my while lrd.Read()

Dim schoolID_Trim As String = skole_ID.Text
                Dim teacherID_Trim As String = subjectID.Text
                Dim FullText_Trim As String = secretHoldkode.Text

                Dim process1 As String
                Dim process2 As String
                Dim result As String


                process1 = FullText_Trim
                process2 = process1.Remove(0, schoolID_Trim.Length)
                result = process2.Remove(process2.Length - teacherID_Trim.Length, teacherID_Trim.Length)

                holdkodeTXTBOX.Text = result



It goes from the original output: "katmat3" to katma3". I am not sure why.

The whole reason why I want to "trim" my string is because, I want to get the middle value of the original output: "katmat3" -> "mat" (middle value/What the user typed, when he was creating the password string.)
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5121
  • View blog
  • Posts: 13,788
  • Joined: 18-April 07

Re: Trimming a simple string VB.NET [FAST]

Posted 29 December 2017 - 10:57 AM

I want to start off by saying you are doing one heck of a lot of variable/value shuffling there and half of it I believe might be unnecessary. I think what you might be running into is multiple variables referring to the same string. In .NET a string is a reference type and immutable. So it is very possible that two variables can point at the same string so when you change a string in one part, it will be reflected in another.

I am not sure, but since subjectpass, which is assigned to holdkode.Text, is also assigned to secretHoldKode.Text those two may be pointing to the same string. You then assign that string to FullText_Trim which means that FullText_Trim also points at subjectpass. Then you assign that to process1 which means that too also points to subjectpass. You then go ahead and start your removing which could be altering subjectpass through process1's reference and thus changes it for holdkode.Text, secretHoldKode.Text, FullText_trim etc.

Of course that is just a wild guess at this point. What can help you nail this down is to put in a break point on line 12 of the code snippet above. Check the value of subjectpass. Then step through each line watching subjectpass' value in your locals window as well as the value of holdkode.Text (you can still hover over holdkode.Text on line 11 while your in a break on line 26. Watching the values will show where in the process you are altering the value of holdkode.

Hopefully I am making myself clear here. To get a better idea of reference types and strings and the idea of two variables pointing to the same value, refer to the page below...

https://msdn.microso...(v=vs.110).aspx

The page shows an example of why you may need the method "Copy" and the difference between copying a string vs assigning to it.
Was This Post Helpful? 1
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13657
  • View blog
  • Posts: 54,503
  • Joined: 12-June 08

Re: Trimming a simple string VB.NET [FAST]

Posted 29 December 2017 - 10:58 AM

Look at the ranges of text you are doing a '.remove' with. Why not just do 'substring' for the start and end index points you want?
Was This Post Helpful? 0
  • +
  • -

#6 Y00PER  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 08-November 17

Re: Trimming a simple string VB.NET [FAST]

Posted 01 January 2018 - 03:21 PM

I'm guessing that the code you're posting isn't exactly what's in your actual program. When you first grab the teacher ID, you are getting it with the leading space (" 3"). But there is no leading space in the input password, so line 26 removes the last two characters ("t3"), leaving just "ma" then you reassemble "kat" and "ma" and "3" (now trimmed of its space) to end up with katma3.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1