CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: error CS0118

  1. #1
    Join Date
    Apr 2004
    Location
    Colchester, UK
    Posts
    97

    error CS0118

    I have this code, where `p_entry' is a struct containing a string.

    Code:
    public static void AddEntryToPlaylist(ref p_entry pEntry)
    {
    	lstPlaylist.Items.Add(pEntry.name);
    }
    I get this error regarding the line in the body of the function:
    mp3pro_3.MainForm.lstPlaylist' denotes a 'field' where a 'class' was expected
    The MSDN says this: error CS0118


    I can't make sense of it... can anyone help?

  2. #2
    Join Date
    Feb 2005
    Location
    Texas, U.S.A
    Posts
    81

    Re: error CS0118

    Why dont you just add a string directly to the list if all the struct contains is just a string?

  3. #3
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: error CS0118

    How have you initialized lstPlaylist?
    Useful? Then click on (Rate This Post) at the top of this post.

  4. #4
    Join Date
    Apr 2004
    Location
    Colchester, UK
    Posts
    97

    Re: error CS0118

    This is the beginning of a lagre project and I plan on adding more to the struct in the future.

    Here is how I am initialising it:
    Code:
    this.lstPlaylist = new System.Windows.Forms.ListBox();
    
    this.lstPlaylist.BackColor = System.Drawing.Color.Gainsboro;
    this.lstPlaylist.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lstPlaylist.Font = new System.Drawing.Font("Lucida Console", 9.75F,
            System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
            ((System.Byte)(0)));
    this.lstPlaylist.ForeColor = System.Drawing.Color.Brown;
    this.lstPlaylist.Location = new System.Drawing.Point(12, 180);
    this.lstPlaylist.Name = "lstPlaylist";
    this.lstPlaylist.RightToLeft = System.Windows.Forms.RightToLeft.No;
    this.lstPlaylist.Size = new System.Drawing.Size(512, 236);
    this.lstPlaylist.TabIndex = 2;
    I am using SharpDevelop version 1.0.3

  5. #5
    Join Date
    Jul 2003
    Location
    DELHI
    Posts
    62

    Re: error CS0118

    I think the problem is .... the methid is static
    and lstPlaylist is non static.

    and you are calling a method of non-static member i.e lstPlaylist from static method

    Code:
    public static void AddEntryToPlaylist(ref p_entry pEntry)
    {
    	lstPlaylist.Items.Add(pEntry.name);
    }
    Last edited by Amit_Roy; February 12th, 2005 at 08:43 AM.

  6. #6
    Join Date
    Apr 2004
    Location
    Colchester, UK
    Posts
    97

    Re: error CS0118

    Yes, I had thought that. But when I removed the `static' keyword, it told me it must have an extra `object' parameter.

  7. #7
    Join Date
    Jul 2003
    Location
    DELHI
    Posts
    62

    Re: error CS0118

    could u post the line where the error occurs
    Last edited by Amit_Roy; February 12th, 2005 at 09:13 AM.

  8. #8
    Join Date
    Apr 2004
    Location
    Colchester, UK
    Posts
    97

    Re: error CS0118

    Quote Originally Posted by Amit_Roy
    could u post the line where the occurs
    Where what occurs? I have posted the code that generates the error already. It is this:
    Code:
    public static void AddEntryToPlaylist(ref p_entry pEntry)
    {
    	lstPlaylist.Items.Add(pEntry.name);
    }
    Don't think of it as a struct, that just confuses things. This code still gives the error:
    Code:
    public static void AddEntryToPlaylist()
    {
    	lstPlaylist.Items.Add("test");
    }

    [edit]: If I remove the `static' keyword, I get this error:
    An object reference is required for the nonstatic field, method, or property 'mp3pro_3.MainForm.AddEntryToPlaylist(ref mp3pro_3.p_entry)

  9. #9
    Join Date
    Feb 2005
    Location
    Texas, U.S.A
    Posts
    81

    Re: error CS0118

    This is the issue I think.

    When you use the static keyword you the lstPlaylist and/or the pEntry are non static so you get the error.

    When you remove the static keyword either you are still calling the method without instantiating the object like so:

    Object.AddEntryToPlaylist(pEntry);

    instead of

    Object obj = new Object();
    obj.AddEntryToPlaylist(pEntry);

    or there is still a static reference somewhere.

  10. #10
    Join Date
    Apr 2004
    Location
    Colchester, UK
    Posts
    97

    Re: error CS0118

    Ok, I'm removed all the statics and I get this error:
    An object reference is required for the nonstatic field, method, or property 'mp3pro_3.MainForm.AddEntryToPlaylist(ref mp3pro_3.p_entry)'
    So, I did this:
    Code:
    public void AddEntryToPlaylist(object o,ref p_entry pEntry)
    {
            lstPlaylist.Items.Add(pEntry.name);
    }
    
    ...
    ...
    
    AddEntryToPlaylist(new object(),ref pEntry);
    Which doesn't appear to change anything. I still get the error I started with.

  11. #11
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: error CS0118

    how is pentry defined?
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  12. #12
    Join Date
    Apr 2004
    Location
    Colchester, UK
    Posts
    97

    Re: error CS0118

    Quote Originally Posted by Andy Tacker
    how is pentry defined?
    A public struct:
    Code:
    public struct p_entry {
        string name,file,dir;
    }

  13. #13
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: error CS0118

    Sounds to me like the method that's calling this method is static too.

    And don't bother passing an object in : there's no point ! It's not used anywhere.

    Make sure the whole call graph for this method isn't static (inside of this class of course).

    In fact : look up statics as you don't seem to know what they are. Personally I never use something without looking it up first.

    Darwen.
    Last edited by darwen; February 14th, 2005 at 12:30 PM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  14. #14
    Join Date
    Apr 2004
    Location
    Colchester, UK
    Posts
    97

    Re: error CS0118

    I assume the `static' keyword in C# doesn't have the same effect as `static' in C++... ?

  15. #15
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: error CS0118

    Actually it does have the same effect as the static keyword in classes in C++. However do you know what effect it has on methods in C++ ? I'm sorry but it doesn't look likely from your questions above.

    A static declaration in a class means that whatever it is applied to is only created once, and without a class instance.

    Therefore this below will give you exactly the same problem as you're seeing above :

    Code:
    class MyClass
    {
        private int m_nValue = 0;
    
        // this is a method which operates on the class instance.
        public void Function()
        {
            StaticFunction();
        }
    
        // this is not attached to an instance
        static public void StaticFunction()
        {
            m_nValue = 0; // this fails because there is no class instance.
    
            // here 'this' has no meaning as the method isnt a part of an instance of the class
        }
    }
    However, static methods and member variables can be accessed without a class instance :

    Code:
    MyClass.StaticFunction();
    // this is ok because it's a static method.
    
    MyClass.Function();
    // this isn't ok because it needs an instance of the class
    
    MyClass myClass = new MyClass();
    myClass.Function(); // this is ok
    Hope this helps. It's not a great explanation : I suggest you read up on statics in MSDN because they are tremendously useful - especially in C# where you can do things like this :

    Code:
    // simple singleton
    public sealed class MyClass
    {
       // instansiated on first call to MyClass
       static private MyClass m_theClass = new MyClass;
    
       static public MyClass Singleton
       {
            get
            {
                return m_theClass;
            }
       }
     
       // ensures you can't 'new' the class  
       private MyClass()
       {
       }
    }
    Darwen.
    Last edited by darwen; February 14th, 2005 at 04:12 PM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured