Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you declare a variable, its data type can either be a simple value or a complex object. Simple values include data types like those I showed in a previous video, including strings, Booleans and numeric values. Complex objects are instances of classes. In Object Oriented terminology, a class is a definition of a bit of software and when you create an instance of that class it's called an object. In this demonstration I'll show you how to declare an instance of a class where an object in both C# and in Visual Basic, and the particular type of object that will use is a DateTime value. This is represented as the DateTime class and you use the same class in either language.
I will start will C#. I'll open the page ProgrammingCSharp.aspx and then I'll save it as ObjectCSharp.aspx. Then I'll place the cursor inside the runbutton_Click function and I'll first create an instance of the DateTime class using the following syntax. I'll start with DateTime and I'll assign the variable name as dt for date-time. When you instantiate or create an instance of a class, you use the new keyword.
This new keyword is used in both Visual Basic and in C#. And then you call a function that has the same name as the class itself. This is known as the constructor method of the class. I'll use DateTime and I'll follow it with an opening and closing parenthesis. Now I'll output the value of the DateTime object using my output function. The DateTime object is a complex object. For most complex objects, if you want to turn it into a string, you call a function or a method of a class called ToString. So I'll pass in dt.ToString. I'll save the changes and I'll run the page. I'll click Run Code and I'll see the results.
Notice that when you create a new instance of the DateTime class, it automatically represents the first day of the first month of the first year at midnight. If you want this same object to represent the current date-time, the syntax looks a little different. I'll close the browser and return back to the function code. Now I'll create another DateTime object. Once again setting the data type of DateTime and I'll call this one nowDT. The DateTime class has a property called now. This is known as a static property. You refer to it as a member of the class definition itself rather than as a property of an instance of the class.
When you refer to static property, it look like this and notice that the now property is displayed as an available member of the class. The now property is not a function, it's a property, and so you don't put the parenthesis at the end. I will call the output function again and I'll pass in nowDT.ToString. I'll save the changes and run the page again. I'll Run Code and you will see that the first DateTime, which was instantiated using the new keyword, represents the default DateTime, the first month of the first of the first year and so on, whereas the version of the DateTime object that's created from the now property represents the current date-time and you see the date and the time on which I recorded this video.
Now I'll do the same thing in Visual Basic. I'll open the page ProgrammingVB.aspx and I'll save the file as ObjectVB.aspx. I'll place the cursor inside the runbutton_Click function and now I'll declare an instance of the DateTime class using Visual Basic syntax. I'll start with Dim, meaning that it's variable that's local to the function. I'll put in the As operator and this time instead of using DateTime I use the Visual Basic version of this variable which is simply date.
Then the rest of the code looks pretty much the same as in Visual Basic. I'll use the keyword new and then I'll use Date as a method. Then I'll output the value of the DT object. In Visual Basic you don't need the ToString function. So I'll just output DT and I'll run the page, click the Run Code button and this time you only see the time. So now if I would like the same level of functionality as I had with C#, instead of using the Visual Basic date object, I'll use the underlying .NET Framework object which in Visual Basic is known by its fully qualified prefix, System.DateTime.
So I'll set that as the data type and I'll call the appropriate constructor function. Then I'll go to the call to the output method and I'll add DT.ToString. I'll save the changes and run the page again, click the Run Code button and now I'm seeing the same results as I did in C#. I follow with this same pattern to get the current date and time. I'll type in Dim dtNow as As System.DateTime and once again I'll use the now property of the DateTime class. System.DateTime.Now and then I'll output the ToString version of that object, dtNow.ToString. I'll save my changes and run the page, and you will see once again that I'm showing the date-time on the server at the moment that I run the page.
So that's a look at how to use the System.DateTime class in either C# or Visual Basic. The .NET Framework contains thousands of class definitions. Once you understand the basic syntax of working in ASP.NET, expanding your ASP.NET programming skills is largely a matter of learning about new classes and how they work in your programming language.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98584 Viewers
61 Video lessons · 85881 Viewers
71 Video lessons · 69763 Viewers
56 Video lessons · 102070 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.