0

im getting an exception on this:

Dim strings_extreme As String()
strings_extreme(0) = ""

it says that i am using it before it is being assigned a value

how do i initialize it?

please note that i need to be able to do this:

strings_extreme = input.Split(","c).Distinct().OrderBy(Function(s) s)
2
  • i dont know how many elements i will need in this area Commented Dec 30, 2009 at 22:14
  • What version of .NET are you using? There's several different ways of doing what you want but some of the better ones are only available in later versions of .NET. Commented Dec 30, 2009 at 22:22

5 Answers 5

6

If you truly don't know how many strings there are going to be, then why not just use an IList:

Dim stringList As IList(Of String) = New List(Of String)
stringList.Add("")

You can get the Count of how many strings there are and you can For Each through all the strings in the list.

EDIT: If you're just trying to build an array from a Split you should be able to do this:

Dim strings_extreme() As String = input.Split(...)
Sign up to request clarification or add additional context in comments.

Comments

3
Dim strings_extreme As List(Of String) = New List(Of String)
strings_extreme.Add("value1")
strings_extreme.Add("value 2")
strings_extreme.Add("value 3rd")
strings_extreme.Add("value again")

Dim strings() As String = strings_extreme.ToArray()

...

6 Comments

I would not use arrays then. Look into Generic List and ArrayList (arraylist ONLY if you do not know the type).
But with ReDim you can enlarge it when you need. Maybe you should try other kind of containers, like List.
you cannot use ReDim within an If-Then statement
I modified the answer to use a List (Generics)
that doesnt work since i need to use this strings_extreme = input.Split(","c).Distinct().OrderBy(Function(s) s)
|
2
Dim strings_extreme(10) As String
strings_extreme(0) = ""

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

1 Comment

Then you don't need an array but rather a list.
2
Dim strings_extreme as String() = {"yourfirstitem"}

But actually, why not take a look at some more advanced data structure from System.Collections namespace?

Comments

0

Shouldn't bother setting a string to "".

Try using:

Dim strings_extreme(10) As String
strings_extreme(yourIndex) = String.Empty

2 Comments

string is not a member of system.array
Doesn't have to be what I meant was strings_extreme(yourIndex) = String.Empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.