How can I refactor these two lines to one statement?

Kevin

I have the following code:

// TryGetAttributeValue returns string value or null if attribute not found
var attribute = element.TryGetAttributeValue("bgimage");

// Convert attribute to int if not null
if (attribute != null) BgImage = convert.ToInt32(attribute);

The thing I don't like is that I have to create a temp variable, attribute, in order to test if it's null or not, and then assign the value to the BgImage variable, which is a nullable int.

I was hoping I could figure out a way to write it all on one line, but I cannot figure a way. I even tried using a ternary statement, but got nowhere:

if (element.TryGetAttributeValue("bgimage") != null) ? BgImage = //Convert result to int :  else null; 

Realistically, my original two lines of code do the job. I was just hoping to pare it down to one line. But, if anyone knows how to do what I'm trying to accomplish, I'd love to learn how.

Sergey Berezovskiy

I recommend you to use Linq to Xml for parsing Xml (according to your attempt you have BgImage as nullable integer):

BgImage = (int?)element.Attribute("bgimage");

You also can assign some default value if BgImage is not nullable:

BgImage = (int?)element.Attribute("bgimage") ?? 0;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i refactor these lines of code?

From Dev

How can I refactor my if-else statement?

From Dev

How do I refactor two case value expressions into one?

From Dev

c# - How could I refactor this switch statement to use minimum number code lines?

From Dev

How can I make UILabel or UIButton with two lines , the first with one character and the second with two characters?

From Dev

How to refactor two similar methods in one

From Dev

How can I capture Sublime 2 build output when it it sometimes on one line, sometimes on two lines

From Dev

How can I print the lines in a file that contain two regex where one is the substring of another

From Dev

Groovy: How can I read/compare lines from two files, without loading one or both files into memory

From Dev

How can I check two conditions in an if statement?

From Dev

How I can divide two statement in PHP

From Dev

How can I increase one of two numbers in a comparative statement with a button click

From Dev

How can I run two or more tasks by using just one input statement?

From Dev

How do I refactor these two functions?

From Java

Can I delete entries from two tables in one statement?

From Dev

Can I put two divs in if statement and close it with one div?

From Dev

How do I combine two lines into one list?

From Dev

How to refactor nested if statement

From Dev

How can I refactor this code to be more concise?

From Dev

How can I refactor a set of ugly if statements?

From Dev

How Can I Refactor To Avoid Duplication?

From Dev

how can I join two regex into one?

From Dev

How i can bind two substring to one?

From Dev

How can I combine two userscripts into one?

From Dev

How can I combine these two queries into one?

From Dev

How can I combine two QStrings into one?

From Dev

How can I receive one OR two input?

From Dev

how can combine the two sqlite statements into one statement?

From Dev

How can can I combine two IF statement in Excel?

Related Related

  1. 1

    How can i refactor these lines of code?

  2. 2

    How can I refactor my if-else statement?

  3. 3

    How do I refactor two case value expressions into one?

  4. 4

    c# - How could I refactor this switch statement to use minimum number code lines?

  5. 5

    How can I make UILabel or UIButton with two lines , the first with one character and the second with two characters?

  6. 6

    How to refactor two similar methods in one

  7. 7

    How can I capture Sublime 2 build output when it it sometimes on one line, sometimes on two lines

  8. 8

    How can I print the lines in a file that contain two regex where one is the substring of another

  9. 9

    Groovy: How can I read/compare lines from two files, without loading one or both files into memory

  10. 10

    How can I check two conditions in an if statement?

  11. 11

    How I can divide two statement in PHP

  12. 12

    How can I increase one of two numbers in a comparative statement with a button click

  13. 13

    How can I run two or more tasks by using just one input statement?

  14. 14

    How do I refactor these two functions?

  15. 15

    Can I delete entries from two tables in one statement?

  16. 16

    Can I put two divs in if statement and close it with one div?

  17. 17

    How do I combine two lines into one list?

  18. 18

    How to refactor nested if statement

  19. 19

    How can I refactor this code to be more concise?

  20. 20

    How can I refactor a set of ugly if statements?

  21. 21

    How Can I Refactor To Avoid Duplication?

  22. 22

    how can I join two regex into one?

  23. 23

    How i can bind two substring to one?

  24. 24

    How can I combine two userscripts into one?

  25. 25

    How can I combine these two queries into one?

  26. 26

    How can I combine two QStrings into one?

  27. 27

    How can I receive one OR two input?

  28. 28

    how can combine the two sqlite statements into one statement?

  29. 29

    How can can I combine two IF statement in Excel?

HotTag

Archive