22 Kasım 2017 Çarşamba

What is Encapsulation & 'Private' Access Modifier

We have created a property named “Age” without a field and encapsulated it with "private" access modifier to restrict directly access on it. So, the "Age" property is unreachable from the outside of the given class (The class that we gonna use in this example is “Person”). Thats what "private" access modifiers do. This process is called "Encapsulation". It's a meaningful identifier since we are "encapsulating" the given property and hiding it from the outer-space. With the encapsulation in this example, you are not able to change the "Age" property by directly addressing it. For example: Person p1 = new Person(); p1.Age = 33; /*That will not work as expected. If we could use the "public" access modifier, that expression could work properly. Lets see another example: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { class Person{ private int Age = 30; public void IncAge(int n){ Age += n; } public void DecAge(int n){ Age -= n; } public int GetAge(){ return Age; } } static void Main(){ Person p1 = new Person(); p1.DecAge(4); Console.WriteLine(p1.GetAge()); p1.IncAge(10); Console.WriteLine(p1.GetAge()); p1.Age = 23; /* As you can see, this line will cause an error too since "Age" Property's access modifier has set to "Private". It can’t be reached from this Main Method or any other methods in your project for reasons that i have wrote above. */ } } }

Overloading a Method (misuse)


// When overloading methods, methods' parameters' // definitions must be differ from each other. // You cannot overload these methods for example: int NewMethod(int number); int NewMethod(int number2); int NewMethod(int number3); // These declarations will result an error.

21 Kasım 2017 Salı

Overloading Sample (string a, int b)


We'll declare a method that has string and double parameters in this example. When you overloading methods, the methods will be called based on the argument's type. A double argument will call the method which gets a "double" inside it. An integer argument will match with a method which gets a "int" inside it. Thats the point of the Overloading. Lets see it in example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main() { Console.WriteLine("You are in Main Method."); Console.WriteLine("---"); Enter(5); Console.WriteLine("---"); Enter(5.2); Console.WriteLine("---"); New("A text here. The number is:", 666); } static void Enter(int n) { Console.WriteLine("You are in Enter Method with INT parameter"); Console.WriteLine(n); } static void Enter(double n) { Console.WriteLine("You are in Enter Method with DOUBLE parameter"); Console.WriteLine(n); } static void New(string st, double s) { Console.WriteLine("You are in 'New' Method with 2 parameter: string, double"); Console.WriteLine(st + s); } } }

16 Kasım 2017 Perşembe

23 Nisan 2017 Pazar

ASP.NET MVC'de Search İşlemi

