Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

field initializer

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    field initializer

    I have the following:

    #region Variables
    public int dotsize = 10;

    private
    Font pfont = new Font("Arial", dotsize, FontStyle.Bold, GraphicsUnit.Point);

    when I place "dotsize" in the code i get an error:

    A field initializer cannot reference the nonstatic field, method, or property

    When I type
    private Font pfont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point);

    everything is fine. How do I swap the word dotsize for the number 10 ?
    Last edited by velocity; 03-18-2009, 11:04 AM.

    #2
    Is that "publicint" a typo? If not, I believe it should be:

    private int dotsize = 10;
    Last edited by eDanny; 03-18-2009, 07:05 AM.
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      You likely can't do that because dotsize has not been initialized yet.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        So how do I initialize it?

        (publicint was a typo.)

        Comment


          #5
          You can't. Do it after your dotsize has been initialized in OnBarUpdate() somewhere.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            I'm confused, you say I can't, Then you say I can and to put it after bar update. Still a little puzzled here as to what the code is for initializing dotsize would look like.

            Comment


              #7
              I say you can't meaning not from the Variables section up there.

              Leave your dotsize as is. Move your Font down to OnBarUpdate(). Or just hard set the dotsize in your Font. The point is you can't do it that early. It doesn't know what dotsize is and will only know at runtime. That is why you can't compile.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                ok, I will give it a try. thanks

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by CarlTrading, 03-31-2026, 09:41 PM
                1 response
                152 views
                1 like
                Last Post NinjaTrader_ChelseaB  
                Started by CarlTrading, 04-01-2026, 02:41 AM
                0 responses
                89 views
                1 like
                Last Post CarlTrading  
                Started by CaptainJack, 03-31-2026, 11:44 PM
                0 responses
                133 views
                2 likes
                Last Post CaptainJack  
                Started by CarlTrading, 03-30-2026, 11:51 AM
                0 responses
                127 views
                1 like
                Last Post CarlTrading  
                Started by CarlTrading, 03-30-2026, 11:48 AM
                0 responses
                107 views
                0 likes
                Last Post CarlTrading  
                Working...
                X