Type Conversion

Ace your homework & exams now with Quizwiz!

What are the three kinds of type conversion?

Implicit, explicit and non-compatible type conversion.

How do you handle exceptions?

Use Try and Catch. Type Try, then wrap your code in the try wrapper that appears. In the Catch wrapper that appears, write a message to the user to explain the exception.

When should you use explicit type conversion? What is the syntax?

Use explicit type conversion when the data type you're converting to is smaller than the data type you're converting from. You have to explicitly tell the code that you're ok with any data loss that happens. You do this by sort of re-declaring the data type you want to convert to preceding the value. Example syntax: Float f= 10.56 Short x = (Short)f

When can you use implicit type conversion? What is the syntax?

Use implicit type conversion when the data type you're converting to is larger (in bytes) than the type you're converting from (because when you convert to a larger data type, there won't be data loss in the conversation. Example syntax: Int x = 2 Long L = x

How do you convert any data type (that is not a string) to a non-compatible primitive type?

Use the Convert class which is part of the System Namespace. This class has a bunch of methods to convert data types to various other data types and they all start with: to. Example syntax: String s = "1" Int x = convert.ToInt32(s)

How do you convert a string to a non-compatible primitive type?

Use the parse method. This method will guess the data type you want to convert to based on what's in the string. Example syntax: String d = "1" Int x = int.Parse(d)

When should you use non-compatible type conversion?

When types are not compatible, like a string and an int (text and numbers).

What are unhandled exceptions?

When you try to convert to a new data type, but the value exceeds the limits of that new data type (e.g. a value of 3,456 is too much for a byte which can only handle 0 - 255).


Related study sets

Match the terms that pertain to urinalysis with their correct meanings

View Set

A+ 220-801 Objective 2.9: Compare and contrast network devices and their functions and features

View Set

Unit 4- Nervous, senses, endocrine

View Set

Unit Circle sine, cosine, tangent

View Set