2 Replies - 643 Views - Last Post: 26 February 2013 - 04:50 PM Rate Topic: -----

#1 TheRudeBoy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-February 13

Using the value of an instantiated value as a method parameter

Posted 26 February 2013 - 12:27 AM

OK... this is something that should be simple, but I have spent hours upon hours for several days trying to figure this out, to no avail.

I apologize in advance if I'm not using the right terminology... hopefully it will be clear what I'm trying to accomplish. Also... I'm brand new to this forum, so if I'm not following any generally accepted rules for posting, don't flame me... just tell me politely.

Could some kind soul PLEASE tell me how to use a variable as an argument in a function parameter?

I'll try to give a simple example of what I am trying to do, and if I can get an answer that works, I think I can figure out how to incorporate it in the actual project I'm working on.

Let's say I have created a Stack Container named "alphabet" in a Windows Form, 'Form1'.
User input or actions (Button clicks, CheckBox or Radio Button selection/deselection, etc.) result in pushes of various letters of the alphabet to the stack.
For instance, we'll pretend that a Button Control named 'btnAtoE' generates a push of the letters "A", "B", "C", "D", and "E" to the stack.

The code would look something like:
    private void btnAtoE_Click(object sender, EventArgs e)
    {
       var alphabet = new Stack<char>();
       alphabet.Push('A');
       alphabet.Push('B');
       alphabet.Push('C');
       alphabet.Push('D');
       alphabet.Push('E');
    }



Now, let's say that other pushes may or may not have occurred, but I want to be able to programmatically check the stack to see if a particular letter exists on the stack... I can directly declare the value (letter) I'm looking for... with the code immediately below.

if (alphabet.Contains('D'))
{
   txtBoolResult.Text = "The letter D exists in alphabet stack container.";
}
else
{
  txtBoolResult.Text = "The letter D does not exist in the alphabet stack container.";
}



BUT... let's say (for whatever reason), I want to retrieve the letter I'm looking for from a variable or Form Control. The string variable 's' is declared and instantiated to equal the value input in a TextBox named 'txtSearchStack'.
The code below will NOT work

if (string.IsNullOrWhiteSpace(txtSearchStack.Text))
{
  MessageBox.Show("Please enter a letter to search for.");
  txtSearchStack.Focus():
}
else
{
  string s = txtSearchStack.Text;

  if (alphabet.Contains(s))
  {
    txtBoolResult.Text = "The letter " + s + " exists in alphabet stack container.";
  }
  else
  {
    txtBoolResult.Text = "The letter " + s + " does not exist in the alphabet stack container.";
  }
}



the if (alphabet.Contains(s)) is the part that doesn't work.
I also tried: if (alphabet.Contains(Convert.ToChar(txtContains.Text))) which DOES work.

If you have a solution on how I can use a variable (s) in place of ('<letter>'), I would be very appreciative.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Using the value of an instantiated value as a method parameter

#2 andrewsw   User is offline

  • head thrashing
  • member icon

Reputation: 6622
  • View blog
  • Posts: 27,031
  • Joined: 12-December 12

Re: Using the value of an instantiated value as a method parameter

Posted 26 February 2013 - 12:34 AM

Quote

I also tried: if (alphabet.Contains(Convert.ToChar(txtContains.Text))) which DOES work.

If you have a solution on how I can use a variable (s) in place of ('<letter>'), I would be very appreciative.

You've already solved this. You declared your Stack<char> to contain characters, not strings, so you need to convert the .Text, which is a string, to a character before you can compare it.

This post has been edited by andrewsw: 26 February 2013 - 12:34 AM

Was This Post Helpful? 3
  • +
  • -

#3 TheRudeBoy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-February 13

Re: Using the value of an instantiated value as a method parameter

Posted 26 February 2013 - 04:50 PM

View Postandrewsw, on 26 February 2013 - 01:34 AM, said:

Quote

I also tried: if (alphabet.Contains(Convert.ToChar(txtContains.Text))) which DOES work.

If you have a solution on how I can use a variable (s) in place of ('<letter>'), I would be very appreciative.

You've already solved this. You declared your Stack<char> to contain characters, not strings, so you need to convert the .Text, which is a string, to a character before you can compare it.


Doh!

Good call, Andrew!

When I changed string s = Convert.ToChar(txtContains.Text); to
char c = Convert.ToChar(txtContains.Text);
and if(alphabet.Contains(s)) to if (alphabet.Contains©)
it works perfect! It's the simple things that trip me up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1