Results 1 to 11 of 11

Thread: What is happening here ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,378

    What is happening here ?

    Hi,

    I don't understand what is happening here...
    I have a subroutine which is called when I change the text in a TextBox...
    vb.net Code:
    1. Private Sub BoxChange(sender As Object, e As System.EventArgs)
    2.         Static chk As String
    3.         If sender.text = chk Then Exit Sub  ' Stop recursion.
    4.         chk = sender.Text.ToString.ToUpper
    5.         sender.Text = chk
    6.     End Sub
    The textbox only has to have one letter, I enter a letter and control passes to this subroutine, if it's text doesn't match chk then control passes to Line 4, otherwise 'Exit Sub'.
    From Line 4, chk is changed to upper case, then the textbox text is changed to chk, so the subroutine is called again, but this time its text IS the same as chk so control jumps to Line 6. Fine that's what it's supposed to do, BUT...
    Step once more and the control jumps back to Line 5.

    What's going on ?


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,929

    Re: What is happening here ?

    If sender.text = chk Then Exit Sub
    Check the value of sender.text and chk at that line.

  3. #3
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,929

    Re: What is happening here ?

    That seems really messy and confusing code. It is calling ToString on a .Text property that is already a string, it doesn't have Option Strict set either.

    The problem you are seeing is down to recursion, step through the code again but this time have the call stack window open. That should explain why.

  4. #4
    Lively Member
    Join Date
    Aug 2014
    Posts
    65

    Re: What is happening here ?

    I think it is something like this. You have the first pass through the subroutine. When it comes to line 5 the textbox value changes. But it did not finish doing that line because the subroutine is called again for a second pass through. When that second pass through is done it continues where it left off on the first pass through. And that was on line 5.

  5. #5

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,378

    Re: What is happening here ?

    Quote Originally Posted by PlausiblyDamp View Post
    That seems really messy and confusing code. It is calling ToString on a .Text property that is already a string, it doesn't have Option Strict set either.
    Yes, I agree, You'd think that 'chk = sender.Text.ToUpper' would do the job wouldn't you? However, doing that the '.ToUpper' part just throws up an error, 'ToLower' it seems is ok, (I've not tried it, it's likely to be lower case already) but 'ToUpper' isn't even offered.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  6. #6
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,929

    Re: What is happening here ?

    Quote Originally Posted by Poppa Mintin View Post
    Yes, I agree, You'd think that 'chk = sender.Text.ToUpper' would do the job wouldn't you? However, doing that the '.ToUpper' part just throws up an error, 'ToLower' it seems is ok, (I've not tried it, it's likely to be lower case already) but 'ToUpper' isn't even offered.


    Poppa
    .ToLower works but .ToUpper throws an error? That sounds weird - what error does it throw?

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,078

    Re: What is happening here ?

    You need to turn Option Strict On. You're trying to fix issues that shouldn't even exist in the first place. Just do the right thing from the start and there will be fewer issues to fix.

  8. #8
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,929

    Re: What is happening here ?

    Quote Originally Posted by Poppa Mintin View Post
    Hi,

    I don't understand what is happening here...
    I have a subroutine which is called when I change the text in a TextBox...
    vb.net Code:
    1. Private Sub BoxChange(sender As Object, e As System.EventArgs)
    2.         Static chk As String
    3.         If sender.text = chk Then Exit Sub  ' Stop recursion.
    4.         chk = sender.Text.ToString.ToUpper
    5.         sender.Text = chk
    6.     End Sub
    The textbox only has to have one letter, I enter a letter and control passes to this subroutine, if it's text doesn't match chk then control passes to Line 4, otherwise 'Exit Sub'.
    From Line 4, chk is changed to upper case, then the textbox text is changed to chk, so the subroutine is called again, but this time its text IS the same as chk so control jumps to Line 6. Fine that's what it's supposed to do, BUT...
    Step once more and the control jumps back to Line 5.

    What's going on ?


    Poppa
    In fact, the more I think about this the more I am convinced you shouldn't be doing things this way. If you really must do this through code then just handle the KeyPress or KeyDown events, however a TextBox has a CharacterCasing property that will do this for you anyway.

  9. #9
    Frenzied Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    1,619

    Re: What is happening here ?

    What is happening here? I don't know what is happening there, where you are but here I am having a cup of tea and a Digestive biscuit. I'm doing that whilst thinking that your requests for help could have a more intelligently thought out subject title that helps people to determine whether they want to dive into your request or not.

    Perhaps you could title the next one, "unspecified problem from Yaddlethorpe" - but I suppose that might be providing TOO much information.
    By the power invested in me, all the threads I start are Niya and Olaf free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  10. #10

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,378

    Re: What is happening here ?

    Quote Originally Posted by jmcilhinney View Post
    You need to turn Option Strict On. You're trying to fix issues that shouldn't even exist in the first place. Just do the right thing from the start and there will be fewer issues to fix.
    Option Strict is on by default.
    Along with the sunshine there has to be a little rain sometime.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,078

    Re: What is happening here ?

    The code you posted will not compile with Option Strict On, so you must either have it Off at the project level or the file level. Your 'sender' parameter is type Object, as is the case for all event handlers, and yet you are accessing its 'text' property. Nope.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width