Note that there are some explanatory texts on larger screens.

plurals
  1. POpartial class does not contain definition
    primarykey
    data
    text
    <p>I got a weird problem and finally fixed it. Just like to share this expeirence so other people will search this topic and get the answer. I defined 2 partial classes, and compiled with the error of "does not contain a definition for ...". It was weird because it worked when the 2 classes were individual classes.</p> <p>Here is the code:</p> <p>Before: Foo1.cs</p> <pre><code>public class foo1{public void xyz(){}} </code></pre> <p>Foo2.cs</p> <pre><code>public class foo2{} </code></pre> <p>After: Foo1.cs</p> <pre><code>public partial class foo1{ public void xyz(){}} </code></pre> <p>Foo2.cs</p> <pre><code>public partial class foo1{} </code></pre> <p>Compilation said "MyNamespace.foo1 does not contain a definition for xyz". But xyz was good when the class was not partial! This could be caused by different namespaces. But I doubted that. Because if namespaces were different, it would not compile.</p> <p>I figured out at last. The problem was in Foo1.cs, the namespace was not defined but it was defined in Foo2.cs. Before I defined the partial class, the compiler provided a default namespace to Foo1.cs, that was the same as the one defined explicitly in Foo2.cs. But with the partial class, the compiler would not provide a default namespace to the partial class. This is why the compiler complained when a third party called foo1.xyz().</p> <p>I learned a lesson that always define the namespace explicitly for each single cs file, except that you do not define the namespace.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload