1

How can i add 2 and more Strings to an Array list ?

I need it for a Bill List which means i need to add Values to 1 Array Position something like this.

  myarray.AddRange(Article, Price, Value)

and then i need to split these again on other Point ... to get each Value to a single Variable again.

Like

  For each myarray in Array
  Var1 = myarray.Price
  Var2 = myarray.Value
  Next 
1

2 Answers 2

2

what about using a Class ?

Dim aBill as Bill

MyArray.Add(Bill)

 For each bill in MyArray
  Var1 = bill.Price
  Var2 = bill.Value
 Next 



  Public Class Bill        
    Property Article() As String          
    Property Price() As String
    Property Value() As String
  End Class
Sign up to request clarification or add additional context in comments.

4 Comments

? ... this answer isnt much helpfull ?
oh yes it is... you should use a class instead of arrays of strings , if your 3 strings is depending on each other , using position in an array is realy bad practice
then i guess iam totally confused .. can you explain a bit more ?
1

How can i add 2 and more Strings to an Array list ?

Using an Array of Strings for the item that you are adding in the ArrayList, with the ArrayList.Add() method.

An example:

Dim arrlist As New ArrayList
arrlist.Add({"string1", "string2", "string3"})

For Each item As String() In arrlist
    Console.WriteLine(String.Join(", ", item))
Next item

and then i need to split these again on other Point ... to get each Value to a single Variable again.

You could resolve both needs in several ways, in the example above you can just "cast" the items to a tuple or an Anonymous Type using LINQ, but a proper way would be changing the way that you are doing things then define a custom Type with two properties (Price, Value, maybe Article, etc) as @Thorarins mentioned, but as you are not friendly with that concept then to simplify things I suggest you to avoid ArrayLists usage for a generic List(T) where T in this case its a KeyValuePair(Of String, String) (you can use a tuple instead if need more than 2 strings).

An example:

Dim pricesList As New List(Of KeyValuePair(Of String, String))
pricesList.AddRange({New KeyValuePair(Of String, String)("$5", "1"),
                     New KeyValuePair(Of String, String)("$3", "2")})

For Each item As KeyValuePair(Of String, String) In pricesList
    Console.WriteLine(String.Format("Price: {0}, Value {1}", item.Key, item.Value))
Next item

Note that maybe you will preffer to use a proper datatype than String for numeric values, such as Integer.

1 Comment

Thanks for your good Answer :) the Secound Example is right that what i want :) But how can i add now another Valuue ? ... cause it is saying to many Arguments ?

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.