Click to See Complete Forum and Search --> : error CS0118


tomcant
February 11th, 2005, 04:22 PM
I have this code, where `p_entry' is a struct containing a string.

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 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscomp/html/vcerrCompilerErrorSC0118.asp)


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

checksal
February 11th, 2005, 10:40 PM
Why dont you just add a string directly to the list if all the struct contains is just a string?

Norfy
February 12th, 2005, 03:36 AM
How have you initialized lstPlaylist?

tomcant
February 12th, 2005, 06:51 AM
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:
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

Amit_Roy
February 12th, 2005, 07:36 AM
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


public static void AddEntryToPlaylist(ref p_entry pEntry)
{
lstPlaylist.Items.Add(pEntry.name);
}

tomcant
February 12th, 2005, 07:55 AM
Yes, I had thought that. But when I removed the `static' keyword, it told me it must have an extra `object' parameter.

Amit_Roy
February 12th, 2005, 08:03 AM
could u post the line where the error occurs

tomcant
February 12th, 2005, 08:12 AM
could u post the line where the occurs

Where what occurs? I have posted the code that generates the error already. It is this:
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:
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)

checksal
February 12th, 2005, 11:15 PM
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.

tomcant
February 13th, 2005, 04:34 AM
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: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.

Andy Tacker
February 14th, 2005, 04:22 AM
how is pentry defined?

tomcant
February 14th, 2005, 11:20 AM
how is pentry defined?

A public struct:
public struct p_entry {
string name,file,dir;
}

darwen
February 14th, 2005, 11:26 AM
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.

tomcant
February 14th, 2005, 01:52 PM
I assume the `static' keyword in C# doesn't have the same effect as `static' in C++... ?

darwen
February 14th, 2005, 03:03 PM
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 :


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 :


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 :


// 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.