|
-
February 11th, 2005, 05:22 PM
#1
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?
-
February 11th, 2005, 11:40 PM
#2
Re: error CS0118
Why dont you just add a string directly to the list if all the struct contains is just a string?
-
February 12th, 2005, 04:36 AM
#3
Re: error CS0118
How have you initialized lstPlaylist?
Useful? Then click on (Rate This Post) at the top of this post.
-
February 12th, 2005, 07:51 AM
#4
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
-
February 12th, 2005, 08:36 AM
#5
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.
-
February 12th, 2005, 08:55 AM
#6
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.
-
February 12th, 2005, 09:03 AM
#7
Re: error CS0118
could u post the line where the error occurs
Last edited by Amit_Roy; February 12th, 2005 at 09:13 AM.
-
February 12th, 2005, 09:12 AM
#8
Re: error CS0118
 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)
-
February 13th, 2005, 12:15 AM
#9
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.
-
February 13th, 2005, 05:34 AM
#10
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.
-
February 14th, 2005, 05:22 AM
#11
Re: error CS0118
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.
-
February 14th, 2005, 12:20 PM
#12
Re: error CS0118
 Originally Posted by Andy Tacker
how is pentry defined?
A public struct:
Code:
public struct p_entry {
string name,file,dir;
}
-
February 14th, 2005, 12:26 PM
#13
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.
-
February 14th, 2005, 02:52 PM
#14
Re: error CS0118
I assume the `static' keyword in C# doesn't have the same effect as `static' in C++... ?
-
February 14th, 2005, 04:03 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|