<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>logical Archive - Robert Skibbe</title>
	<atom:link href="https://robbelroot.de/blog/tag/logical/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>alias RobbelRoot – Freelance Full Stack Developer .NET</description>
	<lastBuildDate>Mon, 12 Jul 2021 00:24:33 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://robbelroot.de/wp-content/uploads/2020/12/cropped-favicon-32x32.png</url>
	<title>logical Archive - Robert Skibbe</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>VB.NET AndAlso</title>
		<link>https://robbelroot.de/blog/vbnet-andalso/</link>
					<comments>https://robbelroot.de/blog/vbnet-andalso/#respond</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Mon, 12 Jul 2021 00:17:55 +0000</pubDate>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Visual Basic .NET]]></category>
		<category><![CDATA[Visual Basic .NET Problemlösungen]]></category>
		<category><![CDATA[abfrage]]></category>
		<category><![CDATA[abfragen]]></category>
		<category><![CDATA[also]]></category>
		<category><![CDATA[and]]></category>
		<category><![CDATA[andalso]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[combine]]></category>
		<category><![CDATA[false]]></category>
		<category><![CDATA[geschwindigkeit]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[leak]]></category>
		<category><![CDATA[logical]]></category>
		<category><![CDATA[logisch]]></category>
		<category><![CDATA[logisches]]></category>
		<category><![CDATA[optimierung]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[resources]]></category>
		<category><![CDATA[ressourcen]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[true]]></category>
		<category><![CDATA[und]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vbnet]]></category>
		<category><![CDATA[verknüpfen]]></category>
		<category><![CDATA[verunden]]></category>
		<category><![CDATA[zeit]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=3587</guid>

					<description><![CDATA[<p>VB.NET AndAlso Du möchtest mehr über den VB.NET AndAlso Operator lernen und verstehen wie Er funktioniert? In diesem Beitrag zeige ich Dir einige Bespiele des VB.NET AndAlso Operators und erkläre Dir die Vorteile wie z. B. die Performance bezogenen Aspekte. Vielleicht interessieren Dich z. B. auch noch folgende Beiträge: Application &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-andalso/">VB.NET AndAlso</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso.png"><img fetchpriority="high" decoding="async" width="1024" height="536" src="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-1024x536.png" alt="VB.NET AndAlso" class="wp-image-3590" title="VB.NET AndAlso" srcset="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-1024x536.png 1024w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-300x157.png 300w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-768x402.png 768w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-700x366.png 700w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-332x174.png 332w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso.png 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>VB.NET AndAlso</figcaption></figure>






<h2 class="wp-block-heading">VB.NET AndAlso</h2>



<p>Du möchtest <strong>mehr über </strong>den <strong>VB.NET AndAlso</strong> Operator <strong>lernen und verstehen </strong>wie Er funktioniert?</p>



<p>In <strong>diesem Beitrag </strong>zeige ich Dir <strong>einige Bespiele </strong>des <strong>VB.NET AndAlso</strong> Operators und <strong>erkläre Dir </strong>die <strong>Vorteile </strong>wie z. B. die <strong>Performance </strong>bezogenen Aspekte.</p>



<p><strong>Vielleicht </strong>interessieren Dich z. B. auch noch <strong>folgende Beiträge</strong>: <strong><a href="/blog/vbnet-application-path" target="_blank" rel="noreferrer noopener">Application Path</a></strong>, <strong><a href="/blog/vbnet-pdf-oeffnen" target="_blank" rel="noreferrer noopener">PDF Öffnen</a></strong>, oder <strong><a href="/blog/vbnet-button-click-ausloesen" target="_blank" rel="noreferrer noopener">Button Click auslösen</a></strong>.</p>



<h2 class="wp-block-heading">Vorarbeit – Wetter-Sensor</h2>



<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/Wetter-Sensor-Sonne-Regen.png"><img decoding="async" width="1024" height="536" src="https://robbelroot.de/wp-content/uploads/2021/07/Wetter-Sensor-Sonne-Regen-1024x536.png" alt="Wetter-Sensor Sonne Regen" class="wp-image-3600" title="Wetter-Sensor Sonne Regen" srcset="https://robbelroot.de/wp-content/uploads/2021/07/Wetter-Sensor-Sonne-Regen-1024x536.png 1024w, https://robbelroot.de/wp-content/uploads/2021/07/Wetter-Sensor-Sonne-Regen-300x157.png 300w, https://robbelroot.de/wp-content/uploads/2021/07/Wetter-Sensor-Sonne-Regen-768x402.png 768w, https://robbelroot.de/wp-content/uploads/2021/07/Wetter-Sensor-Sonne-Regen-700x366.png 700w, https://robbelroot.de/wp-content/uploads/2021/07/Wetter-Sensor-Sonne-Regen-332x174.png 332w, https://robbelroot.de/wp-content/uploads/2021/07/Wetter-Sensor-Sonne-Regen.png 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>Wetter-Sensor Sonne Regen</figcaption></figure>



<p><strong>Um </strong>das VB.NET AndAlso <strong>Beispiel klarer </strong>zu gestalten, leisten wir ein <strong>wenig Vorarbeit</strong>, indem wir eine kleine <strong>WetterSensor-Klasse</strong> bauen.</p>



<p><strong>Keine Sorge</strong>, dabei wir es sich um relativ <strong>stupide Daten ohne </strong>jegliche Anbindung einer <strong>API </strong>handeln.</p>



<p>Die Sensor-Klasse dient<strong> </strong>letztendlich nur der <strong>einfacheren Visualisierung und </strong>dem <strong>Verständnis </strong>hinter den Kulissen.</p>



<h3 class="wp-block-heading" id="wetter-sensor-klasse-achtung">Achtung – Textbox-Referenz</h3>



<p>Ich <strong>verwende </strong>hier <strong>beabsichtigt </strong>eine <strong>Referenz </strong>zur <strong>Log-Textbox</strong>, <strong>normalerweise </strong>sollte man dies <strong>nicht </strong>tun.</p>



<p><strong>Mein Ziel </strong>ist es aber, das Beispiel so <strong>einfach wie möglich </strong>zu halten <strong>und </strong>Dir irgendwelche <strong>Handler </strong>zu <strong>ersparen</strong>.</p>



<p>Die Sensor-<strong>Klasse soll </strong>also beabsichtigt <strong>direkt in </strong>die <strong>Textbox loggen </strong>und somit eine <strong>direkte Abhängigkeit </strong>zu Ihr besitzen.</p>



<h3 class="wp-block-heading">WeatherSensor-Klasse</h3>



<p>An diesem Punkt <strong>stelle </strong>ich <strong>kurz </strong>die <strong>WeatherSensor</strong>&#8211;<strong>Klasse </strong>vor:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Public Class WeatherSensor

    Public Property TbLog As TextBox

    Sub New(tbLog As TextBox)
        Me.TbLog = tbLog
    End Sub

    Public Async Function IsSunny() As Task(Of Boolean)
        Log($"{NameOf(IsSunny)}()")
        Await ConnectToSensor()
        Await DisconnectFromSensor()
        Return False
    End Function

    Public Async Function IsRaining() As Task(Of Boolean)
        Log($"{NameOf(IsRaining)}()")
        Await ConnectToSensor()
        Await DisconnectFromSensor()
        Return True
    End Function

    Private Async Function ConnectToSensor() As Task
        Log($"{NameOf(ConnectToSensor)}()")
        Await Task.Delay(1000)
        Log("Connected to sensor")
        Log("------")
    End Function

    Private Async Function DisconnectFromSensor() As Task
        Log($"{NameOf(DisconnectFromSensor)}()")
        Await Task.Delay(1000)
        Log("Disconnected from sensor")
        Log("------")
    End Function

    Private Sub Log(txt As String)
        TbLog.AppendText($"[{Now:HH:mm:ss}] {txt}{Environment.NewLine}")
    End Sub

End Class</pre>



<h4 class="wp-block-heading">Eigenschaften</h4>



<h5 class="wp-block-heading">TbLog</h5>



<p><strong>Hier speichern </strong>wir die <strong>Referenz</strong>, <strong>um </strong>direkt aus der Klasse <strong>loggen </strong>zu können (bitte <strong><a href="#wetter-sensor-klasse-achtung">Hinweise</a></strong> beachten).</p>



<h4 class="wp-block-heading">Konstruktoren</h4>



<h5 class="wp-block-heading">new(TextBox)</h5>



<p>Der <strong>erste </strong>und <strong>einzige Konstruktor </strong>der Klasse <strong>benötigt </strong>die <strong>Referenz </strong>zur <strong>Log</strong>-Textbox.</p>



<h4 class="wp-block-heading">Methoden</h4>



<h5 class="wp-block-heading">IsSunny</h5>



<p>Eine <strong>Funktion </strong>um festzustellen, <strong>ob </strong>es aktuell <strong>sonnig </strong>ist.</p>



<p>Im <strong>Hintergrund </strong>müssen wir uns natürlich – simuliert – zum Sensor <strong>verbinden</strong>, etc.</p>



<h5 class="wp-block-heading">IsRaining</h5>



<p>Mit dieser Funktion stellen wir fest, <strong>ob es </strong>aktuell <strong>regnet </strong>(die <strong>Intensität spielt </strong>hier <strong>keine Rolle</strong>).</p>



<p><strong>Auch hier </strong>muss im <strong>Hintergrund </strong>natürlich eine &#8222;<strong>Verbindung </strong>hergestellt&#8220; werden.</p>



<h5 class="wp-block-heading">ConnectToSensor</h5>



<p><strong>Simuliert </strong>eine <strong>Verbindung </strong>zum Sensor aufzubauen.</p>



<h5 class="wp-block-heading">DisconnectFromSensor</h5>



<p><strong>Tut so</strong>, als ob die Instanz sich <strong>vom Sensor trennen </strong>würde.</p>



<h5 class="wp-block-heading">Log(String)</h5>



<p>Eine <strong>Hilfsfunktion um </strong>Daten direkt in die Textbox <strong>zu loggen </strong>(bitte auch hier <strong><a href="#wetter-sensor-klasse-achtung">Hinweise</a></strong> beachten!).</p>



<h2 class="wp-block-heading">Grafische Oberfläche</h2>



<p>Für unser &#8222;<strong>VB.NET AndAlso</strong>&#8222;-Beispiel kannst Du Dir am besten die <strong>folgende</strong>, <strong>oder </strong>eine <strong>ähnliche Oberfläche </strong>zusammenklicken:</p>



<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-GUI.png"><img decoding="async" width="521" height="545" src="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-GUI.png" alt="VB.NET AndAlso GUI" class="wp-image-3613" title="VB.NET AndAlso GUI" srcset="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-GUI.png 521w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-GUI-287x300.png 287w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-GUI-332x347.png 332w" sizes="(max-width: 521px) 100vw, 521px" /></a><figcaption>VB.NET AndAlso GUI</figcaption></figure>



<h3 class="wp-block-heading">VB.NET And – der linke Button</h3>



<p><strong>Zuerst </strong>einmal schauen wir uns an, wie man <strong>das normale &#8222;<a href="https://docs.microsoft.com/de-de/dotnet/visual-basic/language-reference/operators/and-operator" target="_blank" rel="noreferrer noopener">And</a></strong>&#8222;, also den normalen And-Operator verwendet.</p>



<p><strong>Wie </strong>schon <strong>angepriesen verwenden </strong>wir dafür die erstellte WetterSensor-<strong>Klasse</strong>, um eine <strong>sinnvoll</strong> gestützte Basis zu haben.</p>



<p><strong>Doppelklicke </strong>den <strong>linken Button und </strong>füge den folgenden <strong>Code </strong>ein (<strong>vergiss nicht </strong>die Sub als <strong>Async </strong>zu deklarieren):</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    Private Async Sub btnCheckWeatherByAnd_Click(sender As Object, e As EventArgs) Handles btnCheckWeatherByAnd.Click
        tbLog.Clear()
        Dim isNiceWeather = Await WeatherSensor.IsSunny() And Await WeatherSensor.IsRaining()
        ' do something
    End Sub</pre>



<p>Im <strong>ersten Schritt löschen </strong>wir den aktuellen <strong>Inhalt </strong>des <strong>Logs</strong>, <strong>damit </strong>wir <strong>bei jedem Durchgang </strong>einen <strong>sauberen Start </strong>haben.</p>



<p><strong>Dann legen </strong>wir eine <strong>Variable </strong>fest, <strong>worin </strong>wir das <strong>boolesche Ergebnis </strong>des einfachen &#8222;And&#8220;-Operators speichern.</p>



<p><strong>Letztendlich </strong>ist für uns hier nur das <strong>Ergebnis im Log relevant</strong>, um zu überprüfen, <strong>was</strong>, <strong>wie </strong>und <strong>wann passiert </strong>ist.</p>



<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-And-Wetter-Sensor-Log-Ausgabe.png"><img loading="lazy" decoding="async" width="246" height="220" src="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-And-Wetter-Sensor-Log-Ausgabe.png" alt="VB.NET And Wetter-Sensor Log Ausgabe" class="wp-image-3622" title="VB.NET And Wetter-Sensor Log Ausgabe"/></a><figcaption>VB.NET And Wetter-Sensor Log Ausgabe</figcaption></figure>



<p>Wir <strong>können </strong>im Log <strong>beobachten</strong>, <strong>dass beide Funktionen </strong>(IsSunny und IsRaining) <strong>unabhängig </strong>vom ersten Ergebnis <strong>aufgerufen </strong>werden.</p>



<p><strong>Bei </strong>einer &#8222;<strong>Verundung</strong>&#8220; müssen <strong>alle Bestandteile True </strong>ergeben, <strong>damit </strong>letztendlich das <strong>Ergebnis </strong>den Wert <strong>True </strong>erhält.</p>



<p><strong>Trotz </strong>des &#8222;<strong>False</strong>&#8222;-Wertes, also <strong>trotz </strong>des <strong>Ausschlusses</strong>, <strong>dass </strong>der <strong>erste Wert </strong>überhaupt True sein kann, <strong>wird </strong>die <strong>zweite Funktion </strong>&#8222;IsRaining&#8220; <strong>aufgerufen</strong>!</p>



<p><strong>Das dies </strong>natürlich <strong>absolut sinnlos </strong>ist, liegt auf der Hand, doch <strong>wie verhindern </strong>wir das?</p>



<p><strong>Nicht nur </strong>die <strong>sinnlosen Verbindungsversuche </strong>der Klasse zum Sensor <strong>stoßen </strong>hier <strong>übel auf</strong>.</p>



<p><strong>Ebenso </strong>stören auch die dabei <strong>verschwendeten Ressourcen</strong> und die <strong>sinnlose</strong> <strong>Warterei</strong>.</p>



<h3 class="wp-block-heading">VB.NET AndAlso – der rechte Button</h3>



<p>Schauen wir uns <strong>als nächstes </strong>die <strong>Vorgehensweise </strong>im <strong>rechten Button </strong>an:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    Private Async Sub btnCheckWeatherByAndAlso_Click(sender As Object, e As EventArgs) Handles btnCheckWeatherByAndAlso.Click
        tbLog.Clear()
        Dim isNiceWeather = Await WeatherSensor.IsSunny() AndAlso Await WeatherSensor.IsRaining()
    End Sub</pre>



<p>Dir wird <strong>vermutlich </strong>schnell <strong>auffallen</strong>, <strong>dass </strong>der Code im <strong>Handler </strong>des zweiten Buttons nur <strong>ein anderes Wort </strong>enthält.</p>



<p><strong>Statt </strong>des &#8222;<strong>And</strong>&#8222;-Operators <strong>verwenden </strong>wir hier den &#8222;<strong>AndAlso</strong>&#8222;-Operator und <strong>verändern </strong>somit die <strong>Verhaltensweise</strong>.</p>



<p>Werfen wir <strong>nun </strong>das <strong>zweite Mal </strong>einen <strong>Blick auf </strong>das <strong>Log</strong>:</p>



<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-Wetter-Sensor-Log-Ausgabe.png"><img loading="lazy" decoding="async" width="246" height="130" src="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-AndAlso-Wetter-Sensor-Log-Ausgabe.png" alt="VB.NET AndAlso Wetter-Sensor Log Ausgabe" class="wp-image-3631" title="VB.NET AndAlso Wetter-Sensor Log Ausgabe"/></a><figcaption>VB.NET AndAlso Wetter-Sensor Log Ausgabe</figcaption></figure>



<p>Nun <strong>können </strong>wir mit glücklicher Miene <strong>feststellen</strong>, <strong>dass nur </strong>die &#8222;<strong>IsSunny</strong>&#8222;-<strong>Funktion aufgerufen </strong>wird und die &#8222;IsRaining&#8220;-Funktion außen vor bleibt.</p>



<p>Somit <strong>finden keine unnötigen Verbindungsversuche </strong>und auch <strong>keine Verschwendung </strong>von Ressourcen <strong>statt</strong>, SUPER!</p>



<h2 class="wp-block-heading">Kompletter Code – VB.NET AndAlso</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Public Class Form1

    Private ReadOnly WeatherSensor As WeatherSensor

    Sub New()
        InitializeComponent()
        WeatherSensor = New WeatherSensor(tbLog)
    End Sub

    Private Sub btnClearLog_Click(sender As Object, e As EventArgs) Handles btnClearLog.Click
        tbLog.Clear()
    End Sub

    Private Async Sub btnCheckWeatherByAnd_Click(sender As Object, e As EventArgs) Handles btnCheckWeatherByAnd.Click
        tbLog.Clear()
        Dim isNiceWeather = Await WeatherSensor.IsSunny() And Await WeatherSensor.IsRaining()
    End Sub

    Private Async Sub btnCheckWeatherByAndAlso_Click(sender As Object, e As EventArgs) Handles btnCheckWeatherByAndAlso.Click
        tbLog.Clear()
        Dim isNiceWeather = Await WeatherSensor.IsSunny() AndAlso Await WeatherSensor.IsRaining()
    End Sub

End Class

Public Class WeatherSensor

    Public Property TbLog As TextBox

    Sub New(tbLog As TextBox)
        Me.TbLog = tbLog
    End Sub

    Public Async Function IsSunny() As Task(Of Boolean)
        Log($"{NameOf(IsSunny)}()")
        Await ConnectToSensor()
        Await DisconnectFromSensor()
        Return False
    End Function

    Public Async Function IsRaining() As Task(Of Boolean)
        Log($"{NameOf(IsRaining)}()")
        Await ConnectToSensor()
        Await DisconnectFromSensor()
        Return True
    End Function

    Private Async Function ConnectToSensor() As Task
        Log($"{NameOf(ConnectToSensor)}()")
        Await Task.Delay(1000)
        Log("Connected to sensor")
        Log("------")
    End Function

    Private Async Function DisconnectFromSensor() As Task
        Log($"{NameOf(DisconnectFromSensor)}()")
        Await Task.Delay(1000)
        Log("Disconnected from sensor")
        Log("------")
    End Function

    Private Sub Log(txt As String)
        TbLog.AppendText($"[{Now:HH:mm:ss}] {txt}{Environment.NewLine}")
    End Sub

End Class</pre>



<h2 class="wp-block-heading">Downloads</h2>



<div class="wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button"><a class="wp-block-button__link" href="/downloads/vbnet/AndAlsoExample.zip" target="_blank" rel="noreferrer noopener">AndAlsoExample.zip</a></div>
</div>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-andalso/">VB.NET AndAlso</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/vbnet-andalso/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
