none
Can't use a partial class NavMenu RRS feed

  • Question

  • I'm using Shared/NavMenu.razor with html and C#.
    Now I try the extract the C# to a newly created NavMenu.razor.cs.
    But the cs class is not recognised from the system.
    Also Visual Studio does not makes any highlighting at this class.

    How can I extract the C# code from NavMenu into a own class?

    Monday, July 25, 2022 10:27 AM

Answers

All replies

  • Hello Frank

    Microsoft forums migrated from this system to a new system long time ago. Please use the following link and ask your question in the new forum interface: https://docs.microsoft.com/answers

    Note: The new system based on tags. It is like one forum for all but using the right tag you will get to the right experts


    signature   Ronen Ariely
     [Personal Site]    [Blog]    [Facebook]    [Linkedin]


    Tuesday, July 26, 2022 7:57 AM
  • Thank you very much Ronen! <3
    Tuesday, July 26, 2022 12:24 PM
  • How long will it take to complete this migration?

    • Edited by jhonnplay Tuesday, July 26, 2022 8:30 PM change
    Tuesday, July 26, 2022 8:29 PM
  • There are several situations when splitting a class definition is desirable:

    When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.
    When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
    When using source generators to generate additional functionality in a class.
    To split a class definition, use the partial keyword modifier, as shown here:

    C#

    Copy
    public partial class Employee
    {
        public void DoWork()
        {
        }
    }

    public partial class Employee
    {
        public void GoToLunch()
        {
        }
    }
    The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.

    If any part is declared abstract, then the whole type is considered abstract. If any part is declared sealed, then the whole type is considered sealed. If any part declares a base type, then the whole type inherits that class.

    All the parts that specify a base class must agree, but parts that omit a base class still inherit the base type. Parts can specify different base interfaces, and the final type implements all the interfaces listed by all the partial declarations. Any class, struct, or interface members declared in a partial definition are available to all the other parts. The final type is the combination of all the parts at compile time.

     Note

    The partial modifier is not available on delegate or enumeration declarations.

    The following example shows that nested types can be partial, even if the type they are nested within is not partial itself.

    C#

    Copy
    class Container
    {
        partial class Nested
        {
            void Test() { }
        }

        partial class Nested
        {
            void Test2() { }
        }
    }
    At compile time, attributes of partial-type definitions are merged. For example, consider the following declarations:

    C#

    Copy
    [SerializableAttribute]
    partial class Moon { }

    [ObsoleteAttribute]
    partial class Moon { }
    They are equivalent to the following declarations:

    C#

    Copy
    [SerializableAttribute]
    [ObsoleteAttribute]
    class Moon { }
    The following are merged from all the partial-type definitions:

    XML comments
    interfaces
    generic-type parameter attributes
    class attributes
    members
    For example, consider the following declarations:

    C#

    Copy
    partial class Earth : Planet, IRotate { }
    partial class Earth : IRevolve { }
    They are equivalent to the following declarations:

    C#

    Copy
    class Earth : Planet, IRotate, IRevolve { }
    Restrictions
    There are several rules to follow when you are working with partial class definitions:

    All partial-type definitions meant to be parts of the same type must be modified with partial. For example, the following class declarations generate an error:
    C#

    Copy
    public partial class A { }
    //public class A { }  // Error, must also be marked partial
    The partial modifier can only appear immediately before the keywords class, struct, or interface.
    Nested partial types are allowed in partial-type definitions as illustrated in the following example:
    C#

    Copy
    partial class ClassWithNestedClass
    {
        partial class NestedClass { }
    }

    partial class ClassWithNestedClass
    {
        partial class NestedClass { }
    }
    All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.
    The class name and generic-type parameters must match on all partial-type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.
    The following keywords on a partial-type definition are optional, but if present on one partial-type definition, cannot conflict with the keywords specified on another partial definition for the same type:
    public
    private
    protected
    internal
    abstract
    sealed
    base class
    new modifier (nested parts)
    generic constraints

    Regards,

    Rachel Gomez

    Saturday, September 24, 2022 5:18 AM
  • Such a Good Way with Explained. Ty Rachel
    Wednesday, September 28, 2022 5:04 PM