partial-classes

Partial Classes in C#

折月煮酒 提交于 2019-12-03 23:54:30
Are there are good uses of Partial Classes outside the webforms/winforms generated code scenarios? Or is this feature basically to support that? It is in part to support scenarios (WebForms, WinForms, LINQ-to-SQL, etc) mixing generated code with programmer code. There are more reasons to use it. For example, if you have big classes in large, unwieldy files, but the classes have groups of logically related methods, partial classes may be an option to make your file sizes more manageable. Code generation was the driving force behind partial classes. The need comes from having a code-generated

How to extend DbContext with partial class and partial OnModelCreating method in EntityFramework Core

▼魔方 西西 提交于 2019-12-03 12:10:34
问题 I'm using EF Core DbFirst approach. My dbContext is created automatically by Scaffold-DbContext command. I need to add additional DbSets into a dbContext and add into OnModelCreating method of dbContext some additional code but after each scaffolding that added code are erased and I have to add it each time again. What I want to do is to create another partial dbContext class and mark protected override void OnModelCreating(ModelBuilder modelBuilder) as a partial method but get errors: A

access and set variables in a class from another class

烈酒焚心 提交于 2019-12-02 14:23:13
问题 i'v a shopping_cart.aspx.cs file & also have a class file spcart.cs, shopping_cart.aspx.cs public partial class Ui_ShoppingCart : System.Web.UI.Page { public int tax = 0; public int subtotal = 0; public int granttotal = 0; protected void Page_Load(object sender, EventArgs e) { -------------------------/////some code } --------------------------------/////some code } spcart.cs public class Spcart { public void updatecart(int pid,int qty) { ---------/////some code } } now i want to set some

C++ class definition split into two headers?

*爱你&永不变心* 提交于 2019-12-02 11:39:27
问题 Is it possible in C++ to split the definition of class members in two headers? What would be the appropriate way to code it? For instance: a1.h class A { public: int var; void foo1(int b); } a1.cpp #include "a1.h" void A::foo1(int b) { cout << b; } a2.h [extend] class A { public: void foo2(double c); } a2.cpp #include "a2.h" void A::foo2(double c) { cout << c; } 回答1: You can't extend a class that way, but you can use the pimpl pattern: class A { public: void foo1(int b); private: AImpl* pimpl

access and set variables in a class from another class

限于喜欢 提交于 2019-12-02 09:33:36
i'v a shopping_cart.aspx.cs file & also have a class file spcart.cs, shopping_cart.aspx.cs public partial class Ui_ShoppingCart : System.Web.UI.Page { public int tax = 0; public int subtotal = 0; public int granttotal = 0; protected void Page_Load(object sender, EventArgs e) { -------------------------/////some code } --------------------------------/////some code } spcart.cs public class Spcart { public void updatecart(int pid,int qty) { ---------/////some code } } now i want to set some values in class Ui_ShoppingCart variables tax, subtoal & granttotals from class Spcart, so i'd tried--> Ui

How can I use attributes on a property defined in the other half of a partial class?

风格不统一 提交于 2019-12-01 18:41:44
I have an autogenerated class from importing a web service containing something like this (abbreviated): [System.Runtime.Serialization.DataMemberAttribute()] public System.DateTime StartDate { get { return this.StartDateField; } set { /* implementation prop changed */ } } And I want to add an MVC format attribute to this member. So in another file containing the same partial class definition, I would like to do something like the following (which is illegal): [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] public DateTime StartDate; A partial method is of no

member names cannot be the same as their enclosing type with partial class

无人久伴 提交于 2019-12-01 17:59:47
I have defined a partial class with a property like this: public partial class Item{ public string this[string key] { get { if (Fields == null) return null; if (!Fields.ContainsKey(key)) { var prop = GetType().GetProperty(key); if (prop == null) return null; return prop.GetValue(this, null) as string; } object value = Fields[key]; return value as string; } set { var property = GetType().GetProperty(key); if (property == null) { Fields[key] = value; } else { property.SetValue(this, value, null); } } } } So that i can do: myItem["key"]; and get the content of the Fields dictionary. But when i

How to combine a Partial Class Object in C#?

放肆的年华 提交于 2019-12-01 17:57:49
I defined a class: public class Sample{ public string name {get;set;} public int price {get;set} } Then Sample sampleA = new Sample(); sampleA.name = "test"; Sample sampleB = new Sample(); sampleB.price = 100; I do this because I will save JSONed sampleA and JSONed sampleB to Azure table, to present a full sample object. In other code, sometime only need name, sometime only need price, so I only need to pull data that needed each time. Of course in real code, the structure of the sample is much much more complex. My question is :, is there any easy method that can do: Sample sampleC = sampleA

How can I use attributes on a property defined in the other half of a partial class?

半腔热情 提交于 2019-12-01 17:37:15
问题 I have an autogenerated class from importing a web service containing something like this (abbreviated): [System.Runtime.Serialization.DataMemberAttribute()] public System.DateTime StartDate { get { return this.StartDateField; } set { /* implementation prop changed */ } } And I want to add an MVC format attribute to this member. So in another file containing the same partial class definition, I would like to do something like the following (which is illegal): [DisplayFormat(DataFormatString =

How to combine a Partial Class Object in C#?

梦想的初衷 提交于 2019-12-01 17:34:23
问题 I defined a class: public class Sample{ public string name {get;set;} public int price {get;set} } Then Sample sampleA = new Sample(); sampleA.name = "test"; Sample sampleB = new Sample(); sampleB.price = 100; I do this because I will save JSONed sampleA and JSONed sampleB to Azure table, to present a full sample object. In other code, sometime only need name, sometime only need price, so I only need to pull data that needed each time. Of course in real code, the structure of the sample is