943,648 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 932
  • VB.NET RSS
Jan 2nd, 2009
0

Timing problem? Variable does not always update

Expand Post »
All my (programming) life I’ve been plagued by timing problems! I assumed (haha) that when I changed from a pervasively multitasking OS to Vista I would escape the problem but it seems like I must be missing something.
My problem lies in WhichChar. Note the line:
'MsgBox(vbNullString)
If I leave it commented out, the StringBuilder retVal is only occasionally updated, about half the time. If I uncomment the MsgBox line, retVal is always correctly updated.
I did see that the Controls are not guaranteed to be thread-safe, that may be the problem I am running into. I have two more problems with ComboBoxes in general, but I will keep digging to see if I can resolve them myself.
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub asteriskComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles asteriskComboBox.SelectedIndexChanged
  2. WhichChar(asteriskComboBox, asteriskReplacement, asteriskComboIndex)
  3. End Sub
  4.  
  5. Public Sub WhichChar(ByVal CBox As ComboBox, ByRef retVal As StringBuilder, ByRef indexToChange As Integer)
  6. If Not isInstantiating Then
  7. indexToChange = CBox.SelectedIndex
  8. Dim selectedIndex As Integer
  9. selectedIndex = CBox.SelectedIndex
  10. Dim selectedItem As New Object
  11. selectedItem = CBox.SelectedItem
  12. retVal.Length = 0
  13. 'MsgBox(vbNullString)
  14. Select Case indexToChange
  15. Case 0 'single space
  16. retVal.Append(" ")
  17. Case 1 'hyphen
  18. retVal.Append("-")
  19. Case 2 'single space>hyphen<single space>
  20. retVal.Append(" - ")
  21. Case 3 'open single quote
  22. retVal.Append("`")
  23. Case 4 '' close single quote
  24. retVal.Append("'")
  25. Case 5 'underscore
  26. retVal.Append("_")
  27. Case Else 'everything else
  28. retVal.Append(CBox.SelectedText)
  29. End Select
  30. End If
  31. End Sub

This is a project with multiple purposes. The actual job of the program is to rename songs. I am using iTunes to put my CD collection on my computer. Most of the CDs are identified automatically, others I can ID with a few of the free online DB services. A handful I can find the track list online but not in a database. In these cases the file name and song title look like “01 Track 01”, “Track 1” or “01 Track”.
Entering all this info manually would take less time than writing the code! So, I last worked in Basic in the late ‘70s, got into C then C++ as soon as alpha wear was available. I recently switched to Windows and thought I would play with a bit of programming. This is my first attempt (after “Hello, world!”) and I am finding the going a bit slow.

Thanks in advance for the help I get!

-Ed
Last edited by edgar5; Jan 2nd, 2009 at 3:41 am.
Reputation Points: 18
Solved Threads: 2
Light Poster
edgar5 is offline Offline
30 posts
since Dec 2008
Jan 2nd, 2009
0

Re: Timing problem? Variable does not always update

just for kicks and laughs, let's try to replace the (or just below the line of) 'msgbox line with Application.Doevents(). I've noticed that sometimes when a msgbox makes code work that doesn't seem to work without it, a Doevents is the right medicine. Let me know if it works or not.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 2nd, 2009
0

Re: Timing problem? Variable does not always update

Click to Expand / Collapse  Quote originally posted by Comatose ...
...replace...with Application.Doevents()
No, did not help. In fact made the problem 100%--with neither MsgBox nor Doevents it picks up three or four of the eight Listboxes. With Doevents it gets none!

-Ed
Reputation Points: 18
Solved Threads: 2
Light Poster
edgar5 is offline Offline
30 posts
since Dec 2008
Jan 8th, 2009
1

Re: Timing problem? Variable does not always update

I have (I think) determined the problem. My terminology is very rusty, but I think the word is “scope” (might now be a Namespace thing).

Here is what is going on…

The Form and the ComboBox were created in the VB visual editor. As I expected this to be a simple project (HaHa) I decided to put all the code within the Public Class form1…End Class code section (as the beginner examples like “Hello, world!” seem to do. VB declares all the gadgets and underlying support code ( main() etc.) in some hidden file—I found it once but seem to have lost it! The ComboBox is declared as somewhat global, but it’s member Subs are somehow private (is my guess).

This mean that when I call Private Sub ComboBox_SelectedIndexChanged() and within that function examine the contents of ComboBox.SelectedText the value returned is accurate, but when I pass (ByRef or ByVal) ComboBox to Public Sub WhichChar() the values returned by ComboBox.SelectedText are in fact wild pointers and only accidently point to anything reasonable occasionally.

I need to learn how to create classes which inherit from others (I do that in C++--maybe I should simply try Visual C++) That way I might be able to get the data out of the ComboBox any time I needed it. Have to play around with it some.
Reputation Points: 18
Solved Threads: 2
Light Poster
edgar5 is offline Offline
30 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: how to change file name?
Next Thread in VB.NET Forum Timeline: Problem in Removing Node from XML using Vb.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC