Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What is a top level static as i am trying to paste in some code but the problem is it says

"Extension method must be defined in a top level static class."

I can't put it under namespace class or it says the designer can be designed but it is no the first class in the file. Visualk studio requires that designer use the first class in the file?

any suggestions please what i can do?
Posted

I've tried the method you supplied in the comments of SA's solution, and apart from the incorrect variable (I assume "name" is a typo and should be "classname", or vice versa) it compiles fine as an extension method.

It sounds like you're just trying to put it in the wrong place.

I would strongly suggest that you shouldn't put this class in the same file as a designer based class. Indeed, good practice is to define only one class per file - so add another file and put this class in it - e.g. Extended.cs (same name as your class, although I think you should come up with a better name for the class). Although it doesn't have to be in a namespace to compile, it should be - just put it in your application's namespace.

Regards,
Ian.

Updated Example:

HtmlDocumentExtensions.cs
C#
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class HtmlDocumentExtensions
    {
        public static HtmlElement GetElementByName(this HtmlDocument document, string classname)
        {
            foreach (HtmlElement element in document.All)
            {
                if (element.GetAttribute("name").Contains(classname))
                {
                    return element;
                }
            }
            return null;
        }
    }
}

Somewhere else in the code, in this case in my test form, Form1.cs
C#
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HtmlDocument doc = null; // Do whatever you need to do to load the document...
            //...
            HtmlElement element = doc.GetElementByName("The name");
        }
    }
}
 
Share this answer
 
v5
Comments
[no name] 3-May-13 11:26am    
ok how can i then call this in my my program please
Ian A Davidson 3-May-13 12:37pm    
You call it as if it's a method on the class you're extending, in this case HtmlDocument.
I.e. if you have:
HtmlDocument doc /* = etc */;
You would simply do this:
doc.GetElementsByName("TheClassnameToBeLookedfor");
Regards,
Ian.
[no name] 3-May-13 12:56pm    
so about calling it would you do

TestProg test = new TestProg();

what would you do to class it further is that it or?

i have been stuck on this for hours i have researched it but no luck
Ian A Davidson 3-May-13 13:17pm    
No. You need to replace "TestProg" in my example with your program's or module's namespace. If it's not in the same namespace from where you are calling it you would most likely add a "using" statement to include the namespace.
Think about it this way: how would you call the method if it didn't have "this" declared in the first parameter?
public static HtmlElement GetElementsByName(HtmlDocument document, string classname) ...

You would do:
GetElementsByName(doc, "TheClassname");

With an extension method, it's similar - just that you're calling it "on" the parameter which you have declared with "this", and then don't pass that as a parameter:

doc.GetElementsByName("TheClassname");


It does sound like you need to do some basic language learning before you start trying to create and use extension methods!

Regards,
Ian.
Ian A Davidson 3-May-13 13:35pm    
I'm too good to you! I have updated my solution to be more explicit, giving an example of how you would call the method. I hope it is clear now, otherwise, give up for now until you understand some more of the basics.
Regards,
Ian.
Not quite a correct question, but this is because you are confused with extension method in general. There is no "top level static", specifically. Please learn about extension methods, it's easy enough: http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx[^].

In the code samples, please locate "public static class Extension". Can you see how this class is declared? It it not a nested class of some other class, which means it is a "top-level" one. As simple as that. Static class is one thing, static method is something else, access modifier and "top-level" are another, unrelated things. Do you understand all those concepts?

—SA
 
Share this answer
 
v2
Comments
[no name] 2-May-13 16:21pm    
this is the code i have


public static class Extended
{
public static HtmlElement GetElementsByName(this HtmlDocument document, string classname)
{
foreach (HtmlElement element in document.All)
{
if (element.GetAttribute("name").Contains(name))
return element;
}
return null;
}
}
[no name] 2-May-13 16:22pm    
i put this below the name space and it says the about the designer needs to be first in the file still :(
Sergey Alexandrovich Kryukov 2-May-13 16:24pm    
The order in the file does not matter, but it does not look like the beginning of the file. Where is the namespace declaration?
—SA
Sergey Alexandrovich Kryukov 2-May-13 17:02pm    
What designer, for goodness sake? Can you finally show some code?
—SA
[no name] 2-May-13 16:26pm    
i have put it just above this? if i dont put the code just below the namespace dec then it says its needs to be a top level static class?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900