| hoofdstuk |
25. 26. 27.  |
| onderwerp |
26.4. 26.5. My Namespace 26.6.  |
| rubrieken | 



|
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
My bied ons, door het creëren van een aantal default object instanties, op eenvoudige wijze toegang tot informatie over onze applicatie en de runtime.
De rootmembers van My worden als objecten ter beschikking gesteld en kan je gebruiken als een namespace of een klasse met gedeelde ( Shared ) members. Deze My biedt ons in hoofdzaak op vereenvoudigde wijze toegang tot members van enkele klassen uit de .NET Framework klassenbibliotheek.
Een overzicht van de My objecten : My : - My.Computer : * My.Computer.Audio * My.Computer.Clipboard * My.Computer.Clock * My.Computer.FileSystem * My.Computer.Info * My.Computer.Keyboard * My.Computer.Mouse * My.Computer.Network * My.Computer.Ports * My.Computer.Registry - My.Application : * My.Application.Log * My.Application.Info - My.Forms - My.Log - My.Request - My.Response - My.Resources - My.Settings - My.User - My.WebServices Welke van deze objecten de My namespace ter beschikking stelt is afhankelijk van het type project waarin je je bevindt. My.Forms is bijvoorbeeld beschikbaar in een Windows Forms Application, maar niet in een Console.Application. My.Request en My.Response bijvoorbeeld zijn dan enkel beschikbaar in projecten van het type Web Site. 26.5.1. My.Application ObjectMy.Application is bruikbaar om informatie gerelateerd aan de huidige applicatie op te vragen. We zouden bijvoorbeeld in een Console Application de commandline argumenten kunnen opvragen via de My.Application.CommandLineArgs property. Deze levert ons de argumenten in System.Collections.ObjectModel.ReadOnlyCollection(Of String) vorm op. Visual Basic 2010 Broncode Class MyApplicationCommandLineArgsExample Public Shared Sub Main() Dim arguments As _ System.Collections.ObjectModel.ReadOnlyCollection( Of String) = _ My.Application.CommandLineArgs Console.WriteLine( "This application is invoked with " & _ "following arguments :") For Each argument As String In arguments Console.WriteLine( "- " & argument) Next Console.ReadLine() End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Om bovenstaand voorbeeld te testen kan je op de commandprompt volgende opdracht geven : MyApplicationCommandLineArgsExample.exe test1 "test2 test3" test4 Eenvoudiger nog om dit te testen, zonder dat men rechtstreeks in de commandprompt een opdracht hoeft te geven, is werken via de Debug Start Options. Ga naar de projecteigenschappen en vul bij de optie Command line arguments van rubriek Start Options op het tabblad Debug volgende waardes in : test1 "test2 test3" test4 Hoe je het voorbeeld ook test, volgend resultaat wordt bekomen : Console Application Output This application is invoked with following arguments :
- test1
- test2 test3
- test4 Deze CommandLineArgs property levert ons op iets eenvoudiger wijze dan de Shared method System.Environment.GetCommandLineArg() de argumenten op. Visual Basic 2010 Broncode Class EnvironmentGetCommandLineArgsExample Public Shared Sub Main() Dim arguments As String() = Environment.GetCommandLineArgs() Console.WriteLine( "This application is invoked with " & _ "following arguments :") For Each argument As String In arguments Console.WriteLine( "- " & argument) Next Console.ReadLine() End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output This application is invoked with following arguments :
- C:\TestMy\MyApplicationCommandLineArgsExample\.exe
- test1
- test2 test3
- test4 Bovenstaand voorbeeld illustreert hoe we in dit geval een String() terug krijgen, die dan ook nog eens de naam van de executable weergeeft. Dit is informatie waar je meestal niet in geïntresseerd bent. boven
26.5.2. My.Application.InfoEen ander voorbeeld is My.Application.Info, deze levert ons in Microsoft.VisualBasic.ApplicationServices.AssemblyInfo vorm informatie over de ingeladen assembly op : Visual Basic 2010 Broncode Class MyApplicationInfoExample Public Shared Sub Main() Dim assemblyInfo As ApplicationServices. AssemblyInfo = _ My.Application.Info Console.WriteLine( "AssemblyName : " & assemblyInfo.AssemblyName) Console.WriteLine( "ProductName : " & assemblyInfo.ProductName) Console.WriteLine( "CompanyName : " & assemblyInfo.CompanyName) Console.WriteLine( "Copyright : " & assemblyInfo.Copyright) Console.WriteLine( "Description : " & assemblyInfo.Description) Console.WriteLine( "DirectoryPath : " & assemblyInfo.DirectoryPath) Console.WriteLine( "Title : " & assemblyInfo.Title) Console.WriteLine( "Trademark : " & assemblyInfo.Trademark) Console.WriteLine( "Version : " & assemblyInfo.Version.ToString()) Console.WriteLine( "Physical Memory mapped to the Process : " & _ assemblyInfo.WorkingSet.ToString()) Console.WriteLine( "Loaded Asseblies :") Dim loadedAssemblies As _ ObjectModel.ReadOnlyCollection( Of Reflection. Assembly) = _ assemblyInfo.LoadedAssemblies For Each assembly As Reflection. Assembly In loadedAssemblies Console.WriteLine( "- " & assembly.GetName().Name) Next Console.ReadLine() End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output AssemblyName : MyApplicationInfoExample
ProductName : MyApplicationInfoExample
CompanyName : Some Company Name
Copyright : Copyright © 2008
Description : Some Description
DirectoryPath : C:\TestMy\MyApplicationInfoExample\bin\Debug
Title : MyApplicationInfoExample
Trademark :
Version : 1.0.0.0
Physical Memory mapped to the Process : 19734528
Loaded Asseblies :
- mscorlib
- Microsoft.VisualStudio.HostingProcess.Utilities
- System.Windows.Forms
- System
- System.Drawing
- Microsoft.VisualStudio.HostingProcess.Utilities.Sync
- Microsoft.VisualStudio.Debugger.Runtime
- vshost
- System.Data
- System.Deployment
- System.Xml
- System.Core
- System.Xml.Linq
- System.Data.DataSetExtensions
- Microsoft.VisualBasic
- MyApplicationInfoExample vshost en de assembies uit de Microsoft.VisualStudio namespace krijgen we slechts te zien als we bovenstaand voorbeeld vanuit Visual Studio uitvoeren. boven
26.5.3. My.Computer ObjectIs bruikbaar om informatie over de computer op te vragen ( My.Computer.Info ), maar biedt ook op heel eenvoudige manier toegang tot enkele operaties op het filesysteem ( My.Computer.FileSystem ), zelfs veel gebruikte IO operaties kan je vanuit dit object bereiken. boven
26.5.4. My.Forms ObjectEen voorbeeld van een handige feature die het My.Forms object ons levert is de mogelijkheid om naar een formulier te verwijzen : Visual Basic 2010 Broncode Public Class Form1 Private Sub Button1_Click() Handles Button1.Click My.Forms.Form2.Show() End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
| hoofdstuk |
25. 26. 27.  |
| onderwerp |
26.4. 26.5. My Namespace 26.6.  |
| broncode |
Download My-Namespace.vb of My-Namespace.cs |
| datum |
laatst gewijzigd op woensdag 26 november 2008, laatst gepubliceerd op zondag 31 juli 2011 |
|