Click here to Skip to main content
15,867,288 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
VB
Partial Public Class Employee

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

End Class

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#:
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;
	    }
    }

}


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.
C#
public partial class Employee : NEIInventorySystemDAL.Employee
       {

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

       }
Posted
Updated 4-Jan-13 6:54am
v4

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
 
Share this answer
 
Comments
Mr.McCloud 4-Jan-13 12:51pm    
Alright, thanks for the advice on the concatenation. I never knew it was bad practice the way I've been doing it. I'll definitely start using a better method.

And I guess when it comes to the partial class, I was just hoping for an easy solution to my problem. I've never fully understood exactly how the VB.NET code that I used worked in the first place.

I think the best thing to do, is go back and first learn what the VB.NET code is actually doing, and then I can probably translate that logic somewhat into C#

Thanks for the help.
Sergey Alexandrovich Kryukov 4-Jan-13 15:57pm    
Absolutely. Good idea.
Well, you are very welcome.
Good luck, call again.
—SA
Try this one:
C#
public partial class Employee
{

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

}


[Update]
C#
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);
    }
}
 
Share this answer
 
v4
Comments
Mr.McCloud 3-Jan-13 17:11pm    
It can't recognize FirstName and LastName as columns.

I don't think Employee in C# is actually referencing the Employee table like it does in VB.NET. I tried getting it to inherit from the Employee's table but its not an option to inherit from.

Do you know how I can make sure Employee is actually a partial class of the Employee table instead of just a new class?
Mr.McCloud 3-Jan-13 17:27pm    
Okay scratch that.

The inheriting works and FirstName and LastName are both now treated as objects in the table.
Zoltán Zörgő 4-Jan-13 3:49am    
You have to decide what's needed. See update. Be sure to use proper namespaces, properly qualified names and reference all assamblies needed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900