Skip to main content

Passing Arrays to methods in a better way

අද අපි බලන්න යන්නේ C# වල තියෙන තවත් අපට ගොඩක් ප්‍රයෝජනවත් වෙන දෙයක් ගැන. ගොඩක් වෙලාවට අපි methods ලියද්දි අපිට ඕනේ වෙනවා මේ methods වලට Arrays pass කරන්න. ඒ වගේ වෙලාවට අපි කරන්නේ method එක call කරන තැන Array එකක් හදල ඒකට data දාල method ඒකට pass කරන එක. 

පහල පෙන්නලා තියෙන්නේ එක කරගන්න විදිහ.
int Add(int[] data)
{
   int sum = 0;
   for (int i = 0; i < data.Length; i++)
   {
      sum += data[i];
   }
   return sum;
}
අපි දැන් මේ method එක call කරන්න පුළුවන් මෙන්න මේ විදිහට.

void TestArray()
{
    int[] data = new int[4];
    data[0] = 3;
    data[1] = 5;
    data[2] = 2;
    data[3] = 7;
    int sum = Add(data);
}
පේනවා නෙ අපි array එක හදල ඒකට values දාල එවන විදිහ.මේක ඉතින් එච්චරම ලේසි වැඩක් නෙමෙයි කියල දැන් පැහැදිලි නෙ.

ඒත් මේක කරගන්න මීට වඩා ලේසි ක්‍රමයක් තියෙනවා. ඒ ගැන කතා කරන්න තම අපි මේ ලෑස්ති වෙන්නේ.
මේකට අපට තියෙන ලේසිම විදිහ තමා params keyword එක පාවිච්චි කරන එක. එක කරගන්නේ කොහොමද කියල පහල පෙන්නලා දීල තියෙනවා.
int Add(param int[] data)
{
   int sum = 0;
   for (int i = 0; i < data.Length; i++)
   {
      sum += data[i];
   }
   return sum;
}
දැන් මේ method එක call කරන විදිහ තමා පහල පෙන්නලා තියෙන්නේ.
void test()
{
   int sum1 = Add(1, 3, 5, 2, 7);
}
පේනවානේ, call කරන තැන අපිට array එකක් හදාගන්න ඕනේ වෙන්නේ නෑ. ඒ වගේම තමා අපිට ඕනෙම තරමක් parameters මේ විදියට pass කරන්න පුළුවන්.

හැබැයි මෙතෙන අපි මතක තියාගන්න ඕනේ කරුණු තුනක් තියෙනවා.
  1. මේ Add method ඒකට තවත් integer array එකක් ගන්න overload method එකක් තියෙනවා නම්, අපි මේ ලියපු method එක නෙමෙයි call වෙන්නේ ඒ අපි overload කරලා තියෙන method එක.
  2. param array එකක් අපිට ඕනේ නම් පුළුවන් තවත් parameters එක්ක පාවිච්චි කරන්න, හැබැයි එහෙම පාවිච්චි කරනවා නම් අපේ param array එක තමා parameters list එකේ අන්තිම parameter එක වෙන්න ඕනේ. ඒ කියන්නේ param array එකට පස්සේ තවත් parameters තියෙන්න විදිහක් නෑ.
  3. එක method එකකට parameter එකක් විදිහට තියෙන්න පුළුවන් එක param array එකක් විතරයි.
ඉතින් මතක තියාගන්න අපිට මේක පාවිච්චි කරන්න පුළුවන් ඔය කියන කොන්දේසි වලට එකග වෙනවා නම් විතරයි. නරක නෑ නේද වැඩේ.

Comments

Popular posts from this blog

Adding Unique Constraints with Entity Framework Code First

Entity Framework Code First is a great way to define and maintain your database within your application it self. While it poses a nice set of complementing libraries like Data Annotations and Fluent Configurations which helps you specially in defining phase of your database, it would give you a headache if you try to define a unique constraint on a column. For example in the users table of your application you could probably have an int ID column which would serve as the primary key and you might need to make your Username column a unique one. Since you are using EF Code First you will soon find out there is no direct way to accomplish this requirement. Unfortunately fluent configurations doesn't have syntax like HasUnique(u => u.Username); I asked the same question in stackoverflow , but didn't get a convincing answer. Since there is no direct support from EF for this you could take one of following alternatives to achieve it. Approach 1 :  

How to disable ASP.NET page validations

ASP.NET වල page validation පාවිච්චි වෙන්නේ script injection attacks නවත්ත ගන්න, ඒ ගැන වැඩි විස්තර ඕනෙ නම් මෙතනින් බලාගන්න පුළුවන්. හැබැයි මේක අපිට කරදරයක් වෙන වෙලාවලුත් තියෙනවා. උදාහරණයක් විදියට අපි හිතුවොත් අපි අපේ user ට හිතලම ඉඩ දෙන්න ඕනේ script වගේ දේවල් page එකකට submit කරන්න. මේක උනේ මම වැඩ කරපු Admin site එකක, මේ site ඒක run වෙන්නේ company එකක intranet එකේ, මේක පාවිච්චි කරන්නේ Admin ල විතරයි ඒක අපි වෙන විදියකට authenticate කරනවා ඒ ගැන පස්සේ කියන්නම්. ඉතින් ප්‍රශ්නේ උනේ මේ page validations නිසා අපිට ඕනේ කරන user inputs ගන්න විදියක් නැති උනා. ඒක විසඳගන්න මට සිද්ද උනා page validations disable කරන්න. ඒක කරන්න ක්‍රම දෙකක් තියෙනවා. මොන දේ කරන්නත් ඉස්සෙල්ල site එකේ web.config එක පහල තියෙන විදිහට වෙනස් කරන්න ඕනේ. (සැ. යු. ASP.NET 4.0 web sites සඳහා). <configuration> <system.web> <httpRuntime requestValidationMode="2.0"/> </system.web> </configuration> ඊට පස්සේ පහල තියෙන ක්‍රම දෙකෙන් එකකට වැඩේ කරන්න පුළුවන්. Site එක

HTML INPUT enhancement with Regular Expressions and Java Scripts

අද කථා කරන්න යන්නේ බොහොම සරල දෙයක් ගැන. සරල උනාට ඉතින් මට මේක කරගන්න ඕන වෙලා හොයන කොට නම් ඉතින් හොයාගන්න ටිකක් කරදර උනා, ඒ නිසාම තමා මම හිතුවේ මේක ගැන ලියන්න ඕනෙ කියල. අපි හැමෝම HTML forms validate කරන්න Java Script පාවිච්චි කරනවා නේ, එක අලුත් දෙයක් නෙමේ. ඒ වගේම තමා Regular Expressions කියන්නෙත් අපිට අලුත් දෙයක් නෙමේ. අපි බලමු මේ දෙකම පාවිච්චි කරලා HTML textbox එකකට අපිට ඕනෙ characters විතරක් enter කරන්න දෙන්නේ කොහොමද කියල. මෙන්න මේක තමා අපි පාවිච්චි කරන HTML page එක. <!DOCTYPE html> <html> <head> <title>JS and RegEx</title> </head> <body> <label for="txtUsername">Username</lable> <input type="text" id="txtUsername" placeholder="Enter Username"/> </body> </html> මේ තියෙන page එක html විදිහට save කරලා browser එකේ බැලුවම මෙන්න මේ වගේ තමා පෙන්නේ. දැන් හිතන්න අපිට මේ username එකට @,#,%,$ වගේ