MSMVPS.COM
The Ultimate Destination for Blogs by Current and Former Microsoft Most Valuable Professionals.

Tür dönüşüm kontrolünü daha rahat yapabiliriz ?

Bir çok uygulamada özellikle de uygulamalarin sunum (presentation) katmaninda asagidakine benzer kod parçalari görüyorum.

for(int i = 0; i < this.Controls.Count; i++)
{
    
try
    
{
         
TextBox btn = this.Controls[i];
    
}
    
catch
    
{
    
}
}

Yani bir tür dönüsümünün geçerli olup olmadigi try-catch bloklari ile çözülmeye çalisiyor. Evet, bu yöntem istenilen sonuca ulastirir ama try-catch blogu çokta amaci dogrultusunda kullanilmamistir. Hepiniz biliyorsunuki try-catch bloklari sadece istisnai durumlari düzenlemek için kullanilmalidir. Ama bir button nesnesini textbox nesnesine atamak çok ta istisnai bir durum degildir. Bu tür durumlar için C#'taki as operatörü biçilmis kaftandir. Bakiniz, yukaridaki kod asagidaki sekilde yazilirsa daha güzel ve daha okunabilir olacaktir.

for(int i = 0; i < this.Controls.Count; i++)
{
     TextBox btn = this.Controls[i]  as TextBox;

     
if(btn != null)
    
{
           //isleme devam          
     }
 
}

Yalniz unutmamak gerekir ki, as operatörü sadece referans türleri ile birlikte kullanilabilir.


Posted Sep 22 2004, 11:32 AM by sefer

Add a Comment

(required)  
(optional)
(required)  
Remember Me?


Copyright © is the original authors. Blog site is an independent site not sponsored by Microsoft. The Yoda blog server and the Brianna SQL server would like to thank www.ownwebnow.com and www.exchangedefender.com. They wouldn't be here and broadcasting without the generosity of Vlad Mazek and his companies.

Powered by Community Server (Commercial Edition), by Telligent Systems