3

I am a newbie

I have problem with string builder. I want to show to Richtextbox in vb with Richtextbox template

i.e.

Jan 674  Meet 670  Missed 4
Feb 635 Meet 631 Missed 4

etc.

with source from datagirdview with 8 columns and xxxx rows.

for ex. columns are : Registered Date, Deadline Date, Month, Meet/Not Meet,etc.

This my Code :

For Each keyvalue As KeyValuePair(Of String, Integer) In DicMonth
 sb.AppendLine(String.Format("{0} : {1}", UCase(keyvalue.Key), keyvalue.Value))
Next

For Each keyvalue1 As KeyValuePair(Of String, Integer) In DicMeetTotal
  sb.AppendLine(String.Format("{0}", "MEET : " & keyvalue1.Value))
Next

RichTextBox2.Text = sb.ToString

and the result is :

Jan : 674
Feb : 635
Mar : 623
Meet : 670
Meet : 631
Meet : 621
Missed : 4
Missed : 4
Missed : 2
Eilon
  • 25,582
  • 3
  • 84
  • 102
Kukuh Sp
  • 29
  • 1
  • Do you know what AppenLine Does ? – CheGueVerra Feb 04 '15 at 04:49
  • There are only 2 dictionaries - where do the values for "missed" come from? (or is it just the difference between the two) Are the keys of all dictionaries the Month? And do the dictionaries have the same number of Key value pairs? – StuartLC Feb 04 '15 at 04:58

1 Answers1

2

Assuming the same order and length of dictionaries, you can use Zip to stitch the two dictionaries together:

Sub Main
    Dim sb = New StringBuilder()
    Dim DicMonth = New Dictionary(Of String, Integer)() From { _
        {"Jan", 674}, _
        {"Feb", 635} _
    }
    Dim DicMeetTotal = New Dictionary(Of String, Integer)() From { _
        {"Jan", 670}, _
        {"Feb", 631} _
    }

    Dim lineStrings = DicMonth.Zip(DicMeetTotal, _
       Function(m, mt) String.Format("{0} {1} Meet {2} Missed {3}", _
           m.Key, m.Value, mt.Value, m.Value - mt.Value))
    For Each ls In lineStrings
        sb.AppendLine(ls)
    Next
    Console.WriteLine(sb.ToString())
End Sub

Alternatively, if there is a join key (e.g. the Key value in both dictionaries is the same), you can use Linq Join them together, like so:

Dim lineStrings = DicMonth.Join(DicMeetTotal, _
     Function(m) m.Key, Function(mt) mt.Key,  _
     Function(m, mt) String.Format("{0} {1} Meet {2} Missed {3}", _
        m.Key, m.Value, mt.Value, m.Value - mt.Value))

Edit

Assuming that you wouldn't have modelled N different dictionaries each containing just a single value (this would be a modelling error along the lines of Entity Attribute Value, IMO), I'm guessing you'll want an entity to hold the data:

Class MeetTotalEntity
   Public Property Meet As Integer
   Public Property Missed As Integer
   Public Property Cancel As Integer
   Public Property Other As Integer
End Class

And then the Zip (or Join) still holds. The Value of the second dictionary contains the above entity, so just dereference the fields accordingly.

Sub Main
     Dim sb = New StringBuilder()
        Dim DicMonth = New Dictionary(Of String, Integer)() From { _
            {"Jan", 674}, _
            {"Feb", 635} _
        }
        Dim DicMeetTotal = New Dictionary(Of String, MeetTotalEntity)() From { _
            {"Jan", New MeetTotalEntity With {.Meet = 670, .Missed = 4, .Cancel = 10, .Other = 5}}, _
            {"Feb", New MeetTotalEntity With {.Meet = 631, .Missed = 10, .Cancel = 3, .Other = 2}} _
        }

        Dim lineStrings = DicMonth.Zip(DicMeetTotal, _
           Function(m, mt) String.Format("{0} Total {1} Meet {2} Missed {3} Cancel {4} Other {5}", _
               m.Key, m.Value, mt.Value.Meet, mt.Value.Missed, mt.Value.Cancel, mt.Value.Other))
        For Each ls In lineStrings
            sb.AppendLine(ls)
        Next
        Console.WriteLine(sb.ToString())
End Sub
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Isn't that a GroupJoin – CheGueVerra Feb 04 '15 at 05:19
  • If OP's dictionaries had missing months, and OP wished to do a Left Outer Join, then a [GroupJoin](http://stackoverflow.com/a/584840/314291) could be used to simulate the LOJ. But if all values are present, a simple inner join will suffice? – StuartLC Feb 04 '15 at 05:22
  • You are right, while I was doing a GroupJoin I understood more your point, I guess that I'm not yet capable of seeing the innner join, in those cases – CheGueVerra Feb 04 '15 at 05:27
  • Thanks Stuart. That's Work. I try to add "Cancel" and "Other" on that function, but not work. Please tell me, how to do it ? – Kukuh Sp Feb 04 '15 at 07:12
  • I'm not following - are Cancel and Other contained in two other Dictionaries, also keyed by Month? If so, you can keep Zipping additional projections. If you use the Join approach, you will need to carry the key across as well after each Join projection. – StuartLC Feb 04 '15 at 07:47
  • it's in DicMeetTotal dictionaries. in Columns : Meet/Missed there is 4 value (Meet,Missed,Cancel,Other) and i would to adding more value (Cancel, Other) in your function to Zip Function but isn't work. Please help me – Kukuh Sp Feb 04 '15 at 09:55