Welcome to Dream.In.Code
Become a VB Expert!

Join 149,478 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,667 people online right now. Registration is fast and FREE... Join Now!




Arrays

2 Pages V  1 2 >  
Reply to this topicStart new topic

Arrays

INeedHelpPlease
26 Apr, 2007 - 11:00 AM
Post #1

New D.I.C Head
*

Joined: 25 Jan, 2007
Posts: 41


My Contributions
Hi i have created a program but its not working and wondered if anyone could help.

i have a list of names which consist of a member number (7 digits and a name).

i was required to

create a boolean function called onthelist that accepts a single parameter(membern0) of data type string.

the function should process the array, searching for the member as specified by the parameter memberno. once the memberno has been found the function should return a true value or if not found false value. also a message will appear saying if there a member or not.

here is my current code. when i enter a digit or letter it comes up with an error saying "index was outside the bounds of the array


thanks if anyone can help

This post has been edited by INeedHelpPlease: 29 Apr, 2007 - 10:36 AM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Arrays
26 Apr, 2007 - 10:47 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
When you get the error message, which line is it pointing too?

Also, where is the Function you are describing?

You don't have any functions in the code you provided.
User is online!Profile CardPM
+Quote Post

INeedHelpPlease
RE: Arrays
27 Apr, 2007 - 06:07 AM
Post #3

New D.I.C Head
*

Joined: 25 Jan, 2007
Posts: 41


My Contributions
this part of the coding

CODE

If Thelist(counter) = "" Then


what would i change to get it to be what i want it to be?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Arrays
27 Apr, 2007 - 08:08 AM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Your array only contains 100 elements. The index number of your array can only be 0 to 99, in this case. So the following statement will produce your error when it reached 100. Remove the equals sign to keep from actually getting to 100, which will cause your error.
CODE

While counter < 100


User is online!Profile CardPM
+Quote Post

INeedHelpPlease
RE: Arrays
27 Apr, 2007 - 08:36 AM
Post #5

New D.I.C Head
*

Joined: 25 Jan, 2007
Posts: 41


My Contributions
that worked but then when i tried to enter the member number it came up with member not on list when the number is in the list
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Arrays
27 Apr, 2007 - 09:32 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
That is because of the problem that exists in this loop. This loop will iterate through every member in the list and will always exit with the memberdetails of the last member in your array.
CODE

        Do While Counter <= Members - 1
            memberdetails = Thelist(Counter).Substring(0, 6)
            If memberdetails = membernotocheck Then
                Found = True
                Counter = Members
            Else
                Found = False
            End If
            Counter += 1
        Loop

What you need to do is exit the loop immediately upon finding the correct member. This can be done simply by inserting an Exit statement in your loop.
CODE

        Do While Counter <= Members - 1
            memberdetails = Thelist(Counter).Substring(0, 6)
            If memberdetails = membernotocheck Then
                Found = True
                Counter = Members
                Exit Do
            Else
                Found = False
            End If
            Counter += 1
        Loop


User is online!Profile CardPM
+Quote Post

INeedHelpPlease
RE: Arrays
27 Apr, 2007 - 09:47 AM
Post #7

New D.I.C Head
*

Joined: 25 Jan, 2007
Posts: 41


My Contributions
im afraid that doesnt work. here is an example of what is in the text box

2308571 John Smith
2391912 Ben Sherman
2385837 Ralph Lauren

there are 100 of these in the text file
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Arrays
27 Apr, 2007 - 02:23 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Can you zip up your entire project and upload it to this topic?

I think I can provide better help, if I can actually see whats going on, while its running.
User is online!Profile CardPM
+Quote Post

INeedHelpPlease
RE: Arrays
28 Apr, 2007 - 01:11 AM
Post #9

New D.I.C Head
*

Joined: 25 Jan, 2007
Posts: 41


My Contributions
here it is!

This post has been edited by INeedHelpPlease: 29 Apr, 2007 - 12:40 AM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Arrays
28 Apr, 2007 - 02:07 PM
Post #10

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You have a couple of problems that are preventing your code from working correctly.

The first problem is that you never assign a value to Members, as a result your While loop will never execute.

Change your CountMembers to a function that returns the count.

Also there is a flaw in the logic of your IF statement. What will happen if you have less than 100 names in your text file?

You will get stuck in an endless loop. Give it a try if you want, delete some of the names out of the text file and see what happens.

When designing code, you really need to think outside the box about things that could happen. Just a helpful suggestion for future reference. smile.gif

The way I designed the following function it won't matter if there is 100 names or only 15. It will work correctly each time.

Somthing like this:
CODE

    Function Countmembers() As Integer
        Dim counter As Integer

        While counter < Thelist.Length
            If Thelist(counter) Is Nothing Then
                Return counter
            End If
            counter += 1
        End While

        Return counter
    End Function


Now change how you are calling the Function, so that you are storing the returned value into Members.
CODE

Members = Countmembers()


The next problem is your use of the Substring method. The first parameter is the index number of the starting location inside the string. You have that correct at 0. The second parameter is the number of characters to extract from the string.

You are telling it to extract 6 characters, starting from the 0 position in the string. The member numbers are 7 digits long. So you need to extract 7 characters to get an accurate comparison.

CODE
memberdetails = Thelist(Counter).Substring(0, 7)


Make those changes and it works just fine.
User is online!Profile CardPM
+Quote Post

INeedHelpPlease
RE: Arrays
29 Apr, 2007 - 12:46 AM
Post #11

New D.I.C Head
*

Joined: 25 Jan, 2007
Posts: 41


My Contributions
sorry still havin problems. i declared members at the top

This post has been edited by INeedHelpPlease: 29 Apr, 2007 - 10:37 AM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Arrays
29 Apr, 2007 - 09:15 AM
Post #12

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You missed a step in my previous instructions. You need to change this line.
CODE

Call Countmembers()


To this:
CODE
Members = Countmembers()


A function returns a value, as a result you either need to store the returned value in a variable, as I have shown. Or store its output in a messagebox or some other type of output control.
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:30PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month