{"id":221,"date":"2010-01-11T14:36:00","date_gmt":"1970-01-01T00:00:00","guid":{"rendered":"http:\/\/www.yazilimmutfagi.com\/?p=221"},"modified":"2010-01-11T14:36:00","modified_gmt":"1970-01-01T00:00:00","slug":"vb-net-metotlar","status":"publish","type":"post","link":"https:\/\/www.yazilimmutfagi.com\/index.php\/2010\/01\/11\/vb-net-metotlar\/","title":{"rendered":"VB.NET Metotlar"},"content":{"rendered":"<p>&nbsp;<\/p>\n<div style=\"margin-bottom: 0cm;\"><font size=\"2\" style=\"font-size: 10pt;\">Nesneye dayali programlama dillerinin yapi taslarindan biri de metotlardir.&nbsp;<\/font><\/div>\n<div>Metotlar programimizin fiili, yani yaptigi istir; olmazsa olmazidir.&nbsp;<\/div>\n<div>Insani bir program olarak d&uuml;s&uuml;n&uuml;rsek y&uuml;r&uuml;mek, konusmak, bakmak&#8230; insnanin metotlari olur.<\/div>\n<div>Baska bir degisle metot, belli bir isi yapmak i&ccedil;in olusturulan program parcasidir. Alt programdir.<\/div>\n<div>&Ouml;rnegin yazdigimiz uygulamanin bir &ccedil;ok yerinde fakt&ouml;riyel hesabi yapmamiz gerekiyorsa, bunu bir metot olarak bir kere &nbsp;yazariz ve lazim oldugu her yerde, &ccedil;agirmak suretiyle tekrar tekrar kullaniriz. Bu bize hem zaman hem de programimiza performans kazandiracaktir.<\/div>\n<div>Zaten nesnesel programlamanin amaci da bu kolayligi ve kullanisligi saglamaktir.<\/div>\n<div>&nbsp;<\/div>\n<div>Sub &ndash; End Sub<\/div>\n<div>Belirli bir tekrar gerektiren komutlari her defasinda yeniden yazmak yerine bir prosed&uuml;r i&ccedil;inde bir &nbsp;kere yazar ve ihtiyacimiz oldugunda &ccedil;agiririz.<\/div>\n<div>B&uuml;t&uuml;n &ccedil;alisabilen kodlar mutlaka Sub prosed&uuml;r&uuml;n i&ccedil;inde olmalidir. Bir Sub proced&uuml;r&uuml; Module, Class, Interface veya Structure i&ccedil;inde tanimlayabiliriz fakat bir Sub proced&uuml;r i&ccedil;inde baska bir Sub tanimlanamaz.<\/div>\n<div>Sart degildir fakat metotlara ad olarak bir fiil verilirse daha munsaip olur.<\/div>\n<div>&nbsp;<\/div>\n<div>Kullanimi :<\/div>\n<div>&nbsp;<\/div>\n<div>Sub isim(Parametreler)<\/div>\n<div>Komut veya komutlar<\/div>\n<div>End Sub<\/div>\n<div>&nbsp;<a href='www.yazilimmutfagi.com'>.<\/a><\/div>\n<div>&nbsp;<\/div>\n<div>&ouml;rnek 1:<\/div>\n<div><span style=\"background-color: rgb(255, 255, 255); \">&nbsp;&nbsp; &nbsp;Sub KaresiniAl()<\/span><\/div>\n<div><span style=\"background-color: rgb(255, 255, 255); \">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim b As Integer<\/span><\/div>\n<div><span style=\"background-color: rgb(255, 255, 255); \">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim a As Integer = InputBox(&quot;Karesi alinacak sayiyi gir&quot;)<\/span><\/div>\n<div><span style=\"background-color: rgb(255, 255, 255); \">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;b = a * a<\/span><\/div>\n<div><span style=\"background-color: rgb(255, 255, 255); \">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MsgBox(a &amp; &quot; Karesi: &quot; &amp; b)<\/span><\/div>\n<div><span style=\"background-color: rgb(255, 255, 255); \">&nbsp;&nbsp; &nbsp;End Sub<\/span><\/div>\n<div>&nbsp;<\/div>\n<div>InputBox ile girilen bir sayinin karesini aliyor. &Ccedil;agirmak i&ccedil;in KaresiniAl() yazmamiz yeterli.<\/div>\n<div>Bu verdigim &ouml;rnekteki metot disaridan parametre almiyor. Bazi metotlara ise bizim verdigimiz degerleri islemek i&ccedil;indir. Ayni metodu bir de parametre alacak sekilde, yani disaridan deger alacak sekilde yapalim:<\/div>\n<div>&Ouml;rnek 2:<\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;&nbsp; &nbsp;Sub KaresiniAl(ByVal sayi As Integer)<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim kare As Integer<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;kare = sayi * sayi<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MessageBox.Show(sayi &amp; &quot; sayisinin karesi: &quot; &amp; kare)<\/div>\n<div>&nbsp;&nbsp; &nbsp;End Sub<\/div>\n<div>&nbsp;<\/div>\n<div>&ldquo; Function-End Function &rdquo;<\/div>\n<div>Bu t&uuml;r metotlar Sub metotlarin tasidigi b&uuml;t&uuml;n &ouml;zellikleri tasirlar; farkli olarak Procedure i&ccedil;inde meydana gelecek deger, Procedure &ccedil;agrildiktan sonra herhangi bir degisken veya kontrole atanir. Bu y&uuml;zden d&ouml;nd&uuml;r&uuml;lecek deger Return ifadesi kullanilarak d&ouml;nd&uuml;r&uuml;l&uuml;r.&nbsp;<\/div>\n<div>Ayrica d&ouml;necek olan degerin veri tipini de belirtmemiz gerekir.<\/div>\n<div>&nbsp;<\/div>\n<div>Kullanimi :<\/div>\n<div>Function isim ( Arg&uuml;manlar) as D&ouml;necek olan degerin veri tipi<\/div>\n<div>Komutlar<\/div>\n<div>Return d&ouml;necek olan deger<\/div>\n<div>End Funciton<\/div>\n<div>&nbsp;<\/div>\n<div>Bir fonksiyon End Function satiri ile biter. Ancak bazi sartlar ger&ccedil;eklestiginde<\/div>\n<div>fonksiyonun &ccedil;alismasini bitirmeden &ccedil;ikmak i&ccedil;in Exit Function kullanilabilir.<\/div>\n<div>&nbsp;<\/div>\n<div>&Ouml;rnek 3:<\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;&nbsp; &nbsp;Function Faktoriyel(ByVal f As Integer) As Integer<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim a, b As Integer<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;a = 1<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;For b = 1 To f<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a = a * b<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Next<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Return a<\/div>\n<div>&nbsp;&nbsp; &nbsp;End Function<\/div>\n<div>&nbsp;<\/div>\n<div>Return nedir? Fonsiyonumuzdan d&ouml;necek degerin ne olmasini istiyorsaniz Return ile onu yazin. Yani Return ile d&ouml;necek olan degeri belirleriz. Bu durumda &#8216;a&#8217; degiskeniydi, fakat isteseydik 5 gibi bir sayi da d&ouml;nd&uuml;rebilirdik. Fakat d&ouml;necek degerin, fonksiyonumuzu ilk basta belirttigimiz tipten olmali (bu &ouml;rnekte Integer).<\/div>\n<div>&nbsp;<\/div>\n<div>Not: Metotlarimizi olustururken Private veya Public gibi anahtar kelimeleri kullanmadik. Bu durumda varsayilan olarak Public olacaktir. Bu da metotlarimizin forum i&ccedil;inden de forum disindan da kullanilabilecegini g&ouml;steriri. Private olarak tanimlasaydik yalnizca o forum\/sinif icinde kullanabilirdik.<\/div>\n<div>&nbsp;<\/div>\n<div>ByVal ve ByRef<\/div>\n<div>ByRef olarak tanimlanmis parametrelerde parametrenin degeri degil, o parametrenin<\/div>\n<div>bellekteki adresi g&ouml;nderilir. Dolayisiyla bu parametreye atanan deger ayni bellek b&ouml;lgesinde<\/div>\n<div>degisiklik yapacagi i&ccedil;in fonksiyonu &ccedil;agiran yer de bu degisimden etkilenir.<\/div>\n<div>&nbsp;<\/div>\n<div>&Ouml;rnek 4:<\/div>\n<div>&nbsp;Sub Abc(ByRef x As Integer)<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;x = 13<\/div>\n<div>&nbsp;&nbsp; &nbsp;End Sub<\/div>\n<div>&nbsp;<\/div>\n<div>Abc() metodunu cagirip parametre olarak &#8216;x&#8217; i&ccedil;in, &ouml;rnegin 10 degerini verelim:<\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim Sayi As Integer<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Sayi = 10<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Abc(Sayi)<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MsgBox(Sayi)<\/div>\n<div>&nbsp;<\/div>\n<div>Sonu&ccedil;: 13&#8217;t&uuml;r. &Ccedil;&uuml;nk&uuml; referans olarak bize dogrudan &#8216;x&#8217; degiskenin adresi verildi.<\/div>\n<div>Bunu bir de ByVal ile yapalim:<\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;Sub Abc(ByVal x As Integer)<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;x = 13<\/div>\n<div>&nbsp;&nbsp; &nbsp;End Sub<\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp;Dim Sayi As Integer<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Sayi = 10<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Abc(Sayi)<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MsgBox(Sayi)<\/div>\n<div>&nbsp;<\/div>\n<div>Sonu&ccedil;: 10&#8217;dur.&nbsp;<\/div>\n<div>&Ccedil;&uuml;nk&uuml; ByVal olarak tanimlanmis parametrelerde fonksiyona parametrenin (&#8216;x&#8217;) adresi degil bir kopyasi g&ouml;nderilir. Yani farkli bir bellek b&ouml;lgesi. Bu iki kopya ayri bellek b&ouml;lgelerini kullanacaklarindan parametrenin fonksiyon i&ccedil;indeki degisiminden &ccedil;agiran yer etkilenmez.<\/div>\n<div>&nbsp;<a href='www.yazilimmutfagi.com'>.<\/a><\/div>\n<div><img decoding=\"async\" loading=\"lazy\" width=\"353\" height=\"239\" alt=\"\" src=\"http:\/\/www.yazilimmutfagi.com\/yuklenendosyalar\/image\/ByRef_ByVal.png\" \/><\/div>\n<div>Son bir &ouml;rnek daha yapalim:<\/div>\n<div>&nbsp;&nbsp; &nbsp;Sub ValRef(ByVal a As Integer, ByRef b As Integer, ByRef c As Integer)<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;a = 10<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;b = 10<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;c = 10<\/div>\n<div>&nbsp;&nbsp; &nbsp;End Sub<\/div>\n<div>Fonksiyonumuzu yazdik&#8230;<\/div>\n<div>&nbsp;<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim x As Integer = 20<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim y As Integer = 20<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim z As Integer = 20<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ValRef(x, y, z)<\/div>\n<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MsgBox(&quot;X:&quot; &amp; x &amp; &quot; Y:&quot; &amp; y &amp; &quot; Z:&quot; &amp; z)<\/div>\n<div>&nbsp;<\/div>\n<div>Sonu&ccedil;: X:20 Y:10 Z:10<\/div>\n<div>&nbsp;<\/div>\n<div>Umarim faydali olmusumdur. Bir baska makalede g&ouml;r&uuml;smek &uuml;zere&#8230;<\/div>","protected":false},"excerpt":{"rendered":"<p>Nesneye dayali programlama dillerinin yapi taslarindan biri de metotlardir.<br \/>\nMetotlar programimizin fiili, yani yaptigi istir; olmazsa olmazidir.<br \/>\nInsani bir program olarak d&#252;s&#252;n&#252;rsek y&#252;r&#252;mek, konusmak, bakmak&#8230; insnanin metotlari olur.<\/p>\n","protected":false},"author":519,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[418],"tags":[5325,5560,6213,5981,4926,6371,4591,6135,4190,6288,6325],"_links":{"self":[{"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/posts\/221"}],"collection":[{"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/users\/519"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":0,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yazilimmutfagi.com\/index.php\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}