Create an object reference in your own method

Asked

Viewed 82 times

-1

Problem

I want to get the last digits of this string, then I used the method Substring(), however I had to use the variable "number" again within the method Substring().

string numero = "123456789";
string final = numero.Substring(numero.Length - 4);
Console.Write(final);

Is there any way to reference the variable numero in its own method? Something like:

string final = numero.Substring(reference.Length - 4);

I used the word "Ference" just as an example, I am aware that the way this won’t work.

Because I want it

The way this is very simple to use the variable number, but in the code in which I need to perform this procedure the variable I am using is in a chain of methods and submethods, so the reference leaves the code confused. As a palliative I did the following:

string numero = referenciaMuitoGrandeBlaBla;
string final = numero.Substring(numero.Length - 4);
Console.Write(final);

Already help, however I did not want to use this way, since there may be a way to reference.

2 answers

3

In C# 8 it is possible to do so:

Write("123456789"[^4..]);

This is a banner (crease).

Meanwhile what you want is to have a method called Right(), that is, a method that takes the characters that are on the right. In fact the only way to do this is to use the variable twice, so to avoid this duplicity in the syntax you need to create an abstraction (the lost art of programmers). The most interesting way to do this is to create an extension method for String in System, as in all strings this method will be available as if it were a normal method of type:

using System;
using static System.Console;

public class Program {
    public static void Main() {
        WriteLine("123456789".Right(4));
    }
}

namespace System {
    public static class StringExtensions {
        public static string Right(this string text, int length) => text.Substring(text.Length - length);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

2


Create an extension method to create your Substring customized, within this method you implement the logic you want. To create an extension method we need to create a static class and static methods that have the keyword this which will reference our current object, below is an example:

namespace ExemploExtensao
{
    public static class Extensions
    {
        public static string SubstringCustomizado(this string texto, int length)
        {
            //Implemente a lógica que desejar. 
            //this string texto se refere ao objeto que chamar esse método
            return texto.Substring(texto.Length - length);
        }
    }

To use this method just reference the namespace and call the same way you call the method Substring:

using ExemploExtensao    
{
    static void Main(string[] args)
    {
        string teste = "0123456789";

        //Quando chamar seu SubstringCustomizado, a variável teste irá ser o 
        //parâmetro this do método, assim você não precisa passar como 
        //parâmetro conforme sua pergunta
        Console.WriteLine(teste.SubstringCustomizado(4));
    }
}

More information about extension methods you can check in the Microsoft documentation: https://docs.microsoft.com/pt-br/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

  • Interesting, I go from the +1, but still not the answer I’m looking for, because I really wanted the reference by ease, this way you did is cool, but it goes from more work than putting in a variable before the process as reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.