-2

I have a class like this

Class Myclass
  itemdata as string
  name as string
End Class

how do i initialised this class with and array of string for both properties?

this is what i am trying, which os obviously wrong

Dim ls As New List(Of Myclass)(New Myclass() {("A1,A2,A3,A4".Split(","))})

I need something like this after initialization, the value of each item in the list will be like as if assigned manually like this

List(0).itemdata="A1"
List(0).name="A1"
List(1).itemdata="A2"
List(2).name="A2"

etc

Smith
  • 5,765
  • 17
  • 102
  • 161

2 Answers2

1

To start with Myclass is a reserved name, so I've used Myclass2.

This is the closest to you code that I can make it:

Dim dicOpts = New Dictionary(Of String, String) From {{"foo", "bar,woo"}}
Dim key = "foo"
Dim ls As New List(Of Myclass2) From { New Myclass2() With { .itemdata = dicOpts(key).Split(","c)(0), .name = dicOpts(key).Split(","c)(1) } }

That gives:

result


Based on your edits I think this is closer to what you want:

Dim text = "A1,A2,A3,A4"
Dim ls = _
    text _
        .Split(","c) _
        .Select(Function (x) New Myclass2() With { .itemdata = x, .name = x }) _
        .ToList()

I now get this:

result2

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
-1

i've renamed your class(es)....hope u understand what u mean with it:

Create your Class:

Class xxx '(this was your 'myclass')
    Private _p1 As String
    Private _p2 As String
    Private _p3 as String

    Sub New(p1 As String, p2 As String)
        ' TODO: Complete member initialization 
        _p1 = p1
        _p2 = p2
    End Sub
    Sub New(p1 As String, p2 As String, p3 as String)
        ' TODO: Complete member initialization 
        _p1 = p1
        _p2 = p2
        _p3 = p3
    End Sub
End Class

i'd changed the SubNew-Method with 2 overloads...now u need to call the instance of the class with parameters (or without):

Class yyy
    Private lf As New List(Of xxx)
    Sub CallOrWhatEver()
        lf.Add(New xxx("itemdata", "name"))
        lf.Add(New xxx("p1value","p2value","p3value"))
    End Sub
End Class
oShortyo
  • 1
  • 2