Laman

Minggu, 25 Desember 2011

function multiple return value in C# (C Sharp)

Function adalah salah satu teknik modular, yaitu teknik untuk memecah program yang besar menjadi bagian bagian yang sistematis, tujuan utama dari function itu sendiri adalah untuk mempermudah programer dalam mendevelop software. Dalam suatu permasalahan kadang kita menemukan sebuah problem dimana fucntion yang dibuat mempunyai return value lebih dari satu, seperti kita kenal sekarang ini bahwasanya function jika direturn akan mengembalikan single value.

Ada banyak cara agar function yang dubuat mempunyai return value lebih dari satu, teknik dasarnya adalah merubah type return value dari type data primitif(string, int, double, float, dll) menjadi type data bentukan(array,stcut,list,kelas/object). disini akan dibahas bagaimana sebuah function direturnkan menggunakan type data struct. Sebelumnya perlu diketahui bahwa struct dan array itu berbeda, jika array adalah type data bentukan dengan  type data primitif yang mempunyai type data sama, sedangkan struct adalah type data bentukan yang mempunyai type data yang berbeda beda. Perhatikan contoh berikut

array int dengan panjang data 10
int[] arData=new int[10];

array double dengan panjang data 15
double[] arData=new double[15];

stuct dengan 3 datapembentukan string, int, dan double
struct myStruct
{
     public string sNama;
     public int iUmur;
     public double dGaji;
}


dari ilustrasi diatas, diharapkan sudah mempunyai gambaran tentang perbedaan struct dan array, oke.. sekarang lanjut ke pembuatan program function dengan return value struct (multiple value)
seperti biasa siapkan terlebih dahulu project kosong. klik file new project pada IDE visual studio setelah itu klik new prohject, isi nama project sesuai dengan keinginan. Setelah itu ikuti langkah demi langkah berikut ini
langkah 1 : tambahkan beberapa komponen pada form yang telah tersedia sebelumnya tadi, seperti label, textbox dan juga groubbox. Atur dan tatasedemikian rupa sehingga tampilannya seperti pada gambar dibawah ini

Gambar 1. Preview desain aplikasi

langkah 2 : double klik pada button satu (button yang memepunyai label login). dan tambahkan source code berikut ini

textBoxInfoStatus.Text = login(textBoxUsername.Text, textBoxPassword.Text).bStatus.ToString();
textBoxInfoUser.Text = login(textBoxUsername.Text, textBoxPassword.Text).sUser;
textBoxInfoPassword.Text = login(textBoxUsername.Text, textBoxPassword.Text).sPassword;    

langkah 3 : tambahakan struct dan fuction login,
function login
  private scLogin login(string sUsernam, string sPassword)
        {
           
        /*disini adalah letak code mengambil dan
        * mencocokan data user dan password dari database,
        * akan tetapi disini tidak menggunakan koneksi database
        * agar mudah dalam memahami barir baris program ini
        */
        scLogin oSclogin = new scLogin();
       if ((sUsernam == "username") && (sPassword == "password"))
        {               
            oSclogin.bStatus=true;
            oSclogin.sUser=sUsernam;
            oSclogin.sPassword=sPassword;
        }
         return oSclogin;
        }

Deklarasi Struct
 struct scLogin
        {
           public bool bStatus;
           public string sUser;
           public string sPassword;
        }

langkah 4 : Done, program selesai. Jika langkah demi langkah diatas dilakukan dengan benar, maka program saat dijalankanakan meminta menginputkan username dan password, karena alasan untuk mempermudah membaca source code, maka username dan passwordnya dibuat static, username="usernamae" dan password="password". Berikut ini adalah hasil running program

preview running program saat pertamakali dijalankan, usernmae diisi dengan username dan password diisi dengan passwo (sengaja disalahkan), maka tampilannya akan seperti berikut ini. dari gambar dibawah terlihat bahwa pada kotak informasi untuk status bernilai false,

Gambar 2. Preview hasil running program

sekarang mari masukan dengan username dan password dengan benar, username disiisi dengan username, dan password diisi dengan password, maka apa yang terjadi.? perhatikan gambar dibawah ini

 Gambar 3. Preview hasil running program

