0

I want to append a character to a long integer, using the below code:

if (strArrIds[1].Contains("CO")) { long rdb2 = Convert.ToInt64(strArrIds[1].Substring(strArrIds[1].Length - 1)); assessmentEntity.RatingType = rdb2; } 

If rdb2 = 5, I want to append a L to this value, like rdb2 = 5L.

Any ideas? Thanks in advance.

4
  • string mynewstring = "L" + rb2.ToString(); Commented Jan 23, 2014 at 10:18
  • What is the datatype of "assessmentEntity.RatingType"? Commented Jan 23, 2014 at 10:22
  • assessmentEntity.RatingType data type is long Commented Jan 23, 2014 at 10:28
  • 1
    If so, you don't need to append an 'L' as you'd do with literal assignments. You append an 'L' only in a case like: long x=5L, but at run-time, you don't append an L. You either use Convert or Parse or TryParse. Commented Jan 23, 2014 at 10:31

3 Answers 3

1

You can using Long.Parse instead Convert.ToInt64 to get the long and you wont need to append L to make it long

if (strArrIds[1].Contains("CO")) { long rdb2 = long.Parse(strArrIds[1].Substring(strArrIds[1].Length - 1)); assessmentEntity.RatingType = rdb2; } 
Sign up to request clarification or add additional context in comments.

Comments

0

If you are processing a lot of these, and if this item will be used for further processing, you could consider turning this into a class which might be easier to manage. This would also allow you to more easily customise your ratings.

I'm thinking maybe a factory might serve you well also which could instantiate and return your assessment entity. You could then leverage a dependency injection strategy for any other functionality.

Its a little hard to tell what you require this for without a bit more context.

If this is a once off, I would refactor to

assessmentEntity.RatingType = strArrIds[1].Contains("CO") ? String.Concat(long.Parse(strArrIds[1].Substring(strArrIds[1].Length - 1)).ToString(), "L") : "0N"; 

Assuming "0N" is some other default rating..

Comments

0

You do not need an L here. That is only for literals of type long appearing in the C# source.

You could do something like:

string str1 = strArrIds[1]; if (str1.Contains("CO")) { long rdb2 = str1[str1.Length - 1] - '0'; if (rdb2 < 0L || rdb2 > 9L) throw new InvalidOperationException("Unexpected rdb2 value from str1=" + str1); assessmentEntity.RatingType = rdb2; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.