none
IF TRUE THEN RRS feed

  • Question

  • Hi All

    I'm studing the following code ..... to drqw a line in a Datagridview in a prepaint Event

    Private Sub PaintGroupRow(ByVal e As DataGridViewRowPrePaintEventArgs) Dim grouprow = CType(source(e.RowIndex), GroupRow) If Not selectionset Then setselection() End If Dim info = grouprow.GetDisplayInfo(selectedGroups.Contains(e.RowIndex)) If info Is Nothing OrElse info.Cancel Then Return If info.Font Is Nothing Then info.Font = e.InheritedRowStyle.Font Dim r = e.RowBounds r.Height -= 1 Using bgb = New SolidBrush(info.BackColor) e.Graphics.DrawLine(Pens.SteelBlue, r.Left, r.Bottom, r.Right, r.Bottom) If True Then Dim l As Integer = HeaderOffset + 1 r.X = l - grid.HorizontalScrollingOffset e.Graphics.FillRectangle(bgb, r) .... .... End if

    The question is : What boolean field the statement IF TRUE THEN ......is checking  ? 

    Wednesday, August 29, 2018 11:59 AM

Answers

  •  It appears that the set of braces that is having that 'If True Then' added to it in the conversion is doing nothing at all in your c# code.  So,  either the writer of the code,  or you copying it got it screwed up somehow?  If that is how the code actually appears from where you got it and the code actually functions as it should,  then just remove the 'If True Then' and it's 'End If' from your VB.Net code.

     Perhaps the writer of the code once had an If Then to check some condition before executing the code inside those braces but,  then removed it and forgot to remove the matching braces.  Hard to tell why they are there.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, August 30, 2018 8:25 PM
    • Marked as answer by Claudio111 Friday, August 31, 2018 7:23 AM
    Thursday, August 30, 2018 8:23 PM

All replies

  • Ultimately I believe it is checking if True is True. I bet it returns True

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Wednesday, August 29, 2018 6:44 PM
  • That statement is checking a Boolean Data Type: True, which will always return the value true.  I understand your frustration, if it is always true, then why is that statement there?
    Wednesday, August 29, 2018 7:45 PM
  • I really do not know

    It is a C# to VB convertion .

    This the original C# code that I do not Understand

     using (var bgb = new SolidBrush(info.BackColor))
                {
                    //line under the group row
                    e.Graphics.DrawLine(Pens.SteelBlue, r.Left, r.Bottom, r.Right, r.Bottom);
    
    
    /// here the CONVERTER add  IF TRUE THEN
                    //group value
                    {
                        int l = HeaderOffset + 1;
                        r.X = l - grid.HorizontalScrollingOffset;
                        
    
                        //clear background
                        e.Graphics.FillRectangle(bgb, r);
    
                        using (var fb = new SolidBrush(info.ForeColor))
                        {
                            var sf = new StringFormat { LineAlignment = StringAlignment.Center };
                            if (info.Header != null)
                            {
                                var size = e.Graphics.MeasureString(info.Header, info.Font);
                                e.Graphics.DrawString(info.Header, info.Font, fb, r, sf);
                                r.X += (int)size.Width + 5;
                            }
    
                            if (info.DisplayValue != null)
                            {
                                using (var f = new Font(info.Font.FontFamily, info.Font.Size + 2, FontStyle.Bold))
                                {
                                    var size = e.Graphics.MeasureString(info.DisplayValue, f);
                                    e.Graphics.DrawString(info.DisplayValue, f, fb, r, sf);
                                    r.X += (int)size.Width + 10;
                                }
                            }
                            if (info.Summary != null)
                            {
                                e.Graphics.DrawString(info.Summary, info.Font, fb, r, sf);
                            }
                        }
                        
    
                        e.Graphics.FillRectangle(bgb, 0, r.Top, l, r.Height);
                            
                    }
                }

    Thursday, August 30, 2018 6:47 PM
  •  It appears that the set of braces that is having that 'If True Then' added to it in the conversion is doing nothing at all in your c# code.  So,  either the writer of the code,  or you copying it got it screwed up somehow?  If that is how the code actually appears from where you got it and the code actually functions as it should,  then just remove the 'If True Then' and it's 'End If' from your VB.Net code.

     Perhaps the writer of the code once had an If Then to check some condition before executing the code inside those braces but,  then removed it and forgot to remove the matching braces.  Hard to tell why they are there.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, August 30, 2018 8:25 PM
    • Marked as answer by Claudio111 Friday, August 31, 2018 7:23 AM
    Thursday, August 30, 2018 8:23 PM
  • It makes sense. The matching braces are garbage

    Thanks

    Friday, August 31, 2018 7:23 AM
  • There is one thing the if-true block achieves.  It makes variables declared inside the block local to the block.  So, when code execution passes through the end-if, variable 'l as integer' goes out of scope and ceases to exist.  This is not very important for integer 'l', but a large array or some other memory expensive object will be released when execution passes the end-if.

    So, 'If' blocks (if-true or otherwise) have a similarity with Using blocks.  With Using, an object is created, used, disposed, and destroyed.  In an If block, any internally declared objects are created, used, and destroyed without dispose().  The Using construct can't be used if IDisposable is not implemented for these objects.

    Comparing to C,

    if-true-then code end-if

    is like

    { code }.


    Friday, August 31, 2018 1:31 PM