Öncelikle View’da form oluşturmamız gerek. Çünkü arama şartlarını bu form ile Controller’a ileteceğiz. @using (Html.BeginForm("Index", "Home", FormMethod.Get)) {} Burada yazdığımız “Index” = Action “Home” = Controller’ın adı Formmethod.Get ise; bu form ile yaptığımız işlemin bir “Get” operasyonu olduğunu gösteriyor. Arama şartlarını (Kişiye göre ara / Türe göre ara) Radio Buttonlar aracılığıyla seçeceğiz. @Html.RadioButton("SearchBy", "SearchByBook", true); <text> İsme Göre</text> @Html.RadioButton("SearchBy", "SearchByRating") <text> Puana Göre</text> @Html.RadioButton("SearchBy", "SearchByAuthor") <text> Yazara Göre</text> @Html.RadioButton("SearchBy", "SearchByGender") <text> Türe Göre</text> RadioButton’un aldığı ilk parametre görüldüğü gibi “SearchBy” olarak girildi. Yazdığımız “SearchBy”, RadioButton’un ismidir. Bu ismi, Controller kısmında kullanacağız. RadioButton’un ikinci parametresi de “SearchByBook”. Bu parametre ile, kullanıcının neye göre arama yaptığını (isim,tür,puan vb) anlamış olacağız. Bu arada üçüncü parametre olan “true” ise, bu Radiobutton’un otomatik olarak seçili gelmesini sağlayacak. Ve bir TextBox ve “Ara” isminde bir button oluşturalım ve kullanıcının yazdığı veriyi Server’a gönderelim. @Html.TextBox("SearchTerm") <input type="submit" value="Ara!" class="buttonSearch" /> Görüleceği üzere; TextBox’un ismi “SearchTerm” olarak girilmiştir. Bu ismi kullanarak, Controller kısmında, kullanıcının TextBox’a yazdığı ve gönderdiği veriyi elde etmiş olacağız. Şimdi Controller kısmına geçelim. public ActionResult Index(string searchBy, string searchTerm) { if (searchBy == "SearchByBook") { Return View(_sampleContext.Books.Where(b => b.BookName.StartsWith(SearchTerm)).ToList) Şimdi ise View ve Controller’da tam olarak ne olup bittiğini anlayalım: @Html.RadioButton("SearchBy", "SearchByBook", true); Bu radiobutton un adı SearchBy, değeri ise SearchByBook’tur. Controller kısmında: public ActionResult Index(string searchBy, string searchTerm) String SearchBy yazmıştık. Bu SearchBy parametresiyle; ismi SearchBy olan buttonlarımızın “Value’sini alıyoruz” Örneğin bi üstteki radiobutton’un değeri SearchByBook idi.. Yani kullanıcı eğer bu radiobutton’u seçer ise şayet; biz controller’daki searchBy parametremize SearcyByBook değerini atamış oluruz. SearchBy parametresinde gerçekleşen işlem budur. String SearchTerm ile ise; kullanıcının TextBox’a yazdığı veriyi çekmiş olduk.. @Html.TextBox("SearchTerm") Burada gerçekleşen işlem de aynı mantık ile ilerliyor. TextBox’un adını bildiğimiz üzere “SearchTerm” olarak girmiştik. Dolayısıyla kullanıcının TextBox’a yazmış olacağı herhangi bir kelime veya harfi; Controllerda bulunan String SearchTerm parametresine atamış olacağız. public ActionResult Index(string searchBy, string searchTerm) Yani ne yapmış olduk? Kişinin neye göre arama yapacağını (türe göre mi, isme göre mi, yoksa puana göre mi) SearchBy parametresine atmış olduk è View kısmında radiobuttonların ismi SearcBy idi ve bu ismi kullanarak seçilen RadioButton’un değerine ulaşmış olduk. SearchTerm parametresine ise, yine aynı şekilde kullanıcının, SearchTerm isimli TextBox’a yazmış olduğu veriyi, yine Controller’da bulunan String SearchTerm parametresine atamış olduk. Ve bu parametrelere atanmış olan verileri elde ederek; kullanıcının istediği şekilde olan arama sonuçlarını listeleterek kullanıcıya sunmuş olduk. Return View(_sampleContext.Books.Where(b => b.BookName.StartsWith(SearchTerm)).ToList) Kodların derlenmiş hali: VIEW: @using (Html.BeginForm("Index", "Home", FormMethod.Get)) { <b>Ara: &nbsp&nbsp</b> @Html.RadioButton("SearchBy", "SearchByBook", true); <text> İsme Göre</text> @Html.RadioButton("SearchBy", "SearchByRating") <text> Puana Göre</text> @Html.RadioButton("SearchBy", "SearchByAuthor") <text> Yazara Göre</text> @Html.RadioButton("SearchBy", "SearchByGender") <text> Türe Göre</text> <br /> @Html.TextBox("SearchTerm") <input type="submit" value="Ara!" class="buttonSearch" /> } CONTROLLER: public ActionResult Index(string searchBy, string searchTerm) { if (searchBy == "SearchByBook") { return View( "abc", _myLibraryContext.Books. Where(x=>x.BookName.StartsWith(searchTerm)).ToList()); } if (searchBy == "SearchByRating") { return View("abc", _myLibraryContext.Books. Where(n => n.Rating.ToString() == searchTerm)); } else { return View("abc", _myLibraryContext.Books.ToList()); } }