none
How to create a "Function that returns an Array of a List(Of Double)" ? RRS feed

  • Question

  • Dear Friends,

    In my program, I get a "List" of 'Planetary Positions' (in Degree - a Double) and another "List" of 'Corresponding Date' (in 'Julian Day number' - a Double). The number of elements in the "List" is not fixed; it may be 10 or may be 200.

    Is it possible to create an Array that has 2 elements, each is a List(Of Double) ?

    If it is possible, I want to create a Function that returns two List(Of Double).  Is it possible? I couldn't get any help on googling.

    Please help me. 

    Thanks

    Kind regards

    VKSBK



    A Real Novice Programmer !

    vendredi 23 octobre 2020 17:40

Réponses


  • However, I still like to know whether a "Function" that returns a "List(Of Double)" that has "2 List(Of Double)", similar to a "2-Dimentional Array"? 
    To say it another way, A List(Of Double) that has 2 elements each is a List(Of Double).

    If it is possible how do you declare it? is it a "List(Of List(Of Double))" ????

    Hi

    You want a List(Of List(Of Double))   -   that is perfectly OK,  but then you gp on to say you want to assign 2 x List(Of Double)  or,  you want a List(Of List(Of List(Of Double)) I think?

    Something along the lines of

    		' I think this is what you are asking for
    		Dim lst As New List(Of List(Of List(Of Double)))
    
    		' to get there, crate some List(Of Double)
    		Dim ld1, ld2, ld3, ld4, ld5, ld6 As New List(Of Double)
    
    		ld1.AddRange({1.2, 1.3, 1.4})
    		ld2.AddRange({2.2, 2.3, 2.4})
    		ld3.AddRange({3.2, 3.3, 3.4})
    		ld4.AddRange({4.2, 4.3, 4.4})
    		ld5.AddRange({5.2, 5.3, 5.4})
    		ld6.AddRange({6.2, 6.3, 6.4})
    
    		' create some  List(Of List(OfDouble)
    		Dim ldd1 As New List(Of List(Of Double))
    		Dim ldd2 As New List(Of List(Of Double))
    		Dim ldd3 As New List(Of List(Of Double))
    
    		' assign pairs of List(Of Double)
    		ldd1.AddRange({ld1, ld2})
    		ldd2.AddRange({ld3, ld4})
    		ldd3.AddRange({ld5, ld6})
    
    		' add the paired Lists to the main list
    		lst.AddRange({ldd1, ldd2, ldd3})
    
    		' so now you have what was asked for (as best
    		' as I can understand ir.

    Here you end up with a List of pairs of List(Of Double)

    *

    I do feel you are making things very very difficult for yourself when using a Class would simpify everything.



    Regards Les, Livingston, Scotland


    • Modifié leshay mardi 27 octobre 2020 18:45
    • Marqué comme réponse V.K.S.B.K mardi 27 octobre 2020 22:41
    mardi 27 octobre 2020 18:41
  • Hi

    Don't worry about your English - it is very good.

    Here is a way to create a List containing 10 pairs of List(Of Double), and accessing the members.

    It uses a Class - by far the better way to do it (in my opinion)

    *

    You seem to have a resistance to anything other than arrays, but if you look at this code you will see it as actually much more simple than using arrays.

    	Class DoublePair
    		Property One As List(Of Double)
    		Property Two As List(Of Double)
    	End Class
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		' create a List of 10 Pairs of Doubles
    		Dim lst As New List(Of DoublePair)
    		For i As Integer = 1 To 10
    			lst.Add(New DoublePair With {.One = New List(Of Double)({i / 3, i / 5, i / 7}), .Two = New List(Of Double)({i / 9, i / 11, i / 17})})
    		Next
    
    		' access the members
    		Dim l1 As List(Of Double) = lst(3).One
    		Dim l2 As List(Of Double) = lst(8).Two
    
    		Dim d1 As Double = l1(0) ' = 1.3333333333333333
    		Dim d2 As Double = l1(2) ' = 0.5714285714285714	
    	End Sub



    Regards Les, Livingston, Scotland

    • Marqué comme réponse V.K.S.B.K mardi 27 octobre 2020 22:41
    mardi 27 octobre 2020 20:47

Toutes les réponses

  • Usually it's common for you to try then when the code doesn't work show the code and indicate where you are stuck.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    vendredi 23 octobre 2020 17:57
    Modérateur
  • Hi

    Although I agree with Karen, here is some code which is only a pointer that may be of some help.

    You don't really describe what you want - for example, you don't indicate by which means you want the function to choose the list(s).

    Anyway, here is one interpretation of what may be needed.

    This is a stand alone example.  It needs to generate sample data where you already have data lists so most of this code is superfluous. I used a Class for the List pairs.

    It shows how to get 2 lists using an index into a collection of List pairs (Combined ). I chose to use List(Of ...  without your requested Array (it could be made using an Array but a List is much more usefull for many things.

    OK then, here is the example code.  There is no UI so you can just hover/examine the values when reaching the STOP command.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim rand As New Random
    	Dim Combined As New List(Of Master)
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		For i As Integer = 0 To 9
    			Dim d1, d2 As New List(Of Double)
    			For j As Integer = 0 To 8
    				d1.Add(rand.NextDouble * rand.Next(3, 33))
    				d2.Add(rand.NextDouble * rand.Next(3, 33))
    			Next
    			Dim m As New Master With {.List1 = d1, .List2 = d2}
    			Combined.Add(m)
    		Next
    
    		' a couple of valid caLLS
    		Dim TwoDoubles1 As Master = Get2Lists(3)
    		Dim TwoDoubles2 As Master = Get2Lists(8)
    
    		' a couple of invalid calls
    		Dim TwoDoubles3 As Master = Get2Lists(-1)
    		Dim TwoDoubles4 As Master = Get2Lists(11)
    
    		Stop ' hover over results to see them
    	End Sub
    	Function Get2Lists(inx As Integer) As Master
    		If inx < 0 Or inx > Combined.Count - 1 Then Return New Master
    		Return Combined(inx)
    	End Function
    	Class Master
    		Property List1 As New List(Of Double)
    		Property List2 As New List(Of Double)
    	End Class
    End Class


    Regards Les, Livingston, Scotland


    • Modifié leshay vendredi 23 octobre 2020 18:11
    vendredi 23 octobre 2020 18:10
  • Hi leshay,

    Thanks for the reply.

    I think I didn't express myself clearly in my 1st post.

    I just want to know whether it's possible to create an Array that has 2 elements, each is a List(Of Double)? and if it is possible, I want to know how to declare that 2 element Array of List(Of Double).

    And also, I want to know whether it is possible to create a Function that returns two List(Of Double). 

    I hope it is possible to create a function if an Array of a List(Of Double) can be created.

    Hope now it is clear what I want to know.

    Thanks

    Kind regards

    VKSBK



    A Real Novice Programmer !

    vendredi 23 octobre 2020 18:50
  • Hi leshay,

    Thanks for the reply.

    I think I didn't express myself clearly in my 1st post.

    I just want to know whether it's possible to create an Array that has 2 elements, each is a List(Of Double)? and if it is possible, I want to know how to declare that 2 element Array of List(Of Double).

    And also, I want to know whether it is possible to create a Function that returns two List(Of Double). 

    I hope it is possible to create a function if an Array of a List(Of Double) can be created.

    Hope now it is clear what I want to know.

    Thanks

    Kind regards

    VKSBK



    A Real Novice Programmer !

    Hi

    Do you have to use arrays?


    Regards Les, Livingston, Scotland

    vendredi 23 octobre 2020 18:52
  • Dear leshay,

    Yes, I prefer to use Arrays. I am comfortable with it; easy to iterate, easy to find an element.

    I am comfortable with "List" too;  I don't like Dictionary; difficult to find an element, I mean it doesn't use "Index" which is a "Must" in my program. Read about Dictionary & tried few months ago, didn't suit to my "program"; never tried other types of the "Collections" except few trial on "ArrayList".

    I don't mind learning "how to create a List(Of list(Of double))", but for the current one I like to use Arrays of List(Of Double).

    Thanks

    Regards

    VKSBK


    A Real Novice Programmer !

    vendredi 23 octobre 2020 20:22
  • Hi

    OK, here is my previous code changed to accomodate an Array instead of a List.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim rand As New Random
    	Dim Combined(0) As Master
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		For i As Integer = 0 To 9
    			Dim d1, d2 As New List(Of Double)
    			For j As Integer = 0 To 8
    				d1.Add(rand.NextDouble * rand.Next(3, 33))
    				d2.Add(rand.NextDouble * rand.Next(3, 33))
    			Next
    			Dim m As New Master With {.List1 = d1, .List2 = d2}
    			Dim currsize As Integer = Combined.Length
    			Combined(currsize - 1) = m
    			ReDim Preserve Combined(currsize)
    		Next
    
    		' a couple of valid caLLS
    		Dim TwoDoubles1 As Master = Get2Lists(3)
    		Dim TwoDoubles2 As Master = Get2Lists(8)
    
    		' a couple of invalid calls
    		Dim TwoDoubles3 As Master = Get2Lists(-1)
    		Dim TwoDoubles4 As Master = Get2Lists(11)
    
    		Stop ' hover over results to see them
    	End Sub
    	Function Get2Lists(inx As Integer) As Master
    		If inx < 0 Or inx > Combined.Count - 1 Then Return New Master
    		Return Combined(inx)
    	End Function
    	Class Master
    		Property List1 As New List(Of Double)
    		Property List2 As New List(Of Double)
    	End Class
    End Class

    Regards Les, Livingston, Scotland


    • Modifié leshay vendredi 23 octobre 2020 21:04
    vendredi 23 octobre 2020 21:04
  • Dear leshay,

    Thanks for the code; as I am sleepy now, I will try this tomorrow.

    Thanks

    Kind regards

    VKSBK


    A Real Novice Programmer !

    vendredi 23 octobre 2020 21:35
  • Hi

    Seems you have made a further reply - I got an email saying so.  However, as is often the case, this forum has thrown a wobbly and has decided not to show your post in this thread.  This post is just to say I am not ignoring you.

    If your post ever does show up here, I will respond.


    Regards Les, Livingston, Scotland


    • Modifié leshay dimanche 25 octobre 2020 22:31
    dimanche 25 octobre 2020 17:29

  • I just want to know whether it's possible to create an Array that has 2 elements, each is a List(Of Double)? and if it is possible, I want to know how to declare that 2 element Array of List(Of Double).


    Yes, it's possible.

    Seems rather straight forward. With what part of this task are you having
    problems?

    Dim MyArray(1) As List(Of Double)
    Dim MyList1, MyList2 As New List(Of Double)
    
    MyList1.Add(123.4)
    MyList1.Add(567.8)
    
    MyList2.Add(987.6)
    MyList2.Add(543.2)
    
    MyArray(0) = MyList1
    MyArray(1) = MyList2
    

    - Wayne

    lundi 26 octobre 2020 03:58
  • Dear WayneAKing & leshay,

    leshay - Sorry, Yesterday I was busy trying to solve this problem.

    Thanks for the help; I have already figured out how to do what I wanted.

    Initially, I wanted to create a Function that returns a List(Of Double) that has 2 List(Of Double) collections; as I couldn't figure out how to "declare that function" I worked out a Function that returns a 2-Dimensional Array of Double.

    Since the number of Planetary Positions and its corresponding Dates (Julian Day Number) are not known, I found it easy to use List(Of Double) to save them as mentioned below;

    Dim PositionsList, DateList as New List(Of Double).

    Then, I declared 2 Dynamic Arrays to assign each of these List(Of Double) to.

    Finally, I assigned each of these "Dynamic Arrays" to the 2-Dimensional Array of the Function.

    The function is working without any problem.

    ==================================

    However, I still like to know whether a "Function" that returns a "List(Of Double)" that has "2 List(Of Double)", similar to a "2-Dimentional Array"? 
    To say it another way, A List(Of Double) that has 2 elements each is a List(Of Double).

    If it is possible how do you declare it? is it a "List(Of List(Of Double))" ????

    I did a Trial as given below; but couldn't complete it.

    Function ListOf_ListOfInteger_Maker() As List(Of List(Of Double))
    
            Dim _1st_RandomInteger, _2nd_RandomInteger As Integer
            Dim _1st_RandomNumber_List, _2nd_RandomNumber_List As New List(Of Integer)
    
            Dim ListOfListOfInteger As New List(Of List(Of Integer)) ' Supposed to be a List that contains 2 elements, each is a List(Of Integer).
    
            For i = 0 To 10
                _1st_RandomInteger = (Int(Rnd() * 10))
                _2nd_RandomInteger = (Int(Rnd() * 100))
    
                _1st_RandomNumber_List.Add(_1st_RandomInteger)
                _2nd_RandomNumber_List.Add(_2nd_RandomInteger)
    
                ListOfListOfInteger(0).Add(_1st_RandomInteger)
                ListOfListOfInteger(1).Add(_2nd_RandomInteger)
            Next i
    
            ' ListOf_ListOfInteger_Maker = ??????? How to assign the 2 List(Of Integer)???
    
        End Function

    I am NOT sure whether "List(Of List(Of Integer)" is possible. If it is okay,  how do I get the Return?; here I want the Results assigned to "ListOf_ListOfInteger_Maker".

    Visual Studio doesn't show any "error" for the following 2 lines of code;

     ListOfListOfInteger(0).Add(_1st_RandomInteger)
     ListOfListOfInteger(1).Add(_2nd_RandomInteger)
    So, I assume they are "legal"

    Can you please let me know how to complete this function?

    Thanks

    Kind regards

    VKSBK



    A Real Novice Programmer !

    mardi 27 octobre 2020 18:14

  • However, I still like to know whether a "Function" that returns a "List(Of Double)" that has "2 List(Of Double)", similar to a "2-Dimentional Array"? 
    To say it another way, A List(Of Double) that has 2 elements each is a List(Of Double).

    If it is possible how do you declare it? is it a "List(Of List(Of Double))" ????

    Hi

    You want a List(Of List(Of Double))   -   that is perfectly OK,  but then you gp on to say you want to assign 2 x List(Of Double)  or,  you want a List(Of List(Of List(Of Double)) I think?

    Something along the lines of

    		' I think this is what you are asking for
    		Dim lst As New List(Of List(Of List(Of Double)))
    
    		' to get there, crate some List(Of Double)
    		Dim ld1, ld2, ld3, ld4, ld5, ld6 As New List(Of Double)
    
    		ld1.AddRange({1.2, 1.3, 1.4})
    		ld2.AddRange({2.2, 2.3, 2.4})
    		ld3.AddRange({3.2, 3.3, 3.4})
    		ld4.AddRange({4.2, 4.3, 4.4})
    		ld5.AddRange({5.2, 5.3, 5.4})
    		ld6.AddRange({6.2, 6.3, 6.4})
    
    		' create some  List(Of List(OfDouble)
    		Dim ldd1 As New List(Of List(Of Double))
    		Dim ldd2 As New List(Of List(Of Double))
    		Dim ldd3 As New List(Of List(Of Double))
    
    		' assign pairs of List(Of Double)
    		ldd1.AddRange({ld1, ld2})
    		ldd2.AddRange({ld3, ld4})
    		ldd3.AddRange({ld5, ld6})
    
    		' add the paired Lists to the main list
    		lst.AddRange({ldd1, ldd2, ldd3})
    
    		' so now you have what was asked for (as best
    		' as I can understand ir.

    Here you end up with a List of pairs of List(Of Double)

    *

    I do feel you are making things very very difficult for yourself when using a Class would simpify everything.



    Regards Les, Livingston, Scotland


    • Modifié leshay mardi 27 octobre 2020 18:45
    • Marqué comme réponse V.K.S.B.K mardi 27 octobre 2020 22:41
    mardi 27 octobre 2020 18:41
  • Dear leshay,

    Thanks for the help.

    May be, I didn't write it correctly; 

    What I really want to know & do is a "List" collection that has 2 list(Of Double). I think it would be like a 2-Dimensional Array. And I want to know the syntax for declaring it.

    The " is it a "List(Of List(Of Double))" ????" given in the picture above is my guess only; it need not to be that.

    Please let me know what is the CORRECT one for "List" collection that has 2 Elements each one is a list(Of Double).
     
    I hope I have explained it clearly; if it is still unclear.. pardon me; English is not my mother tongue.

    Thanks

    Kind regards

    VKSBK


    A Real Novice Programmer !


    • Modifié V.K.S.B.K mardi 27 octobre 2020 20:03
    mardi 27 octobre 2020 19:52
  • Dear leshay,

    I am aware of "Structure" - User-defined Data Type; and know it can be used to create an Array (of User-defined Data type) instead of creating a 2-Dimensional Array.  I wanted to know & learn about new things that is NOT given in books I have ( "Microsoft Visual Basic Step by Step" & Mastering Visual Basic 2008") and NOT given in "https://docs.microsoft.com/".

    Hope you can understand.

    Regards

    VKSBK


    A Real Novice Programmer !

    mardi 27 octobre 2020 20:35
  • Hi

    Don't worry about your English - it is very good.

    Here is a way to create a List containing 10 pairs of List(Of Double), and accessing the members.

    It uses a Class - by far the better way to do it (in my opinion)

    *

    You seem to have a resistance to anything other than arrays, but if you look at this code you will see it as actually much more simple than using arrays.

    	Class DoublePair
    		Property One As List(Of Double)
    		Property Two As List(Of Double)
    	End Class
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		' create a List of 10 Pairs of Doubles
    		Dim lst As New List(Of DoublePair)
    		For i As Integer = 1 To 10
    			lst.Add(New DoublePair With {.One = New List(Of Double)({i / 3, i / 5, i / 7}), .Two = New List(Of Double)({i / 9, i / 11, i / 17})})
    		Next
    
    		' access the members
    		Dim l1 As List(Of Double) = lst(3).One
    		Dim l2 As List(Of Double) = lst(8).Two
    
    		Dim d1 As Double = l1(0) ' = 1.3333333333333333
    		Dim d2 As Double = l1(2) ' = 0.5714285714285714	
    	End Sub



    Regards Les, Livingston, Scotland

    • Marqué comme réponse V.K.S.B.K mardi 27 octobre 2020 22:41
    mardi 27 octobre 2020 20:47
  • Dear leshay,

    I am aware of "Structure" - User-defined Data Type; and know it can be used to create an Array (of User-defined Data type) instead of creating a 2-Dimensional Array.  I wanted to know & learn about new things that is NOT given in books I have ( "Microsoft Visual Basic Step by Step" & Mastering Visual Basic 2008") and NOT given in "https://docs.microsoft.com/".

    Hope you can understand.

    Regards

    VKSBK


    A Real Novice Programmer !

    Hi

    A lot of the contents of a book of that age is 'probably' well out of date.  For example, Structures are seldom used now. In those days, Arrays and Structures were more prominent but even then were being used minimally.

    *

    I was actually expecting you to mention multidimensioned arrays. Such as:

    		Dim ma(9, 1) As List(Of Double)
    		For i As Integer = 0 To 9
    			ma(i, 0) = New List(Of Double)({i / 3, i / 5, i / 7})
    			ma(i, 1) = New List(Of Double)({i / 9, i / 11, i / 17})
    		Next
    
    		' access members
    		Dim lst1 As List(Of Double) = ma(2, 0)
    		Dim lst2 As List(Of Double) = ma(7, 1)
    
    		Dim db1 As Double = lst1(2) ' = 0.2857142857142857
    		Dim db2 As Double = lst2(0) ' = 0.77777777777777779


    Regards Les, Livingston, Scotland


    • Modifié leshay mardi 27 octobre 2020 21:24
    mardi 27 octobre 2020 21:16

  • Dear leshay,

    I couldn't find any very latest edition of "Mastering Visual Basic....".  Also, couldn't find any books on advanced programming in VB.Net. 

    Do you have any suggestions?

    I studied about "Class" when I read the book "MS Visual Basic Step by Step", never used; I didn't have any opportunity for it. 

    Since I am learning & doing VB.Net programming as a hobby just to create an Astrology program, I didn't have the chance to use "Class" / didn't know where to use it.  

    Now, as you mentioned here is a chance to use it; let me try.

    Thanks for teaching me.

    Kind regards

    VKSBK


    A Real Novice Programmer !

    mardi 27 octobre 2020 22:57

  • Dear leshay,

    I couldn't find any very latest edition of "Mastering Visual Basic....".  Also, couldn't find any books on advanced programming in VB.Net. 

    Do you have any suggestions?

    I studied about "Class" when I read the book "MS Visual Basic Step by Step", never used; I didn't have any opportunity for it. 

    Since I am learning & doing VB.Net programming as a hobby just to create an Astrology program, I didn't have the chance to use "Class" / didn't know where to use it.  

    Now, as you mentioned here is a chance to use it; let me try.

    Thanks for teaching me.

    Kind regards

    VKSBK


    A Real Novice Programmer !

    Hi

    I can't recommend a book as I have never read one (programming that is 😊) I am only a hobbiest as well, and what little I know, it has been only from this forum and MS Docs. (the f1 key was worn away)

    I did once write an astrology program for my wife, but that was in good old (BBC)Acorn Basic on a RISC machine - and, it was many many years ago.


    Regards Les, Livingston, Scotland




    • Modifié leshay mardi 27 octobre 2020 23:45
    mardi 27 octobre 2020 23:01