Weird VB (.net 2005) problem

enlightenedby42

Supreme [H]ardness
Joined
Jan 19, 2005
Messages
4,412
I'm trying to do a really stupid simple little VB program that stores phone book entries in an array and allows you to search the array for an entry (a simple structure with a name, phone, and address as strings) based on the name and display the name,phone and address strings in the text boxes when you find it.

Stupid easy, and it all works fine except for one little quirk...when I try to set the .Text property of the text boxes to the string values within the structures in the array using something to this effect:

Code:
txtName.Text = phoneBook(i).name
the text boxes display the name of the text box CLASS as well as the value within it like this:

Code:
System.Windows.Forms.TextBox, Text: some name
So, it is displaying the correct name (some name in this case), but it includes all that weird shit before the string to be displayed in the text box.

This is a stupid lower division class that's completely based around displaying this or that in text boxes and I haven't run into this before. Did I accidentally set an obscure text box property or something? Any suggestions would be much appreciated as this is strange and annoying.
 
It looks like when you assign values to the array it looks like your assigning the objects (textbox) hashcode, check that "phoneBook(i).name = sometextbox.text" instead of "phoneBook(i).name = sometextbox".

Without seeing some code, thats' all that comes to mind.
 
Well, the entire function is as follows:
Code:
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim searchName As String
        Dim i As Integer
        searchName = txtName.Text.ToString
        For i = 0 To phoneBook.Length - 1
            If StrComp(phoneBook(i).name, searchName) Then
                txtName.Text = phoneBook(i).name
                txtAddress.Text = phoneBook(i).address
                txtPhone.Text = phoneBook(i).phone
                changeIndex = i
                btnChange.Enabled = True
                Exit For
            End If
        Next
    End Sub
note: you're supposed to activate a button to change and save the values of the entry after you've searched, hence the button enabling and stuff, not really relevant to the issue; phoneBook() and changeIndex are both global.

I see what you mean, but my problems are in going the other direction, as the array is populated by reading the values in those same text boxes. Now the only thing is just trying to get them to display the string. I'm wondering if there's some weird hangup about dereferencing strings within a structure or something but that doesn't make sense.

Text is displayed in the boxes just fine if I hard code a literal or something.

Its SO irritating to have a strange little issue like this for such a simple assignment.
 
I think your problem lies in what you're trying to set the text to.

txtName.Text = phoneBook(i).name
txtAddress.Text = phoneBook(i).address
txtPhone.Text = phoneBook(i).phone

I can't see your array with the code you've provided - but I'm pretty sure that array(1).whatever isn't a valid array key. I also can't see where you're getting the phonebook entries from, if it's a text file, database et al. However, I think you should be using a multi-dimensional array.

As explained here:

http://www.startvbdotnet.com/language/arrays.aspx

Jagged Arrays

Another type of multidimensional array, Jagged Array, is an array of arrays in which the length of each array can differ. Example where this array can be used is to create a table in which the number of columns differ in each row. Say, if row1 has 3 columns, row2 has 3 columns then row3 can have 4 columns, row4 can have 5 columns and so on. The following code demonstrates jagged arrays.

Dim colors(2)() as String
'declaring an array of 3 arrays
colors(0)=New String(){"Red","blue","Green"}
initializing the first array to 3 members and setting values
colors(1)=New String(){"Yellow","Purple","Green","Violet"}
initializing the second array to 4 members and setting values
colors(2)=New String(){"Red","Black","White","Grey","Aqua"}
initializing the third array to 5 members and setting values

Looking at that, I would think that would be the best way to approach the problem you're facing. Store each entry into one key like that

phoneBook(0)=New String(){ "Name", "Address", "Number" }

Then recursively parse through the array and output the information as required. This would also provide you with the ability to add extra information to individual people - but you would need to take particular care to ensure that the data structure is the same for everyone, or you could end up putting a phone number in for someone's address. In that case you might want to use a 'null' string or something to indicate that there's no information for that section.

If you want to continue using the method you're using, you could try the following as explained here: http://en.wikibooks.org/wiki/Visual_Basic_.NET/Arrays

It looks like in your code you're declaring an array key with the 'value' of it. but... the problem with this approach is that you are limiting yourself to one value per key and you can't change 'setvalue' to a persons name. That being said, I think I would probably go with the approach I mentioned above. It seems like much less of a headache.. especially if you're dataset is already structured for you.

Code:
   Option Strict On
   Module Module1
   Sub Main()
       '
       Dim MyArray As System.Array
       Dim En As System.Collections.IEnumerator
       MyArray = System.Array.CreateInstance(GetType(String), 4)
       '
       MyArray.SetValue("a", 0)
       MyArray.SetValue("d", 1)
       MyArray.SetValue("b", 2)
       MyArray.SetValue("c", 3)
       '
       En = MyArray.GetEnumerator
       Console.WriteLine("Before descending sort")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       MyArray.Sort(MyArray)
       MyArray.Reverse(MyArray)
       En = MyArray.GetEnumerator
       Console.WriteLine("After descending sort")
       Do While En.MoveNext
           Console.WriteLine(En.Current())
       Loop
       '
       Console.WriteLine("Press enter to continue")
       Console.ReadLine()
   End Sub
   End Module

I could also be wrong :)

http://www.google.com/search?q=visu...s=org.mozilla:en-US:official&client=firefox-a
 
Wow, thanks for the responses. The thing about this class is that it is beginning level (this assignment was an introduction into arrays) and I'm taking it as a junior. It actually didn't even store or read anything...he just wanted you to store the entires from the text boxes in a one dimensional array as simple structures of strings just while the program was running. Oddly enough, I'm doing something similar in Java for a higher level object oriented programming class using iterators and more interesting things like that to accomplish the same task in productive and useful ways like some of the ones you mentioned.

Never could get the damn thing to display just the value of the string without the class name of the stupid text box, and even the professor (a really really mediocre one) didn't have any idea what was wrong with it, so I just turned the stupid thing in before I had a chance to see the last few responses. Apologies for not posting more code, I was just trying to clutter the thread as little as possible, as I'm a couple of years ahead of this class in the CS program and thought I had just run into some stupid VB gotcha that somebody else might have seen in a similar situation. Pretty much ANY way of accomplishing this task is better than what I'm doing, but that's what he asked for so that's what he's getting.

Nevertheless though, thanks for the info three_sixteen. While it was beyond the scope of that one short bus riding little assignment, I was curious about doing things in VB that were more than trivial and i will definitely remember a few of those things.

Maybe its just me being only a couple of years into the field and using mostly C++ and Java, but I REALLY don't care for VB too much. The syntax is just weird in seemingly arbitrary ways IMO, and with C# being the golden child of .net...it just seems like a red headed step child. I think a lot of it may be the fact I'm stuck doing stupid little "change the color of the background" nonsense in this class, and then going to work and doing serious database stuff in Java and Delphi the next day.

At any rate, thanks again for the replies.
 
Back
Top