برچسب: name

  • use the @ prefix when a name is reserved | Code4IT

    use the @ prefix when a name is reserved | Code4IT


    Just a second! 🫷
    If you are here, it means that you are a software developer.
    So, you know that storage, networking, and domain management have a cost .

    If you want to support this blog, please ensure that you have disabled the adblocker for this site.
    I configured Google AdSense to show as few ADS as possible – I don’t want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.

    Thank you for your understanding.
    Davide

    You already know it: using meaningful names for variables, methods, and classes allows you to write more readable and maintainable code.

    It may happen that a good name for your business entity matches one of the reserved keywords in C#.

    What to do, now?

    There are tons of reserved keywords in C#. Some of these are

    • int
    • interface
    • else
    • null
    • short
    • event
    • params

    Some of these names may be a good fit for describing your domain objects or your variables.

    Talking about variables, have a look at this example:

    var eventList = GetFootballEvents();
    
    foreach(var event in eventList)
    {
        // do something
    }
    

    That snippet will not work, since event is a reserved keyword.

    You can solve this issue in 3 ways.

    You can use a synonym, such as action:

    var eventList = GetFootballEvents();
    
    foreach(var action in eventList)
    {
        // do something
    }
    

    But, you know, it doesn’t fully match the original meaning.

    You can use the my prefix, like this:

    var eventList = GetFootballEvents();
    
    foreach(var myEvent in eventList)
    {
        // do something
    }
    

    But… does it make sense? Is it really your event?

    The third way is by using the @ prefix:

    var eventList = GetFootballEvents();
    
    foreach(var @event in eventList)
    {
        // do something
    }
    

    That way, the code is still readable (even though, I admit, that @ is a bit weird to see around the code).

    Of course, the same works for every keyword, like @int, @class, @public, and so on

    Further readings

    If you are interested in a list of reserved keywords in C#, have a look at this article:

    🔗 C# Keywords (Reserved, Contextual) | Tutlane

    This article first appeared on Code4IT

    Wrapping up

    It’s a tiny tip, but it can help you write better code.

    Happy coding!

    🐧



    Source link

  • Custom Pipe Example In Angular. Pipe example get month name.

    Custom Pipe Example In Angular. Pipe example get month name.


    Custom Pipe Example In Angular.

    This tutorial will show you how to create an Angular Custom Pipe. It is handy if we want to reuse some logic across our applications. It allows us to change the format in which data is displayed on the pages. For instance, consider the date of birth as 01-01-2024 and we want to display it as 01-January-2023. To achieve this we will make one pipe that can be used anywhere in our application. In angular

    Types of Pipe.
    •  Pure Pipe.
    •  Impure pipes.
    Pure Pipe.

    The pure pipe is a pipe called when a pure change is detected in the value. It is called fewer times than the latter.

    Impure Pipes.

    This pipe is often called after every change detection. Be it a pure change or not, the impure pipe is called repeatedly.

    Steps to Create Pipe

    Below are the steps to create the custom pipe.

    1. Create a pipe class.
    2. Decorate the class with the @Pipe decorator.
    3. Give a pipe name under name metadata in @Pipe decorator.
    4. Implement the PipeTransform interface and it will contain only one method that is transform.
    5. The transform method must transform the value and return the result.
    6. Import the pipe class in the component.
    7. Use the custom pipe by its name in the HTML file.
    Example.

    Create a pipe class and add the below code.

    import { Pipe,PipeTransform } from '@angular/core';
    
    @Pipe({
        name: 'custompipe',
        standalone: true,
    })
    
    export class custompipePipe implements PipeTransform {
    
        monthNumbers:any = ['January','February','March','April','May','June','July','August','September','October','November','December'];
        transform(value: any) {
            let date = value.split(/[.,\/ -]/);
            if(date[1]>12 || date[1]<1 || isNaN(date[1])){
                return "Invalid Month";
            }else{
                date[1] = this.monthNumbers[date[1]-1];
                return date.join('-');
            }   
             
        }
    }

    Import the custom pipe in pipe-example.componenet.ts file and add the below code.

    import { Component } from '@angular/core';
    import { custompipePipe } from '../custompipe/custompipe.pipe';
    
    @Component({
      selector: 'app-pipe-example',
      standalone: true,
      imports: [custompipePipe],
      templateUrl: './pipe-example.component.html',
      styleUrl: './pipe-example.component.css'
    })
    
    export class PipeExampleComponent{
    
      monthName:number = 0;
      ngOnIt(){
    
      }
    
      getMonthName(event:any){
        this.monthName = event.target.value;
      }
    }
    

    Add the below code in pipe-example.component.html file

     <input type="text" placeholder="Enter Month Name" (keyup)="getMonthName($event)" autofocus>    
    Month Name: {{ monthName | custompipe}}

    Input: 12-02-2023

    Output: 12-February-2023

     

    JavaScript Program To Count The Frequency Of Given Character In String.          Angular Dropdown With Search And Multi Select.



    Source link