0

i have a class where i am assigning currency variable as follows:

example currency formats USD,EUR etc etc

public class codes{
  public char currency{get;set;}
}

so i have another class that calls codes to assign it into the currency variable as follows:

public async Task<Codes> ExecuteAsync(hCommand command){
Codes c = new Codes();
//i do some coding to get the currency back then i assign it as follows from a string
 c.currency = Convert.ToChar(currency);
}

but it throws an error at that line System.FormatException: 'String must be exactly one character long.' and the output is returned as "currency": "\u0000"

how can i get it to be a char(3)

6
  • 1
    You should use string instead of char Commented Mar 20, 2020 at 11:58
  • why not this: public string currency { get; set; }? since you want value like USD or EUR Commented Mar 20, 2020 at 11:59
  • no i cant use it as a string which it currently is because the requested end point requires it as a char character Commented Mar 20, 2020 at 12:10
  • Where is that currency variable comming from? What is it set to? Why is it a string, if you plan to assign it to a char? Why not use a Enumeration? Commented Mar 20, 2020 at 12:24
  • 1
    C# does not have char(3) as you call it. You can use string, or you can create your own type Currency that holds a string which is assured to have the right format (like, not too long). Commented Mar 20, 2020 at 13:11

2 Answers 2

1

I think that if you want to store something like a currency, "USD" for example, string is the correct type to do so, not char. Char stores a single character.

In any case, if you still want to use char, you should, for this particular case, use an array of chars:

public class codes{
    public char[] currency { get; set; }
}

And then use ToCharArray instead:

c.currency = Convert.ToCharArray();
Sign up to request clarification or add additional context in comments.

Comments

1

You can declare your class like

public class Сodes
{
    public string Сurrency { get; set; }
}

and use in the following way

Codes c = new Codes();
c.Сurrency = currency;

String in C# represents a readonly sequence of char elements, you can't assign string like "USD" to a single character.

Another option is to use a char array for that, like

public class Сodes
{
    public char[] Сurrency { get; set; }
}

and use ToCharArray to assign a value

Codes c = new Codes();
c.Сurrency = currency.ToCharArray();

It's a good rule to use a PascalCase for classes and properties names (btw, you codes class declaration doesn't match the usage example Codes).

You can also refer to this question 3 Digit currency code to currency symbol, if want to convert currency code to currency symbol, like $

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.