Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types and Variables Part D – Common Methods (ToString and Parse)

Similar presentations


Presentation on theme: "Data Types and Variables Part D – Common Methods (ToString and Parse)"— Presentation transcript:

1 Data Types and Variables Part D – Common Methods (ToString and Parse)
Lecture Set 4 Data Types and Variables Part D – Common Methods (ToString and Parse) Text Boxes, Input Focus

2 Objectives Learn about two Common Methods for converting
To a formatted string (Tostring) from a primitive (or other) value From a string to a primitive value (Parse) Examine Imports Future Value Learn how to use the TextBox control Define the order in which control instances get input focus 11/10/2018 7:35 PM

3 Formatting Output Strings
There are a number of common functions that can be used to convert and format data. These include the ToString method, which converts an argument to a string which can be specially formatted Also, a Parse method which converts a string to an equivalent data value Visual Studio supports named formats to supply common formatting -- currency, for example Data can be formatted with format specifiers Custom formats can be created with placeholder characters 11/10/2018 7:35 PM

4 The ToString and Parse Methods
Both are conversion methods common to all data types defined by .NET So we can write … Decimal.Parse (“ D”); … DateTime.Parse (“Jan 30, 2007”); How does the compiler know which version of these functions to use? What would happen if we wrote … Decimal.Parse (“Jan 30, 2007”); 11/10/2018 7:35 PM

5 Formatting Data Using ToString
Here we see another version of the ToString method Previously we had seen a version of ToString that took a primitive type value as an argument Now we have a version of the Common Method ToString that takes a format descriptor (or no argument) With the format descriptor, the specified value to converted to a formatted string A format specifier consists of a single alphabetic character followed by an optional numeric precision indicator  11/10/2018 7:35 PM

6 Format Descriptors Format descriptors may be
Specifier strings consisting of a single alphabetic character followed by an optional numeric precision specifier A format string or mask consisting of special placeholder characters and literal characters value.ToString(“C”); value.ToString (“D6”); value.ToString(“$###,###.00”); Examples next …  11/10/2018 7:35 PM

7 Format Specifiers 11/10/2018 7:35 PM

8 Placeholder Characters
11/10/2018 7:35 PM

9 Format Strings (Examples)
11/10/2018 7:35 PM

10 The FV Function The FV function calculates the future value of an investment (like do we really have any?) The first argument contains the interest rate The second argument contains the number of periods The third argument contains the amount of the periodic payment The optional fourth argument contains the present (initial) value of the investment The optional fifth argument defines whether payments are made at the beginning or end of the period Slide 11 11/10/2018 7:35 PM

11 The FV Function (Example) 1
Calculate the future value of $1, for 12 periods at 1% per period double monthRate = 0.01; int monthTerm = 12; double initialValue = ; double futureValue; futureValue = FV(0.01, 12, 0, 1000); futureValue = FV (monthRate, monthTerm, 0, initialValue);

12 The FV Function (Example) 2
But the FV functions returns a negative value So what do we do? Using System.Convert; ‘Why? InitialValue = ToDouble(txtInitialValue.Text); AnnualInterestRate = … ; YearlyTerm = ToInt32(txtYearlyTerm.Text); MonthlyTerm = … FutureValue = FV(…) FutureValue = System.Math.Abs(FutureValue); // Convert to string in currency format and store in box // Note different ways to call operators such as toString lblFutureValue.Text = FutureValue.ToString(“C”); 11/10/2018 7:35 PM

13 The TextBox Control (Introduction)
The TextBox control gets input and displays output The control derives from the TextBoxBase class Other textual controls include the MaskedTextBox and RichTextBox controls 11/10/2018 7:35 PM

14 The TextBox Control (Properties) 1
The BorderStyle property defines the style of the border The ForeColor and BackColor properties define text and background colors The Enabled property defines whether the control instance can get input focus The ReadOnly property controls whether the text in the control instance can be copied to the clipboard The MultiLine property defines whether text will appear on multiple lines The maximum number of characters is defined in the MaxLength property 11/10/2018 7:35 PM

15 The TextBox Control (Properties) 2
The ScrollBars property defines whether scroll bars appear Scroll bars only appear when the MultiLine property is True The Text property stores the visible text The TextAlign property defines how the text is aligned in the control instance The size and position is defined by the X, Y, Height, and Width properties 11/10/2018 7:35 PM

16 The TextBox Control (Methods and Events)
The Focus method is used to set input focus to a text box The Enter event fires when the control instance gets focus The Leave event fires when the control instance loses focus 11/10/2018 7:35 PM

17 Introduction to Tab Order
Every form has exactly one active control instance at run time This is the control instance having input focus The order in which control instances get focus is called the tab order The tab order is set using the TabIndex property The tab order can also be set using a visual designer 11/10/2018 7:35 PM

18 Setting the Tab Order 11/10/2018 7:35 PM

19 Other Issues (optional)
Read about enumerations – nothing new here Nullable types – can be used to indicate that a variable has never been defined – in other words that the value is unknown Used only for value variables Int? myVariable = null; // null is a keyword You should never depend on a language system to initialize data for you. Do it yourself explicitly If you do this carefully you will probably not need to use nullable types NOTE: Reference variables are by default assigned the null value 11/10/2018 7:35 PM


Download ppt "Data Types and Variables Part D – Common Methods (ToString and Parse)"

Similar presentations


Ads by Google