Hi I have a partial class which reads a value from in
public partial class getFile : System.Web.UI.Page
{
string file = AcceptButton.FileName;
}
string file I want to access in a regular class
public class
{
string _file=file;
}
First class being partial class I cant do that. How can I do this?
First class being partial class I cant do that. How can I do this?
The fact that the class is partial has nothing to do with this. From the point of view of an external type it doesn't know (or care) that the class is a partial class.
The reason you can access file from some other class is that file is private.
To access the member directly you would need that member to be public, and you'd also need an instance of that type.
Having said that, since it seems this is related to as webpage, it doesn't make sense for some other class to be accessing the pages fields in the first place. What you should be doing is having the constructor or method(s) of test accept a parameter of type string to which getFile can pass in it's file field when either creating a test object or calling one of its methods.
Related
I am working with a something.razor file and a code behind Something.razor.cs file. My partial class inherits from SomethingBase which itself inherits from ComponentBase.
This however gives errors
CS0115 .buildrendertree(rendertreebuilder)': no suitable method found to override in partial class
CS0263 partial declarations of must not specify different base classes.
However if Something inherits directly from ComponentBase there is no issue.
First question please be gentle. I'll update with code and more detail in the morning if needed.
With a code behind file you have 2 spots where you can specify the base class.
You will always have to specify it in the .razor file because you don't want the default there:
// Something.razor
#inherits SomethingBase
and then you have a choice in the .razor.cs file:
// Something.razor.cs (a)
partial class Something // take base class from other part
{
}
or
// Something.razor.cs (b)
partial class Something : SomethingBase // must use the same as other part
{
}
I would use (a) .
I have a class Employee.cs that was autogenerated by EntityFramework when i connected to a database, now i want to implement a partial class but I can't use "Employee.cs" name for that class because it already exists. Can I give other name to it and it will work? Can you explain how the compiler will know that there is another partial class of Employee.cs?
You can give the file whatever name you want like Employee.custom.cs but you have to call the class in it Employee in the same namespace as the Employee class in the other file, also with a partial modifier, and you should be good to go.
The compiler just gathers all the partial classes with the same name and compiles it into one class per name.
Compiler always works with fullname of class. If there is a project named MyProject, full name for the class would be something like:
MyProject.Employee
If You want to create class with the same name, You have to add a level of separation in naming, or if it have to be partial class with this generated class, You have to mark it also as a partial.
You can either create (in Model/Employee.cs):
public class MyProject.AnySubPath.Employee {}
Or:
In file Model/Employee.cs
public partial class MyProject.Employee {}
In file ViewModel/Employee.cs
public partial class MyProject.Employee {}
Important for You is to realize what is the partial mean & what will the compiler to with Your class: In fact the compiler will just put all the partial classes together and then compile them. Only benefit from being partial is that You can have the code split among multiple files. Excelent example for this is Windows.Forms, where You have "code-behind" file (Form1.cs) and then You have "designer" file (Form1.Designer.cs).
Also You can name the files whatever name You want, it is just common standard to have 1 class = 1 file and the class name should fit the file name.
It would be great if this would work. Am I trying to implement my idea in the wrong way?
I would like to use partial method, to be able to extend existing code, and simply plug in/out implementation of methods.
Basically exactly what the reference is stating:
Partial methods enable class designers to provide method hooks,
similar to event handlers, that developers may decide to implement or
not. If the developer does not supply an implementation, the compiler
removes the signature at compile time.
My first try of using this is the following:
DefinitionsBase.cs:
namespace ABC {
public partial class Definitions {
// No implementation
static partial void TestImplementaion();
}
}
DefinitionsExt.cs:
namespace ABC {
public partial class Definitions {
static partial void TestImplementaion(){
// Implementation is here
}
}
}
Program.cs:
namespace ABC {
class Program {
static void Main(string[] args) {
Definitions.TestImplementaion();
}
}
}
It's same namespace, but as reference states partial methods are implicitly private. It doesn't accept access modifiers and I cannot call it from my class. Is there a way to use it as I intend to?
Thanks!
You could use a public method that calls the private method, but I am not sure if this is what you want. This just makes your code work.
Partial methods are by definition private so as during compilation time, in case the method was not implemented, the compiler does not need to go through all of the code, find all possible references to the method, and remove them.
This is a design choice since partial methods do not necessarily need to be implemented, the compiler only looks in the partial class implementation and not throughout all of the code.
If you implement a public method that calls the partial method and the partial method was not implemented, the compiler will still only look in the partial class files and code, even though you have access to that partial method from anywhere in your code.
I am in the beginners level to learn C#. Normally in C++ if we need to access the methods/values of the class we have to declare an object instance and thru object only we can access the method. But in C#.Net I came to know for static public class we dont need to declare the object, directly we can call the method using class.method() name. Could you pls clarify me when we refer the standard class library classes, such as messagebox.
System.Windows.MessageBox.ToShow()
System.Windows - Its name space.
ToShow - Its Method
MessageBox - Messagebox is class or object here?
I beleive for all the standard .NET class library namespaces internally create the object instances thru constructors when we refer any of the method belongs to the class. Its always object name is same as class name as its created by constructor. pls correct me if my understanding is wrong.
Thanks,
Karikalan
MessageBox is a class
http://msdn.microsoft.com/en-us/library/system.windows.messagebox.aspx
Show is a static method of MessageBox class. You can call static methods in the format ClassName.MethodName
Another thing about MessageBox is that it is a sealed class. A sealed class cannot be inherited. That means you can not derive a custom class from this class.
More info about Static classes and methods are nicely explained here
I beleive for all the standard .NET class library namespaces internally create the object instances thru constructors when we refer any of the method belongs to the class. Its always object name is same as class name as its created by constructor.
Not at all. Only static methods can be accessed directly from the class. To call an instance method, you need an instance of the class (i.e. an object).
System.Windows.MessageBox.Show();
System.Windows is namespace
MessageBox is a class
Show is a static method
MessageBox is a sealed class and it has a static method ToShow(). And to access static method you dont need to create instances.
The tutorial application, MusicStore -MVC3 (92 PageNo), created a POCO class like:
public partial class ShoppingCart
{
MusicStoreEntities storeDB = new MusicStoreEntities();
public static ShoppingCart GetCart(HttpContextBase context)
{
var cart = new ShoppingCart();
cart.ShoppingCartId = cart.GetCartId(context);
return cart;
}
}
How can we access static method in partial classes? In my opinion, we cannot access static methods in a partial class. The partial attribute means other parts of the class will be included in the namespace. In this scenario, I do not know where this other partial class is implemented.
My questions about this static method are:
Can we access static methods in partial classes? If so, how?
Where is this partial class implemented in this MusicStore application? I am not able to find the other part of this class's implementation.
Updated: There is no other ShoppingCart class in the models directory. Does anyone know where that partial implementation would be?
A partial class in C# can definitely access static methods. The partial attribute simply says a class can (not must) be defined accross multiple files and otherwise doesn't affect member lookup.
EDIT Responding to comment in question
A possible explanation for why you can't find the other implementation of ShoppingCart is it may not exist. A partial class is not required to have multiple definitions. The partial only means there may be other parts of the definition.
Yes, you can access static methods in partial classes.
Partial classes are just a way of representing a regular class in multiple source files, often with some of those source files controlled (or generated) by tools.
You can call ShoppingCart.GetCart(context) anywhere - it's a normal public static method.
It's still not really clear what your second question means, but there doesn't have to be another part at all. It's fine (though unusual) to have a partial class which is only declared in a single file.