views:

81

answers:

4

Right - to start with, I'm entering unfamiliar areas with this - so please be kind!

I have a script that looks a little something like this:

Private Function checkString(ByVal strIn As String) As String
    Dim astrWords As String() = New String() {"bak", "log", "dfd"}
    Dim strOut As String = ""
    Dim strWord As String
    For Each strWord In astrWords
        If strIn.ToLower.IndexOf(strWord.ToLower, 0) >= 0 Then
            strOut = strWord.ToLower
            Exit For
        End If
    Next
    Return strOut
End Function

It's function is to check the input string and see if any of those 'astrWords' are in there and then return the value.

So I wrote a bit of code to dynamically create those words that goes something like this:

Dim extensionArray As String = ""
    Dim count As Integer = 0
    For Each item In lstExtentions.Items
        If count = 0 Then
            extensionArray = extensionArray & """." & item & """"
        Else
            extensionArray = extensionArray & ", ""." & item & """"
        End If
        count = count + 1
    Next
    My.Settings.extensionArray = extensionArray
    My.Settings.Save()

Obviously - it's creating that same array using list items. The output of that code is exactly the same as if I hard coded it - but when I change the first bit of code to: Dim astrWords As String() = New String() {My.Settings.extensionArray} instead of: Dim astrWords As String() = New String() {"bak", "log", "dfd"} It starts looking for the whole statement instead of looping through each individual one?

I think it has something to do with having brackets on the end of the word string - but I'm lost!

Any help appreciated :)

A: 

You need to set extensionArray up as a string array instead of simply a string. Note that

Dim something as String

... defines a single string, but

Dim somethingElse as String()

... defines a whole array of strings.

I think with your code, you need something like:

Dim extensionArray As String() = new String(lstExtensions.Items)
Dim count As Integer = 0
For Each item In lstExtentions.Items
    extensionArray(count) = item
    count = count + 1
Next
My.Settings.extensionArray = extensionArray
My.Settings.Save()

Then at the start of checkString you need something like

Private Function checkString(ByVal strIn As String) As String
    Dim astrWords As String() = My.Settings.extensionArray
    ...

There also might be an even easier way to turn lstExtentions.Items into an Array if Items has a 'ToArray()' method, but I'm not sure what Type you are using there...

codeulike
A: 

What you've done is created a single string containing all 3 words. You need to create an array of strings.

New String() {"bak", "log", "dfd"}

means create a new array of strings containing the 3 strings values "bak", "log" and "dfd".

New String() {My.Settings.extensionArray}

means create a new array of strings containing just one value which is the contents of extensionArray. (Which you have set to ""bak", "log", "dfd""). Note this is one string, not an array of strings. You can't just create 1 string with commas in it, you need to create an array of strings.

If you want to create your array dynamically, you need to define it like this:

Dim astrWords As String() = New String(3)

This creates an array with 3 empty spaces.

You can then assign a string to each space by doing this:

astrWords(0) = "bak"
astrWords(1) = "log"
astrWords(2) = "dfd"

You could do that bit in a for loop:

Dim count As Integer = 0
For Each item In lstExtentions.Items
    astrWords(count) = item
    count = count + 1
Next

Alternatively, you could look at using a generic collection. That way you could use the Add() method to add multiple strings to it

Simon P Stevens
+2  A: 

When you use the string from the settings in the literal array, it's just as if you used a single strings containing the delimited strings:

Dim astrWords As String() = New String() {"""bak"", ""log"", ""dfd"""}

What you probably want to do is to put a comma separated string like "bak,log,dfd" in the settings, then you can split it to get it as an array:

Dim astrWords As String() = My.Settings.extensionArray.Split(","C)
Guffa
You are a genious! This is perfect!Thank you very much - happy coding:)
Blind Trevor
A: 

I think you want your extensionArray to be of type String() and not String. When you are trying to initialize the new array, the initializer doesn't know to parse out multiple values. It just sees your single string.

msergeant