none
String Processing with loop RRS feed

  • Question

  • 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. I can't assign value more than once for this particular variable in a single event. Now, how to do that?


    • Edited by Sayom Shakib Thursday, February 23, 2017 9:48 PM
    Thursday, February 23, 2017 9:21 PM

Answers

  • 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. I can't assign value more than once for this particular variable in a single event. Now, how to do that?

            Dim StrPoints(n) As String
            For I As Integer = 0 To n
                StrPoints(I) = "{X=" & points(I).X.ToString & ",Y=" & points(I).Y.ToString & "}"
            Next
            Dim Res As String = "}" & String.Join("", StrPoints) & "{"

    If you are required to only assign a value to the string variable once only, then there is a problem with your application design.

    • Marked as answer by Sayom Shakib Friday, February 24, 2017 7:17 AM
    Thursday, February 23, 2017 11:35 PM

All replies

  • Very confusing.

    First off how did you get the output?

    Second the numbers in your output do not match what you want.

    If you want to create a single string first create a StringBuilder, then for each item you want to put to the string use the Append method of the StringBuilder.  When you are finished with adding info use the ToString() method of the StringBuilder to get a single string.


    Lloyd Sheen

    Thursday, February 23, 2017 9:33 PM
  • I am sorry. Now the code and Outputs are correct. 
    Thursday, February 23, 2017 9:48 PM
  • I am sorry. Now the code and Outputs are correct. 

    Hi

    As best as I can understand your question - is this what you want?

    I set up an array of 20 points for testing.

            ' just to set up sone random points
            Dim k As Integer = 20
            Dim points(k) As Point
            For j As Integer = 0 To k
                points(j) = New Point(rand.Next(800, 1200), rand.Next(800, 1200))
            Next
    
            Dim res As String = Nothing
            Dim i As Integer = 0
            While i < points.Length
                '        points(i) = New Point(points(i).X, points(i).Y)
                res = (points(i).ToString)
                Console.Write(res)
                i += 1
            End While
    


    Regards Les, Livingston, Scotland

    Thursday, February 23, 2017 11:33 PM
  • 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. I can't assign value more than once for this particular variable in a single event. Now, how to do that?

            Dim StrPoints(n) As String
            For I As Integer = 0 To n
                StrPoints(I) = "{X=" & points(I).X.ToString & ",Y=" & points(I).Y.ToString & "}"
            Next
            Dim Res As String = "}" & String.Join("", StrPoints) & "{"

    If you are required to only assign a value to the string variable once only, then there is a problem with your application design.

    • Marked as answer by Sayom Shakib Friday, February 24, 2017 7:17 AM
    Thursday, February 23, 2017 11:35 PM