-
Code from MS Site
I'm studying the Console.Write formatting and came across this code on the M$ website. But I can't say that I have enough experience with VB yet to copy the code therein and get it working. Can someone enlighten me please on how this is made to work.
-
Re: Code from MS Site
I followed your link and it didn't work. Anyway, the formatting rules are fairly simple for Console.Write and they are the same as String.Format. You specify template text that contains placeholders of the form {N} where N is a number that refers to the index of subsequent arguments. For instance this:
Code:
Console.Write("|{0}|{1}|{2}|{0}|", "Hello", 1, True)
will display:
Quote:
|Hello|1|True|Hello|
The list of arguments following the template text are treated as an array where "Hello" is at index 0, etc. Note that {0} appears twice in the template text so Hello appears twice in the output.
You can also include format specifiers in the placeholders, e.g.
Code:
Console.Write("{0} was born on {1:MMMM d, yyyy}", myName, myDateOfBirth)
The format specifier can be anything that you can pass to the ToString method of the same data type. In this case a Date is being formatted so it will accept any format specifier that Date.ToString will accept. MSDN can tell all the standard and custom format strings for dates, times and numbers.
-
Re: Code from MS Site
Wow, that's weird on the link. Here is the code from the site:
Code:
' This code example demonstrates the Console.WriteLine() method.
' Formatting for this example uses the "en-US" culture.
Imports System
Imports Microsoft.VisualBasic
Class Sample
Public Enum Color
Yellow = 1
Blue = 2
Green = 3
End Enum 'Color
Private Shared thisDate As DateTime = DateTime.Now
Public Shared Sub Main()
Console.Clear()
' Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers")
Console.WriteLine("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _
"(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
"(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
"(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
"(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
"(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
"(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
- 123, - 123.45F)
' Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers")
Console.WriteLine("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _
"(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _
"(t) Short time: . . . . . . . {0:t}" & vbCrLf & _
"(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _
"(f) Full date/short time: . . {0:f}" & vbCrLf & _
"(F) Full date/long time:. . . {0:F}" & vbCrLf & _
"(g) General date/short time:. {0:g}" & vbCrLf & _
"(G) General date/long time: . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _
"(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _
"(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _
"(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _
"(U) Universal full date/time: {0:U}" & vbCrLf & _
"(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _
thisDate)
' Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers")
Console.WriteLine("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
"(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
Color.Green)
End Sub 'Main
End Class 'Sample
I realize that this is not as simple as copying and pasting this into a new Console App, but I am just not able to figure out how to structure the project with this to get it working. I tried creating a new class...but then got nothing.
-
Re: Code from MS Site
Copy the contents of that Sample class into the module created in a new Console application.
-
Re: Code from MS Site
Ok, thanks. I am starting to see through the fog of structuring code now. Which brings me to my next question on this same topic. I have a project which I'm adding more code to now. I am about to reuse some code I previously plagiarized, er, uh, that is authored myself....on automating Excel. To keep things organized I want to place that code in a separate module, or class. I'm reading on classes now but don't seem to grasp when one should use a class or module. They seem to function the same to me, at least at my level of understanding it right now, which is still low in the learning curve.
-
Re: Code from MS Site
The main difference between a class and a module is the fact that you can instantiate a class, i.e. create an object of that type, whereas you can't a module. Given that VB.NET is an object-oriented language, you obviously want to create objects, so it makes sense that you need classes. For instance, when you design a form in a VB.NET project, you are creating a class. When you create a new Windows Forms Application project and it creates Form1 for you, that's the Form1 class, inheriting the Form class. In order to show a form to the user, an instance has to be created. That's how it is with all classes. When you get data from a database you generally create an instance of the DataTable class to hold the data.
Now, in the vast majority of cases, it's appropriate to define a class, create instances of that class and use its members. For instance, every string you use in VB.NET is an instance of the String class. You create one and then you might get its Length property or call its Substring method. Sometimes though, you want to group together functionality that doesn't really represent an object, but rather works on other objects. For instance, consider the File class from the System.IO namespace. You can't actually create an instance of the File class to represent a file. Rather, it contains operations that you can perform on files. Because most of the .NET Framework was written in C#, File was implemented as a static class. That's the C# equivalent to a VB module so, if you were writing that in VB, you'd define a module rather than a class.
-
Re: Code from MS Site
I'm starting to get the hang of it. I guess a little advice is needed on my part. The programs I'm writing are to automate process and the users are not involved in any way, unless you count me or some other tech person who would be starting and stopping them. There are some forms with buttons and textboxes so that monitoring can take place, but for the most part these are completely hands off, no user interface required (or allowed) whatsoever. I am adding a new module to my project this morning which will be passed a stream of data, parse it and then open an instance of Excel. It will populate some cells in the Excel file and then gather up the calculations made by Excel. This data will then be written to a file, or streamed back to the webpage which sent it. My thinking is that this Excel stuff should be in a module. Or would there be advantages to having it in a class?
-
Re: Code from MS Site
It should be a class, or perhaps even multiple classes. I would say that every 10 or 15 projects or perhaps even more I find reason to add one module. Everything else is a class, with the occasional structure thrown in too. Always think class first and only use something other than a class if you have a specific reason to do so.