在C#中使用部分类将两个字段组合成一个字段。 [解决了] [英] Using a Partial Class In C# to combine two fields into one Field. [Solved]

查看:314
本文介绍了在C#中使用部分类将两个字段组合成一个字段。 [解决了]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我在将代码和逻辑从VB.NET转换为C#时遇到了一些麻烦。我基本上要做的是将实体表中的两列组合成一列,以便在datagridview列中显示。



在VB.NET中,我用以下代码完成了这项任务:

Hello,

I''m having some trouble converting code and logic from VB.NET over to C#. What I''m basically trying to do is combine two columns in an entity table into one for displaying in a datagridview column.

In VB.NET I completed this task with the following code:

Partial Public Class Employee

    Public ReadOnly Property GetFullName
        Get
            Dim FullName As String = FirstName & " " & LastName
            Return FullName
        End Get
    End Property

End Class



在上面的代码中,我的部分类Employee实际上是对我的实体模型中的Employee表的直接引用,FirstName和LastName都是表中的列。这在VB.NET中完美运行。



现在我正在C#中开展一个单独的项目,我正在努力实现相同的结果。但是,在将我的VB.NET代码转换为C#之后,在设置DataPropertyName列时找不到方法。



我在想这个是因为代码工作,但只有当我手动编码每一列而不只是用一个绑定源填充它或因为VB.NET中的逻辑不同时,C#和C#有不同的方式来实现相同的结果。



这是我在C#中尝试过的代码:


In the code above, my partial class "Employee" was actually a direct reference to the Employee table in my Entity Model and FirstName and LastName are both columns in the table. This worked perfectly in VB.NET.

Now I''m working on a separate project in C# and I''m trying to achieve the same results. However, after converting my VB.NET code to C#, I can''t find the method when setting the columns "DataPropertyName".

I''m thinking this is because either the code works, but only when I''m manually coding each column and not just filling it with a bindingsource or because the logic in VB.NET is different then C# and C# has a different way of achieving this same result.

Here''s the code I tried in C#:

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public partial class Employee
{

    public object GetFullName {
	get {
		string FullName = FirstName + " " + LastName;
		return FullName;
	    }
    }

}





上面的代码没有扔掉任何错误或任何事情。但它并没有做我希望的事情。我尝试了几种变体,比如使用静态,但似乎没什么用。



所以我的问题是任何人都会帮助我完成这个所需的代码?或者,如果它不可能,有人会帮助我理解以另一种方式实现这一目的所需的逻辑吗?



只是为了澄清我需要帮助的是将列的DataPropertyName设置为将保存组合信息的新分部类。所以在创建类之后,我可以在下拉菜单中为列数据属性名称选择它。



另外,我正在使用Visual Studio 2010和SQL Server 2008是否重要。



感谢您提供任何可能的帮助。



更新:

好​​的,使用下面的代码,我实际上可以调用我的Employee列,但是我仍然没有选择将它用作dgv列的数据属性名。



The above code doesn''t throw any errors or anything. But it doesn''t do what I''m hoping for either. I''ve tried several variations like using "static", but nothing seems to work.

So my questions is will anyone help me with the code necessary to accomplish this? Or if its not possible, will anyone help me with understanding the logic that is needed to accomplish this in another way?

And just to clarify what I need help with doing is to set the "DataPropertyName" of a column to the new partial class that will hold the combined information. So after creating the class, I can select it in the drop down menu for the columns data property name.

Also, I''m using Visual Studio 2010 and SQL Server 2008 if it matters.

Thanks for any and all possible help.

UPDATE:
Okay so with the following code, I can actually call my Employee columns, but I still don''t get the option to use it as the data property name of a dgv column.

public partial class Employee : NEIInventorySystemDAL.Employee
       {

           public string GetFullName
           {
               get
               {
                   return this.FirstName + " " + this.LastName;
               }
           }

       }

推荐答案

首先要做的事情理解:类的部分特性与任何功能无关。这只不过是一种在两个或多个不相交的代码片段中编写类的方法,特别是在不同的文件中。它与语义无关,不会改变编译等等。



你也应该明白重复连接是非常无效的,因为它的性质 System.String 类型, immutable 。你甚至要解释它为什么会这样?在一般情况下,您应该使用 string.Format (在您的情况下最好),或者可变类 System.Text.StringBuilder 。我知道你在函数中的两个连接并不是一个大罪,但你应该更好地理解这个问题。



然后,你还有一个合并的问题? :-)



-SA
First thing to understand: the "partial" feature of the classes has nothing to do with any functionality, whatsoever. This is nothing more than a way to write a class in two or more disjointed fragments of code, notably in different files. It has nothing to do with semantics, does not change the compilation, and so on.

You also should understand that repeated concatenation is very ineffective, due to the nature of the System.String type, which is immutable. Do you even have to explain why is it so? In general case, you should use either string.Format (best in your case), or the mutable class System.Text.StringBuilder. I understand that your two concatenation in your function is not a big sin, but you should better understand this problem.

And, after all that, do you still have a problem with "combining"? :-)

—SA


试试这个:

Try this one:
public partial class Employee
{

    public string GetFullName {
    get {
        return this.FirstName + " " + this.LastName;
        }
    }

}





[更新]



[Update]

public class MyClass
{
    public class A
    {
        public string FirstName {get; set;}
        public string LastName {get; set;}
    }

    public partial class B : A
    {
        public string FirtLast {get {return String.Format("{0} {1}", this.FirstName, this.LastName); }}
    }

    public partial class B : A
    {
        public string LastFirst {get {return String.Format("{0} {1}", this.LastName, this.FirstName); }}
    }

    public static void Main()
    {
        var n = new B {LastName = "Smith", FirstName = "John"};

        Console.WriteLine(n.FirtLast);
        Console.WriteLine(n.LastFirst);
    }
}


这篇关于在C#中使用部分类将两个字段组合成一个字段。 [解决了]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