-3

My Auto Generated Class

public partial class Address
{
    public int Id { get; set; }

    public string Name { get; set; }
}

And I want following output. And I don't take same property name in my partial class. so how can i achieve my result? is there any other way.

public partial class Address

{
   public string Name  => Name + " ";
}
Karan Rana
  • 11
  • 1
  • 5
    Why would you want/need this? A partial class is just a way to split up a class. Imagine declaring `Name` twice in the same class. You'd also have a recursive problem in the 2nd one. – Broots Waymb Jan 28 '20 at 14:58
  • My requirement is that i just need to add space after my Name property value. and my class is auto generated partial class. If i change in one place then i don't need to change in all places in my project. – Karan Rana Jan 28 '20 at 15:00
  • 2
    Just use a different name then. Like `NameWithTrailingSpace`. – Sweeper Jan 28 '20 at 15:00
  • @Sweeper Then i need to change in whole project. and more than thousand places. – Karan Rana Jan 28 '20 at 15:03
  • 3
    In your IDE, there should be a feature called "Rename"... – Sweeper Jan 28 '20 at 15:04
  • 2
    The generated class is not sealed, so derive a class from it and re-introduce the property with the new modifier. It can call the base method in the new implementation. – Pete Stensønes Jan 28 '20 at 15:06
  • 1
    This sounds like UI logic. It shouldn't be on the database. Just change in your implementation. – JazzmanJim Jan 28 '20 at 19:50

1 Answers1

4

This isn't possible and really doesn't make any sense. A partial class is just a way to split up a single class. In your example, this is equivalent to having

public partial class Address
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Name  => Name + " ";
}  

Which is obviously not going to compile. Even if it did, the fact that Name is trying to access itself is going to immediately cause issues.

According to your comment, you just need to add a space to the end of the Name property. Since that class is auto-generated, you'll need to add a new property with a different name, such as:

public partial class Address
{
   public string NamePlusSpace  => Name + " ";
}
Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
  • Yes that i know. with use of different property it is possible but it requires lots of changes in my project. so is there any other way that i can achieve? – Karan Rana Jan 28 '20 at 15:05
  • @KaranRana none that I can think of without making other types of large changes, unless you can change where/how the generated code comes from. Using the IDE to rename should be pretty easy though. – Broots Waymb Jan 28 '20 at 15:06
  • thanks. i will make changes in my database property value. i just wanted to is there other way or not? if available then i don't want to change in database. – Karan Rana Jan 28 '20 at 15:10
  • 2
    @KaranRana - If you're changing the core meaning of the property (yes, even an added space counts) it's not a bad thing at all to have a large change reflected in your source control. And the renaming functionality of Visual Studio should make this quick and nearly painless. – Code Stranger Jan 28 '20 at 15:11
  • 2
    @KaranRana adding a space at the end of a name sound like UI logic. I think it would be better to not have this in the database and maybe even away from the business objects. This could be in the control or even with css. My assumption here could be totally wrong also. – the_lotus Jan 28 '20 at 15:14