Reputation: 180
I am learning c#, I am creating a program that asks for user's first name, if the first letter is not capitalised the program will automatically capitalise it, I wanted to put this into a method but am having trouble with returning the value from the method into the Main method.
Here is the code I am trying
using System;
using System.Text;
namespace ConsoleApp1
{
class Program
{
//Method for letter capitalisation
static string capitalise(string userName, char letter) { //users name, letter being replaced
letter = char.ToUpper(letter); //capitalise first letter
StringBuilder sb1 = new StringBuilder(userName);
sb1.Remove(0, 1); //removes first letter
sb1.Insert(0, letter); //inserts first letter from letter object
userName = sb1.ToString(); //writes string builder into string
return userName; //returns method value to the object
}
static void Main(string[] args)
{
string firstName;
string lastName;
Console.WriteLine("What is your First Name?");
firstName = Console.ReadLine();
Console.WriteLine("What is your Last Name?");
lastName = Console.ReadLine();
char capitalFirst = firstName[0]; // writes first letter of the name as a char
if (char.IsLower(capitalFirst)) { //checks if char is lowercase
capitalise(firstName, capitalFirst); //calls for method and writes parameters
firstName = capitalise(userName); // <--- here i am trying to write the user first name as a result from the capitalise method
}
//Same for last name...
Console.WriteLine(firstName + " " + lastName);
}
The Error: The name username does not exist in the current context
Upvotes: 2
Views: 1053
Reputation: 2313
It seems you may be being confused by some basic programming semantics, so I'll try to break this down for you.
For one, you don't "pass" values from methods to the Main
function of a program. You return values (hence the return statement)
You pass parameters to methods and functions (for example: Test(1,2,3);
, I've passed the int
s 1, 2 and 3 to the method Test
). I bring this up because I believe that you may be under the impression that you are modifying the scope/variables in Main
by calling capitalise
, which is not (and should not) be the case.
Second, you need to understand scope, variables only exist in the scope within which they are declared; scope is more complex than this, but simply put, you can think of anything within curly brances ({ these }) as being a new scope; the only things that exist in that new scope are the things that exist above that scope and inside of it.
Your Main
method and the capitalise
method have two different scopes, you cannot use userName
in the Main
method because userName
only exists in capitalise
.
So, the solution would be to update your Main
method like so:
static void Main(string[] args)
{
string firstName;
string lastName;
Console.WriteLine("What is your First Name?");
firstName = Console.ReadLine();
Console.WriteLine("What is your Last Name?");
lastName = Console.ReadLine();
char capitalFirst = firstName[0]; // writes first letter of the name as a char
char capitalLast = lastName[0];
if (char.IsLower(capitalFirst) || char.IsLower(capitalLast)) { //checks if char is lowercase
firstName = capitalise(firstName, capitalFirst);
lastName = capitalise(lastName, capitalLast);
}
//Same for last name...
Console.WriteLine(firstName + " " + lastName);
}
Upvotes: 2
Reputation: 118
You can replace capitalize function for ToTitleCase:
TextInfo textInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
string resultStr = textInfo.ToTitleCase("firstname secondname");
Result of resultStr variable is = "Firstname Secondname"
Upvotes: 2
Reputation: 199
Because you don't have any variable with name "userName" in Main. You need take return of capitalise and then put in some variable. For example:
string userName = capitalise(firstName, capitalFirst); //calls for method and writes parameters
firstName = capitalise(userName); // <--- here i am trying to write the user first name as a result from the capitalise method
Upvotes: 1
Reputation: 29051
You're really overthinking this.
As the other answers and comments mentioned, yours is a problem of variable scope. In this case, "scope" refers to the block of code where a variable is declared and is therefore accessible.
Any variable declared inside of a brace pair ({
, }
) is only accessible inside of that pair. This area is referred to as a block. Method parameters - like username
and letter
in your example, are only accessible within the body of the method, conveniently also delimited by braces.
For instance, this is valid:
{
string s;
s = "blah;
Console.WriteLine(s);
}
while this is not
{
string s;
s = "blah"
}
Console.WriteLine(s); // s has gone out of scope and does not exist here.
In your sample, there are a number of issues. You're calling capitalise
in two different ways, one of which - with only one parameter, while the method expects two - is invalid.
But, the immediate problem is this:
firstName = capitalise(userName);
userName
is invalid here because it is an argument of capitalize
. Main
knows nothing of that userName
, because it is declared in a different scope.
You might have meant:
firstName = capitalise(firstName, capitalFirst);
Complete working sample below. Use of StringBuilder
is overkill and unnecessary in this case. StringBuilder
is typically used when you need to build very large strings by concatenating a bunch of stuff. String concatenation requires some allocation and copying and potentially triggers the garbage collector, which can be a significant performance hit, and can consume tons of memory in extreme circumstances.
Good luck!
using System;
public class Program
{
public static string Capitalize(string s)
{
if(string.IsNullOrEmpty(s))
return string.Empty;
string result = char.ToUpper(s[0]) + s.Substring(1);
return result;
}
public static void Main()
{
string firstName;
string lastName;
Console.WriteLine("What is your First Name?");
firstName = Console.ReadLine();
Console.WriteLine("What is your Last Name?");
lastName = Console.ReadLine();
firstName = Capitalize(firstName);
lastName = Capitalize(lastName);
Console.Write("Hello, {0} {1}.", firstName, lastName);
}
}
Upvotes: 3