0
While i < n
            array(i) = New System.Drawing.Point(points(i).X, points(i).Y)
            res = (array(i).ToString)
            Debug.Print("Res: " & res)
            i += 1
        End While

This is the code I have...

My output is-

Res: {X=1209,Y=67}
Res: {X=1224,Y=66}
Res: {X=1225,Y=82}
Res: {X=1209,Y=82}
Res: {X=40,Y=83}
Res: {X=41,Y=68}
Res: {X=56,Y=68}
Res: {X=54,Y=84}
Res: {X=40,Y=1054}
Res: {X=41,Y=1040}
Res: {X=56,Y=1040}
Res: {X=55,Y=1056}
Res: {X=1208,Y=1057}
Res: {X=1209,Y=1042}
Res: {X=1224,Y=1042}
Res: {X=1224,Y=1057}

But I want this like-

}{X=1209,Y=67}{X=1224,Y=66}{X=1225,Y=82}{X=1209,Y=82}{X=40,Y=83}{X=41,Y=68}{X=56,Y=68}{X=54,Y=84}{X=40,Y=1054}{X=41,Y=1040}{X=56,Y=1040}{X=55,Y=1056}{X=1208,Y=1057}{X=1209,Y=1042}{X=1224,Y=1042}{X=1224,Y=1057}{

in a single string variable. And that variable has to be assigned only once (The outputs actually came in four times. I meant the loop was triggered 4 times). I can't assign value more than once for this particular variable in a single event. Which means, no matter how many times the loop works for an event, I need all the outputs to be included in the string variable at once.

Now, can I get some help, please? :(

[NB: Number of Outputs can vary.]

nsssayom
  • 364
  • 1
  • 5
  • 21

1 Answers1

0

You can use string.Join together with a linq expression

Dim result = String.Join(" ", array.Select(Function(p) "Res: " & p.ToString()).ToList())

(This goes outside the loop)

The problem with your specification is that when you call your looping code you don't know anything of the future or previous calls. Then you need a place where to store the intermediate values and retrieve them when you have finished with the loops

This could be resolved only with a class global level variable. Suppose you have a class named PointProcessor

 Public Class PointProcessor

    ' This will store the intermediate results and 
    ' give back them through the property
    Dim _processingData = new List<string>()



    Public Sub ProcessData(n As Integer)
        Dim i As Integer = 0           
        While i < n

           ' The variables _array_ and _points_ are assumed to be also
           ' global class level variables set by your code sometime before
           ' calling this sub.

            array(i) = New System.Drawing.Point(points(i).X, points(i).Y)
            i += 1
        End While

        ' Store the result of this loop in the global class variable
        _processingData.Add(String.Join(" ", array.Select(Function(p) "Res: " & p.ToString()).ToList()))
    End Sub

    ' Give back all the result creating a single string
    Public Property ProcessingResult as String
         Get
             return string.Join(" ", _processingData)
         End Get
    End Property
 End Class
Steve
  • 213,761
  • 22
  • 232
  • 286