ketika username dan password diisi dengan benar, maka pada kotak informasi akan tampil informasi informasi  yang sesuai. perhatikan lagi pada source code yang ada di button login, jika diperhatikan source code yang ada dibutton login hanyalah function login, akan tetapi pada function login menggunakan type data bentukan struct yang telah dideklarasikan sebelumnya mengakibatkan function ini mampu menyimpan bermacam macam informasi sesuai dengan yang diinginkan

untuk lebih jelasnya dalam memahami program ini silahkan downlaod file program dan full project
download file program disini
download full project disini
for more information contact on informasi@csharp-indonesia.com

pandauan download : klik link download yang disediaan diatas, setelah itu akan muncul halaman adf.ly, tunggu beberapa detik sampai pada pojok kanan atas muncul tulisan skip ad. klik tombol tersebut ,setelah itu akan muncul halaman ziddu, silahkan klik pada button download yang muncul pada halaman ziddu
NB : Dalam tutorial ini program ditulisa menggunakan IDE Visual studio 2010. dengan target framework 3,5 . Sehingga minimal komputer harus terinstal windows vista atau atau windows xp dengan framework .NET 3.5 untuk bisa menjalankan program *.exe nya, Sedangkan untuk membuka projectnya mau tidak mau harus menginstal microsoft visual studio 2010


Artikel Terkait

