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.
7,595 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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{}
}
}
No link means the value DateCrea will be copied in value Year. Year doesn't change for a new DateCrea value.
I've seem that it was possible with clone function. But I'm not sure
@sblb , Welcome to Microsoft Q&A, Do you mean that Year will be changed when the DateCrea value has been changed? If so, Please try to return the value directly like the following:
I agree with your solution but it is not exactly what I would like.
The variable datecrea represents the current date from which I extract the year with
int datecrea = DateCrea.Year
In the column Year I want to copy datecrea ie currently the values of column Year is 2022 and at the change of year the column Year will take the value 2023 as well as 2022.
Is it possible to do that in class model?
Similar to your other threads, you explain what you think is the solution rather than the actual problem. The community has no idea what you're trying to do and simply answers your question. The community's answer does not solve your problem because your solution makes no logical sense.
All you're trying to do is track when the year changes which is used to reset a counter. This requires two years. One year that is saved and the current year. The current year is always available using DateTime.Now.Year. All you have to do is compare the saved year with the current year. If the two years are different then the year has changed. If your design saves many years - has many records - then you must find the largest year in saved records. This concepts has been explained to you many times with source and unit test code. Yet you continue to ignore the advice. Why???
Review your past threads on this subject.
https://learn.microsoft.com/en-us/answers/questions/981655/populate-the-textbox-from-the-value-stored-in-the.html
https://learn.microsoft.com/en-us/answers/questions/984587/c-find-the-max-of-the-table-column-in-class-model.html
https://learn.microsoft.com/en-us/answers/questions/972901/set-in-model-class-is-not-functionnal.html
Thanks to check my posts.
So anyway, you told me that you have made a T-SQL test code that I understood. So in your test code the input year is
And you have to understand in this case it's easy!!
In my case this input year must be save and after that it'll be easier to do the comparison with current year.
Why don't you want to understand what I want to do?
The code you've shared above is taken out of context. That's test data used in a unit test to verify the logic functions as expected. This is yet another example where you copy and paste code without making an effort to understand the code.
Go through any beginning level tutorial to learn how to save data in a database.
To compare with the current year in SQL-Server
Sign in to comment
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.
Sign in to comment
2 additional answers
Sort by: Most helpful
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?
@sblb , thanks for the feedback, you could refer to the Microsoft doc MemberwiseClone to know more about it. Do you want to use ShallowCopy or DeepCopy?
Thanks for your information.
in my case I have to use DeepCopy.
have you some advise how I can do?
I'm confident cloning is not the solution. You'll end up with two copies of the same year and the same problem. Please explain how cloning solves this programming problem.
Hello,
Yes, I am looking for a solution to my problem.
Indeed I want to record a year and then compare it to the current year.
To do this the current year is obtained by :
DateCrea.Year.ToString()
, it's ok !So my question (or my problem as you want) resides in the saving or copying of the year.
Not being very comfortable with stored procedure (see other threads) I want to implement my counter in the model class.
So how do I save the year in my table so that I can make a comparison with the current year? Do you have an idea?
Every beginning level "working with data tutorial" illustrates how to save data in a database. I've mentioned this many time in your threads, I provided links to the tutorials as well as complete code samples.
Your other threads indicate you are building a Blazor WASM application. You should have a Web API application that has an action which fetches the saved year from your database. Your Blazor WASM application calls the Web API action using HttpClient to get the saved date.
Reference documentation
Call a web API from ASP.NET Core Blazor
Sign in to comment
@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
the controller
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 inYearC
by simply doingDateCrea.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.Sign in to comment
Activity