c# copy one value into class model

sblb 1,171 Reputation points
2022-09-05T15:51:59.227+00:00

Hi,
I would like to know if it's possible to copy the value into another independently of each other in class model.

Below the example of the class model

 public partial class Developer  
    {  
          
        public int Id { get; set; }  
        public DateTime DateCrea { get; set; }  
        public string Year { get; set; }  
        public string EnvT {  
            get   
             {  
              int datecrea = DateCrea.Year;  
                
              // here I want to copy the value datecrea in Year w/o no link between   
               the two values  
              }  
             set{}  
         }  
}  
  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,638 questions
{count} votes

Accepted answer
  1. AgaveJoe 27,686 Reputation points
    2022-09-08T10:19:30.703+00:00

    You are struggling with basic programming and problem solving. The use case is very simple; If the year is not found in the database then set the count to 1 and set the year to the current year. If the year is found then increment the count and set the year to the current year. This logic should exist behind Web API which includes a Web API action, business logic, data access, or stored procedure. It is possible to add the logic to Blazor but, in my opinion, this is not efficient.

    Against my better judgement, I created a working demo on GitHub. THIS IS NOT PRODUCTON CODE!!! The example code is intended to help you write an if statement and LINQ queries.

    https://github.com/mjgebhard/WebApiSqlite

    Please make an effort to learn C# and LINQ rather than your current programming strategy which is copy, paste, and pray.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. sblb 1,171 Reputation points
    2022-09-07T06:42:27.94+00:00

    Hi,
    After reading several docs I understand that it is not possible to copy in c#.
    But I saw that you can use the MemberwiseClone method. Is it possible to apply it in class model?


  2. sblb 1,171 Reputation points
    2022-09-07T20:14:09.757+00:00

    @AgaveJoe thanks for your information.

    I have any problem to post the data in table from database;
    ExactlyI use blazor wasm.

    To post the data

    @page "/developer/create"  
    @inject HttpClient http  
    @inject NavigationManager uriHelper  
      
    <FormCreate ButtonText="Create ECR" dev="@dev" OnValidSubmit="@CreateDeveloper"/>  
      
    @code {  
        Developer dev = new Developer();  
      
        async Task CreateDeveloper()  
        {  
            await http.PostAsJsonAsync("api/developer", dev);  
            uriHelper.NavigateTo("developer");  
      
        }  
    }  
    

    the controller

      [HttpPost]  
            public async Task<IActionResult> Post(Developer developer)  
            {  
                _context.Add(developer);  
                await _context.SaveChangesAsync();  
                return Ok(developer.Id);  
            }  
    

    In class model I have serveral data including [YearC] and [DateCrea] which corresponds to the current date.

    When I put the DateCrea in UI I wanted to get the year in YearC by simply doing DateCrea.Year.ToString(); in the model class. But as you would have understood in this case YearC will change according to the current year. So I can not make the comparison!! This my problem. I don't know how to fix YearC.

    0 comments No comments