61 komentar:

  1. mas bisa ga dengan menekan key enter langsung, tanpa klik login
    itu source code nya gimana makasih

    BalasHapus
  2. untuk melakukan login dengan menekan tombol enter pada textbox password atau username digunakan event yang namanya KeyUp, KeyDown, KepPresd. sebenernya masih banyak lagi event yang digunakan, tapi saya lebih suka menggunakan keUp atau keyDown. setelah event tersebut dibangkitakan tambahkan source berikut ini. konsepnya adalah pada textbox akan mendeteksi tombol apakah yang ditekan. dan jika tombol yang ditekan adalah tombol enter maka function untuk login dipanggil

    if (e.KeyCode == Keys.Enter)
    {
    btn_login.PerformClick();
    }
    else if (e.KeyCode == Keys.Escape)
    {
    btn_cancel.PerformClick();
    }

    BalasHapus
  3. Mas kapan ngasih contoh buat applikasi

    BalasHapus
  4. Semangat-Belajar C#31 Desember 2011 pukul 19.38

    hehe maaf ya.. belum sempat.....
    tar kalau sempat... langsung di upload dech....:-D

    BalasHapus
  5. berarti di dua textbox tersebut ya mas
    naruh source code nya ato beda?

    BalasHapus
  6. kalau naruh source codenya di textbox password atau textbox username terserah programernya, tapi kebanyakan source code diletakan di textbox password, karena sebagian besar user menekan tombol enter setelah memasukan username dan memasukan password, sehingga posisi terakhir kursor berada pada textbox password, kelemahan jika meletakkan source code ditextbox password ketika user kembali lagi ke textbox username dan melakukan tombol enter, function login tidak akan dipanggil, sehingga saya sarankan letakkan source code di textbox username dan ditextbox password, walau diletakkan di textbox password saya sebenarnya juga sudah bagus

    BalasHapus
  7. Wah tanya gan. Kenapa pake struct gan buat nampung datanya? Kenapa ga sekalian pakai class saja?

    BalasHapus
    Balasan
    1. kapan harus menggunakan struct,class,function void, function return, class tergantung dari permasalahan yang ada, jadi semuanya adalah masalah pilihan, manakah yang lebih efektif untuk menyelesaikan masalah yang ada
      suatu kasus mungkin lebih efektif d resolve menggunakan struct, kadang lebih efektif menggunakan class, kadang jg lebih efektif menggunakan function, jadi semuanya adalah masalah pilihan

      Hapus
  8. program c# net utk meng-capture citra dr picturebox1 ke picturebox2 gimana ya??
    mhon bantuan'a...
    tq

    BalasHapus
  9. disini yang dimaksud mengcapture citra dari picture box itu mendapatkan nilai pixelnya atau mendapatkan filenya
    kalau untuk mendapatkan nilai pixelnya gunakan getpixel()
    sedangkah untuk mendapatkan filenya silahkan gunakan library system,io
    dengan menggunakan library tersebut. citra bisa disimpan dari picturebox ke lokasi yang diinginkan

    untuk lebih jelasnya menyimpan image dari picturebox ke lakasi tertentu silahkan baca tutorial berikut
    http://www.csharp-indonesia.com/2011/04/menyimpan-gambar-dari-picturebox-di-c-c.html

    semoga bisa bermanfaat dan membantu

    BalasHapus
  10. mas mau tanya...
    caranya refresh form otomatis untuk beberapa menit/detik gmna y....?
    tengkyu...

    BalasHapus
  11. tambahkan komponen TIMIR ke windows desaigner, setelah itu letakkan function yang ingin direfres secara berkala baik menit, detik atau jam didalam event di timer, waktunya bisa diatur di komponen timernya,
    terimakasih, semoga bisa membantu dan bermanfaat

    BalasHapus
  12. ok, tengkyu mas...
    oy klo mau ngompile(bikin .exe) c# pake database my sql gmna tu mas caranya, hehhee

    BalasHapus
  13. kalau ingin mengkompile, tekan saja F5. F5 itu sama saja dengan dikompile setelah itu dijalankan. jadi setelah F5 ditekan pasti *.exe terbentuk. kalau untuk C# dan MYSQL silahkan baca artikel yang yang berhubungan dengan MYSQL diblok ini. lihat daftar isi. terimakasih semoga bisa membantu dan bermanfaat

    BalasHapus
  14. iya mas...
    mksdny klo mau d jadikan dalam bentuk instalan gtu? :)

    BalasHapus
  15. kalau memakai external data source dalam hal ini menggunakan DBMS MYSQL tidak bisa dijadikan satu dalam *.exe (master instalasi). kecuali jika database yang digunakan adalah local database dari Visual Studio atau menggunakan DBMS embeded database. Sebenarnya dengan menggunakan external datasource bisa di jadikan satu dengan file exe instalasi, hanya akan sangat ribet sekali, dengan mengconversinya menjadi script SQL.

    BalasHapus
  16. mas mohon pencerahan nya ,jika ingin mengetaui code apa saya yang harus di pakai dari sebuah Dll pada C#, Liatnya dari mana Ya mas..!

    BalasHapus
  17. bisa ga kalo function ini di pakai antar form yang berbeda? kalau bisa tolong contohkan

    BalasHapus
  18. bang klo buat ngatasin error "unhandled exception has occurred in your application" gmn ? erornya kluar pas dijalanin di sisi client. sama vshost.exenya gabisa jalan bang..solusinnya dong bang ke powered_kakikucing@yahoo.co.id

    BalasHapus
  19. There is definately a great deal to learn about this subject.
    I love all of the points you've made.

    Have a look at my web-site; Wrinkle Cream

    BalasHapus
  20. Appreciate the recommendation. Will try it out.

    Take a look at my page - Dietary supplement

    BalasHapus
  21. Definitely believe that which you stated. Your favorite reason appeared
    to be on the net the simplest thing to be aware of.

    I say to you, I certainly get irked while people consider worries that they just don't know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people could take a signal. Will likely be back to get more. Thanks

    My web site Buy Nuvocleanse

    BalasHapus
  22. This is my first time visit at here and i am actually
    happy to read everthing at single place.

    Visit my homepage; Buy zinncollection

    BalasHapus
  23. Thanks for finally writing about > "function multiple return value in C# (C Sharp)" < Loved it!

    Feel free to surf to my page: Nitric Power Review

    BalasHapus
  24. Hello there I am so excited I found your weblog, I really found you by accident, while I was browsing on Askjeeve for something else, Anyhow I am here now and would just like
    to say thanks a lot for a tremendous post and
    a all round thrilling blog (I also love the theme/design), I don't have time to read it all at the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the great job.

    Feel free to visit my web blog ... Nuva CLeanse Diet

    BalasHapus
  25. This is my first time visit at here and i am genuinely
    happy to read everthing at single place.

    Feel free to surf to my webpage :: Green coffee diet

    BalasHapus
  26. Fantastic website. Plenty of helpful info here. I am sending it to some friends ans additionally sharing in delicious.
    And naturally, thank you to your sweat!

    Also visit my homepage - Goji Berry Reviews

    BalasHapus
  27. Hey there! I know this is kinda off topic nevertheless I'd figured I'd
    ask. Would you be interested in exchanging links or maybe guest authoring
    a blog post or vice-versa? My website addresses a lot of
    the same topics as yours and I feel we could greatly benefit from each other.

    If you are interested feel free to shoot me an email. I look forward to hearing from you!
    Awesome blog by the way!

    my web blog Mito slim and nuvo cleanse

    BalasHapus
  28. Nice blog here! Also your web site loads up very fast!

    What web host are you using? Can I get your affiliate link to your
    host? I wish my website loaded up as fast as yours lol

    Visit my web blog: Test Force Xtreme

    BalasHapus
  29. I love what you guys are up too. Such clever work and exposure!
    Keep up the wonderful works guys I've included you guys to my own blogroll.

    Have a look at my web page Natural Cleanse Diet

    BalasHapus
  30. Nice post. I learn something new and challenging on websites I stumbleupon every day.

    It's always exciting to read articles from other authors and use something from their websites.

    Feel free to surf to my web site :: Tru Visage Skin Care

    BalasHapus
  31. It's amazing to go to see this website and reading the views of all mates on the topic of this piece of writing, while I am also keen of getting knowledge.

    My web site; Working Online

    BalasHapus
  32. I do not leave a lot of responses, but I looked at a few of the responses here "function multiple return value in C# (C Sharp)".
    I actually do have some questions for you if it's allright. Is it simply me or do some of the remarks look as if they are written by brain dead folks? :-P And, if you are writing at additional online social sites, I would like to keep up with you. Could you post a list of the complete urls of your social networking pages like your linkedin profile, Facebook page or twitter feed?

    Here is my webpage - http://vimaxmaleenhancements.com

    BalasHapus
  33. Greate article. Keep posting such kind of info on your
    blog. Im really impressed by your blog.
    Hey there, You have performed a great job. I'll definitely digg it and in my opinion recommend to my friends. I'm
    confident they will be benefited from this website.


    my blog post; Buy Vimax Male Enhancement

    BalasHapus
  34. Hi all, here every one is sharing these know-how, thus it's good to read this website, and I used to go to see this weblog every day.

    Here is my site: Lift vapor

    BalasHapus
  35. Hello to every body, it's my first pay a quick visit of this weblog; this weblog includes amazing and in fact good information for visitors.

    Feel free to visit my web-site; Order Xtrasize

    BalasHapus
  36. Howdy! I just want to give you a big thumbs
    up for your great info you have right here on this post.
    I am returning to your site for more soon.

    Here is my website :: Test Force Xtreme And Lean Muscle X

    BalasHapus
  37. Have you ever thought about writing an e-book or guest authoring on other sites?

    I have a blog based on the same ideas you discuss and would love to have
    you share some stories/information. I know my visitors would appreciate your work.
    If you are even remotely interested, feel free to shoot me an e
    mail.

    my blog Home income kit reviews

    BalasHapus
  38. Have you ever thought about creating an e-book or
    guest authoring on other blogs? I have a blog based on the
    same subjects you discuss and would love to
    have you share some stories/information. I know my subscribers
    would value your work. If you're even remotely interested, feel free to shoot me an email.

    Have a look at my homepage; Pureberry Max Trial

    BalasHapus
  39. Greetings I am so excited I found your weblog,
    I really found you by mistake, while I was researching on Bing
    for something else, Regardless I am here now and would
    just like to say many thanks for a remarkable post and a all round thrilling blog (I also love the theme/design), I don’t have time to read
    through it all at the moment but I have saved it and also added your RSS feeds, so when I have time I
    will be back to read a great deal more, Please do keep up the excellent b.


    Feel free to visit my blog - acai ultralean

    BalasHapus
  40. Wow that was odd. I just wrote an very long comment but after I clicked submit my
    comment didn't show up. Grrrr... well I'm not writing all that over again.
    Regardless, just wanted to say superb blog!

    Here is my weblog - buy garcinia cambogia

    BalasHapus
  41. Thanks to my father who told me regarding this website, this blog is actually awesome.



    Here is my blog :: Green coffee review

    BalasHapus
  42. Do you have a spam issue on this website; I also am a blogger,
    and I was curious about your situation; we have created
    some nice methods and we are looking to trade solutions with others,
    please shoot me an email if interested.

    Also visit my website Pay day loans

    BalasHapus
  43. Hey there! I just wish to offer you a huge thumbs up for the excellent info you have right here on this
    post. I will be returning to your web site for more soon.


    Review my web blog power pump no formula

    BalasHapus
  44. csharp-indonesia.con21 Juni 2013 pukul 00.39

    Error seperti itu pernah dibahas di blog ini.. error seperti itu memang susah di lakukan trace.. tp dari pengalaman pribadi saya... ketika saya melakukan upgrade visual studio permasalahan sudah hilang...
    Akan tetapi untuk penyebab pastinya kebapa kok sampai muncul eror yang seperti itu saya juga kurang tahu

    BalasHapus
  45. Saya tertarik dengan tulisan anda mengenai Teknologi. Teknologi merupakan suatu ilmu yang sedang berkembang di zaman yang sudah maju sekarang ini. Saya memiliki beberapa tulisan sejenis mengenai Teknologi yang dapat dilihat di www.ejournal.gunadarma.ac.id

    BalasHapus
  46. Moncler, an Italian company known for sporty, shiny down coats, posted a 60 percent jump in revenue from 2011 to 2013. In the first nine months of this year, its sales climbed another 18 percent. Moncler has been a staple in canada goose jakke Swiss ski lodges and the rest of the world’s snowy playgrounds for more than six decades. But it wasn’t until 2006 that the company introduced a haute couture line for women, and a high-fashion men’s line followed three years later. In the past year, Moncler has opened stores in dozens of conspicuously temperate places as part of a push to get its products into urban environments. It’s betting its future on Hawaii—where a Moncler store is already open for business—instead of the Himalayas.
    Reiss also found cheap ways to market canada goose outlet online the brand. He gave coats to bouncers at Toronto nightclubs and scalpers outside the city’s sports arenas. Canada Goose parkas were already popular with movie crews, and he capitalized on that. The company is a sponsor of the Toronto, Sundance, Berlin, and Busan film festivals. Producer Rose Ganguzza remembers calling up the coatmaker in February 2011 when she was making Kill Your Darlings, a drama about the coming-of-age of the Beats and a sensational 1944 murder. “We were going to be shooting scenes on the Hudson River in 20-below temperatures,” doudoune canada goose Ganguzza says, with perhaps a touch of exaggeration. Canada Goose outfitted the entire cast and crew for free. “They found loyal customers in every one of those people,” she says. “Dan Radcliffe”—the English actor plays a young Allen Ginsberg in the film—“every year wants a new Canada Goose jacket.”In late 2012, Sports Illustrated got in touch with the company to outfit a crew going to Antarctica to shoot the 2013 swimsuit issue. The supermodel Kate Upton ended up wearing one of the coats, a white Chilliwack bomber jacket, on the http://www.canadagoosejacketsoutletonlinesale.com cover of the magazine, her face haloed in coyote fur. In 2013, Canada Goose’s U.S. sales increased 25 percent; last year, they rose an additional 30 percent. Reiss expects sales to grow by half in 2015.
    But is that sufficient reason to make it Canada's national symbol?Historical significance aside, the beaver is a destructive animal, indiscriminately gnawing down trees, without a thought as to where they fall. Beaver dams block rivers and streams which can be harmless, but also can alter the landscape and cause flooding of arable lands.Nor is winter jackets canada the beaver handsome, with an admirable personality. The polar bear, of course, is alarmingly handsome and resolute -- the world's largest land carnivore you don't want to mess with.My choice for a national symbol 30 years ago -- and remains so today -- is the Canada Goose. Critics fret that Canada Geese are a nuisance in parks,

    BalasHapus
  47. Thanks for shring it..ireally liked it..
    always success...
    regards,
    Rio
    Thanks for shring it..ireally liked it..
    always success...
    regards,
    Rio
    http://www.kertasnasi.com
    http://www.khulafabiotech.com
    http://www.rajatempatsampah.com

    BalasHapus
  48. Nel complesso, mi piace soprattutto la combinazione in oro nero e rosa della Ingenieur Chronograph Edition “74th Members 'Meeting at Goodwood”,rolex replica mentre penso che la Ingenieur Chronograph Edition “W 125” piacerĂ  a tutti coloro che cercano un cronografo sportivo moderno senza fronzoli . Infine,replica rolex l'Ingenieur Chronograph Edition “Rudolf Caracciola” merita una visita se sei un fan dei nuovi orologi di ispirazione vintage.

    BalasHapus