Method without any code

Asked

Viewed 78 times

-1

Following a book, I created a project windows forms application. I put in the form one textBox and a button. In the onclick of button taken the value of textBox and the attribute to the text of form.

The book asks you to look at the method InitializeComponents() of form. So I came across the method SuspendLayout(). I went to the method to see what he was doing and there was no code in it. But researching, from what I understand, when it is added several controls (I understand how buttons, textBox, etc.) the SuspendLayout() takes care to assemble the layout after processing all of them, making the application perform better. What I find strange is the method not having code, where is this code after all? It is not the first time that I enter a method and there is nothing inside.

partial class frmMain
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(105, 95);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(90, 67);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 22);
            this.textBox1.TabIndex = 1;
            // 
            // frmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(282, 253);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "frmMain";
            this.Text = "frmMain";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
    }
  • Give more context, show code. It’s a class partial?

  • I do not know what is partial, I will search. But I added the code in the post.

  • Where is the empty method?

  • 1

    Where did you look at the code of this method? It is part of . Net Framework, more specifically from the Control class of Windowsforms, the source code you can see here: https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/Winforms/Control.Cs,12393

  • I think his doubt is when we navigate through some method/object of framework(F12 in VS) and it is shown what that framework implements

  • 3

    If it is with F12 by default VS.Net shows only the metadata, then only the method names appear and not the source code

  • What I don’t understand is a method of doing something, in the case of Suspendlayout(), and having no code inside.

  • @Heyjoe look at the link I passed in my first comment

  • @Maniero the Suspendlayout() method is within the Initializecomponents() method, I put the code in the post. And this Suspendlayout() method is in the public class Control class.

  • Also I don’t know what metadata is. I saw the link, it seems that the codes are there on that site, so VS searches the codes there? Now it makes sense.

  • No, inside the InitializeComponents() is just calling the SuspendLayout(), the method itself is not there.

  • @Maniero yes, that’s what I meant, but I didn’t put it in the right words.

Show 7 more comments

2 answers

2

When you say that the method has no code, I imagine you must have navigated to the method definition using Visual Studio (F12 in the default shortcut setting) and there have found no logic.

You can configure Visual Studio to display (and even debug the Framework source). Another way to inspect the implementation’s source code. NET Framework is through the site "Reference Source", by Microsoft itself, which allows browsing and searching the source..

In the case of Suspendlayout(), you can see more details of the method implementation in the Control class in this link https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/Winforms/Control.Cs,ef45260c5d9215da

Imagem mostrando a implementação do Método SuspendLayout

Your conclusions on Suspendlayout() are correct. When invoking Suspendlayout(), the processing of control layout logic is interrupted, that is, you can make multiple layout changes (size, positioning, color, docking, anchoring, etc.) without the control being rendered whenever any property is changed. When invoking Resumelayout() changes are made all at once.

I hope I helped clarify.

Abs

1


As leandro mentioned in the comments, I believe I’m trying to see the code of the method pressing F12 and this appears:

inserir a descrição da imagem aqui

As shown in the image, from metadata this means that the method is coming from the already compiled library (in case System.Windows.Forms.dll) and you have access to the methods signatures but not their code.

To know what the method does, you have to access Microsoft documentation (i.e., who made the library from): https://msdn.microsoft.com/pt-br/library/system.windows.forms.control.suspendlayout(v=vs.110). aspx

And no, the visual studio does not search for the codes of any site. You can add packages by nuget, etc, but there is a command that you have to run.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.