dcsimg
Results 1 to 3 of 3

Thread: [C#] Returning value and using it in if statement without determining the value

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2008
    Location
    >> ( ҉ )
    Posts
    413

    [C#] Returning value and using it in if statement without determining the value

    Can I create a function work like this following example (it doesn't work for real), but hope you understand what I mean.

    Code:
    void MyFunction()
    {
         var loginCheck = new Action(() => {
              if(textBox1.Text == "User") {
                   return true;
              } else {
                   return false;
              }
         });
    
    
         // I would like to re-use it like this.
    
         if(loginCheck()) {
             // Login is OK.
         }
    }
    Well.. I don't know how to do this action/function whatever return a true or false like this fake example. Also I would like to know how to use custom string instead of bool value, but returning bool is more important for me.

    Thanks!

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: [C#] Returning value and using it in if statement without determining the value

    I'm not sure what you are trying to accomplish, but here goes two suggestions:

    Is there any reason why you are inlining a method instead of just putting it beside it? Why not this?
    Code:
    private bool LoginCheck()
    {
        return textBox1.Text == "User";
    }
    
    private void MyFunction()
    {
        if (LoginCheck())
        {
            // Do something
        }
    }
    If you really want to inline the method you could do the following:
    Code:
    private void MyFunction()
    {
        var loginCheck = new Func<bool>(() => 
        {
            return textBox1.Text == "User";
        });
    
        if (loginCheck.Invoke())
        {
            // Do something
        }
    }
    The difference between a Func and an Action is that Func allows (read: requires) you to specify a return value, while Action only allows you to provide function arguments.

    Also, as a minor note to your provided example: Your bracing and spacing is not in line with the C# Coding Conventions. While it is not a real issue, if another developer will ever have to work with your code, it's nice to have it compliant to the language's conventions (this goes for any language). Though it's entirely up to you want to follow it or not.
    Delete it. They just clutter threads anyway.

  3. #3
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Exclamation Re: [C#] Returning value and using it in if statement without determining the value

    Try the following .
    Code:
    private void button1_Click(object sender, EventArgs e) {
                List<string> Actions = new List<string>();
                List<string> gets = Actions.FindAll(s => s.Contains("Users"));
                Actions.Add(textBox1.Text);
                foreach (string result in Actions.Where(s => s.IndexOf("Users") == 0))   {
                    MessageBox.Show("Login is OK");
    
                }         
             
            }
    if in login form the data entered into textbox is not validated . you can try the following way.
    Code:
    private void button1_Click(object sender, EventArgs e) {
                List<string> Actions = new List<string>();
              //  List<string> gets = Actions.FindAll(s => s.Contains("Users"));
                Actions.Add(textBox1.Text);
                foreach (string result in Actions.Where(s => s.IndexOf("Users") == 0))   {
                    MessageBox.Show("Login is OK");
                    return;
    
                }
                MessageBox.Show("Login is not ok ");
             
            }
    Last edited by firoz.raj; Mar 28th, 2013 at 02:37 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width