try.immbar.com

.NET/ASP.NET/C#/VB.NET PDF Document SDK

As we saw earlier, a method is a named block of code. We wrote a method already the Main method that runs when our program starts. And we used methods provided by the .NET Framework class library, such as Console.WriteLine and File.ReadAll Lines. But we haven t looked at how and why you would introduce new methods other than Main into your own code. Methods are an essential mechanism for reducing your code s complexity and enhancing its readability. By putting a section of code into its own method with a carefully chosen name that describes what the method does, you can make it much easier for someone looking at the code to work out what your program is meant to do. Also, methods can help avoid repetition if you need to do similar work in multiple places, a method can help you reuse code. In our race car example, there s a job we may need to do multiple times: reading in numeric values from a file. We did this for timing information, but we re going to need to do the same with fuel consumption and distance. Rather than writing three almost identical bits of code, we can put the majority of the code into a single method. The first thing we need to do is declare the method we need to pick a name, define the information that comes into the method, and optionally define the information that comes back out. Let s call the method ReadNumbersFromFile, since that s what it s going to do. Its input will be a text string containing the filename, and it will return an array of double-precision floating-point numbers. The method declaration, which will go inside our Program class, will look like this:

add qr code to ssrs report, ssrs upc-a, create barcode image vb.net, ssrs gs1 128, ssrs ean 13, ssrs pdf 417, c# remove text from pdf, replace text in pdf using itextsharp in c#, ssrs fixed data matrix, itextsharp remove text from pdf c#,

A model is a representation of something meaningful. It s not necessarily something physical but something real: a business concept or an API that s difficult to work with. When we write object-oriented software, we create classes that make up this representation. We can create our representation so that when we use it we re working in a natural human language, like English or Spanish or business jargon, instead of in programming language constructs like Booleans, meaningless strings, and integers. When working with a user interface (UI) framework like ASP.NET MVC, the UI is the complex problem that we manage. It s the data in a window, a form submission from a user, the options in a select list. Whereas model is an overloaded term in software, this chapter focuses on the presentation model the model that represents the screen and user input of an application.

static double[] ReadNumbersFromFile(string fileName)

As you may recall from the discussion of Main earlier, the static keyword indicates that we do not need an instance of the containing Program type to be created for this method to run. (We ll be looking at nonstatic methods in the next chapter when we start dealing with objects.) C# follows the C-family convention that the kind of data coming out of the method is specified before the name and the inputs, so next we have double[], indicating that this method returns an array of numbers. Then we have the name, and then in parentheses, the inputs required by this method. In this example there s just one, the filename, but this would be a comma-separated list if more inputs were required. After the method declaration comes the method body the statements that make up the method, enclosed in braces. The code isn t going to be quite the same as what we ve seen so far up until now, we ve converted the text to numbers one at a time immediately before processing them. But this code is going to return an array of numbers, just like File.ReadAllLines returns an array of strings. So our code needs to build up that array. Example 2-17 shows one way of doing this.

4

static double[] ReadNumbersFromFile(string fileName) { List<double> numbers = new List<double>(); using (StreamReader file = File.OpenText(fileName)) { while (!file.EndOfStream) { string line = file.ReadLine(); // Skip blank lines if (!string.IsNullOrEmpty(line)) { numbers.Add(double.Parse(line)); } } } return numbers.ToArray(); }

Consider a screen that shows a table to the user, as shown in figure 2.1. This table is the product of our software development. It deserves to exist as a firstclass object in our system. This will allow us to intentionally create it and to maintain it after its initial development. A first-class object representing this table, or rather representing each row, will also allow our view code to easily display the table itself. In listing 2.1 we have a simple model class for the table in figure 2.1.

This looks pretty similar to the example while loop we saw earlier, with one addition: we re creating an object that lets us build up a collection of numbers one at a time a List<double> It s similar to an array (a double[]), but an array needs you to know how many items you want up front you can t add more items onto an existing array The advantage of a List<double> is that you can just keep adding new numbers at will That matters here because if you look closely you ll see we ve modified the code to skip over blank lines, which means that we actually don t know how many numbers we re going to get until we ve read the whole file Once you re done adding numbers to a list, you can call its ToArray() method to get an array of the correct size.

   Copyright 2020.