Error concatenating string from path

Asked

Viewed 146 times

3

In this program right at the beginning has a function that creates a folder in the user’s documents to save some information

Giving a research I came to the conclusion that it should stay like this:

    static int PrepararTerreno()
    {
        string Diretorio = ("C://Users//{0}//Documents//Cadastro",Environment.UserName);
        Directory.CreateDirectory (Diretorio);
        return 0;
    }

It is for the program to see the user name and insert there in the middle of the string for the folder to be created in the documents of the user who opened the program.

But when I run the program the Visual studio gives this error:

CS0029 Cannot implicitly convert type "(string, string Username)" to "string"

  • String.Format("C://Users//{0}//Documents//Cadastro",Environment.UserName);

  • Thanks man this was giving me a headache

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

2 answers

4

This job is a little weird. If you are practicing then it would be interesting to practice what is most important and at the same time easier because it is not even programming itself. If you make a mistake that’s how you always do it.

The function name is not good because it doesn’t say what it really does. Unless she does something that is not yet in the code, but in this case a comment would be good, comments serve precisely for this.

The same goes for the return, if it will return something that is fixed, then it is better to return nothing, unless you will do other things later, and again a comment would be fine.

It makes no sense to use // in the text, or uses \\ or uses /. To \\ is necessary because the bar alone is an escape character since the backslash is used for formatting. Using the backslash can use the @ so you don’t have to duplicate it.

Can use interpolation there and avoid further headaches.

The mistake is that it is creating a tuple (example, another, one more, the last) composed of an object string and other object string with a name. And declared a variable of type string only (which is obviously not a tuple, is incompatible).

The statement is correct, the assignment is wrong. The parentheses left there make all the difference by creating a different kind, so I always talk:

Enquanto você não souber o que cada caractere do seu código faz, até mesmo o espaço em branco, você ainda não sabe programar

Even the space in the function name is unnecessary, does not give an error, but gives a wrong indication, it looks like you are grouping expressions or creating a tuple instead of using a function. It sounds silly but the code is less readable. Even if it works not everything is right, get used to doing right and not just working. The worst mistakes are the ones the compiler doesn’t complain about.

And the variable isn’t even necessary.

Would be better off:

static void PrepararTerreno() => Directory.CreateDirectory($"C:/Users/{Environment.UserName}/Documents/Cadastro");

Or

static void PrepararTerreno() => Directory.CreateDirectory($@"C:\Users\{Environment.UserName}\Documents\Cadastro");

Or else

static void PrepararTerreno() => Directory.CreateDirectory($"C:\\Users\\{Environment.UserName}\\Documents\\Cadastro");

I put in the Github for future reference.

  • I never get tired of reading your answers. a big fan as always kk

  • Thanks for the explanation and the advice mate,helped a lot

  • @Leonardolopes you can see on the [tour] the best way to say thank you. Note that you can vote for all answers you want on the entire site, but you can only accept one answer in a question you asked. Analyze both and see if one of them deserves an acceptance. In general it is good that one is accepted, not the choice is yours of what is best for you.

1

About Error CS0029:

The compiler requires an explicit conversion. For example, it may be need to convert a value r to the same type as a value l. Or, you must provide conversion routines to support certain operator burden.

You need to tell the compiler that you want that output in a specific way, in this case in the string format.

You could do as @Augusto Vasques quoted in the comment using the string.Format:

string diretorio = string.Format("C://Users//{0}//Documents//Cadastro", Environment.UserName);

or using the string.Concat:

string diretorio = string.Concat("C://Users//", Environment.UserName, "//Documents//Cadastro");

There are several ways to build a string, so if you want more definitions about concatenação de string you can have a quick summary on What is the most appropriate way to concatenate strings? here in the forum or consult official documentation.

Browser other questions tagged

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