params string and foreach

WPF, passing in an indeterminate amount of strings to a method

Page 1 of 1

11 Replies - 2066 Views - Last Post: 21 July 2009 - 05:15 AM Rate Topic: -----

#1 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

params string and foreach

Post icon  Posted 17 July 2009 - 12:22 PM

Slightly different problem I have now,

I'm passing in various variables into my method to be stored into a collection. I am storing string, ints and bools which I can (now) do quite easily, however, I now need to pass in some parameters. These are taken from a listbox that has been populated by the user.

This is my SAVE code:
private void btnSaveCont_Click(object sender, RoutedEventArgs e)
		{
			if (tbName.Text == "")
			{
				MessageBox.Show("Please enter a continent name and ensure all fields are entered and territories are assigned", "Error");
			}
			else
			{
				string cname = tbName.Text;
				int cbonus = int.Parse(tbBonus.Text);

				//I'M GUESSING I NEED A FOREACH LOOP HERE TO GET THE ITEMS IN THE LIST AND STORE THEM?
				if (cbOverride.IsChecked == true)
				{
					if (cbPartially.IsChecked == true)
					{
						 
						gbl_clijst.clijst.Add(new cdata(cname, cbonus, true, true, //THIS IS WHERE I NEED TO PASS IN THE STRINGS));
					}
					else
					{
						gbl_clijst.clijst.Add(new cdata(cname, cbonus, true, false));
					}
				}
				else
				{
					gbl_clijst.clijst.Add(new cdata(cname, cbonus, false, false));
				}
			}
		}

So, let's assume the user has two entries in the listbox (e.g. Lightning and Thunder), how would I pass these through into my method?

This is my class and method they are being passed into:
class cdata
	{
		public string cname;
		int bonus;
		bool cbP, cbO;

		public cdata(string cname, int bonus, bool cbP, bool cbO, params string[] AssignedTList)
		{
			this.cname = cname;
			this.bonus = bonus;
			this.cbP = cbP;
			this.cbO = cbO;
			Debug.WriteLine("This collection contains " + cname + ", " + bonus + ", " + cbP + ", " + cbO + ", " + AssignedTList);
		}
	}

	static class gbl_clijst
	{
		static gbl_clijst()
		{
			Debug.WriteLine("** creating application scope instance");
			clijst = new List<cdata>();
		}

		static public List<cdata> clijst;
	}


Mind you, I could be well off here and there is a simpler way to do this...lol

Premier2k

Is This A Good Question/Topic? 0
  • +

Replies To: params string and foreach

#2 firexware  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 06-July 09

Re: params string and foreach

Posted 17 July 2009 - 12:51 PM

foreach (ListBoxItem x in lstListbox.Items)
{
//the listbox text is in ListBoxItem.ToString()
}


Was This Post Helpful? 0
  • +
  • -

#3 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: params string and foreach

Posted 17 July 2009 - 12:52 PM

I think I'm partway there!

I have added this into my IF statements:
ArrayList territs = new ArrayList();
for (int i = 0; i < lbAssigned.Items.Count; i++)
{
	territs.Add(i);
}

So now, I have the items in the listbox(lbAssigned) stored in an array, but how do I pass this array into my method?
I tried this:
gbl_clijst.clijst.Add(new cdata(cname, cbonus, true, true, territs));

But I get an error.

What's wrong with it?

Prem.
Was This Post Helpful? 0
  • +
  • -

#4 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: params string and foreach

Posted 17 July 2009 - 01:04 PM

Now I have this:
{
ArrayList territs = new ArrayList();
foreach (ListBoxItem x in lbAssigned.Items)
{
	  string territ = x.ToString();
	  territs.Add(territ);
}
gbl_clijst.clijst.Add(new cdata(cname, cbonus, true, false, territs));
}



but passing territs still produces an error. Am I passing the right thing?

Prem.
Was This Post Helpful? 0
  • +
  • -

#5 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: params string and foreach

Posted 17 July 2009 - 02:16 PM

What error exactly?
Was This Post Helpful? 0
  • +
  • -

#6 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: params string and foreach

Posted 17 July 2009 - 02:30 PM

View PostRenagado, on 17 Jul, 2009 - 01:16 PM, said:

What error exactly?

The error I'm getting is: The best overloaded method match for 'datahandling.cdata.cdata(string, int, bool, bool, params string[])' has some invalid arguments
Was This Post Helpful? 0
  • +
  • -

#7 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: params string and foreach

Posted 17 July 2009 - 03:11 PM

O right, I think I see the problem. You declared your constructor for cdata to take a string-array(am talking about the last argument) but you're passing a string(territs). So basically you have 3 options as far as I see them:
1. Change the constructor for cdata
2. Make a 2nd constructor taking just a string+the others(thats called an overload)
3. Load territs into an array containing just that string
Was This Post Helpful? 0
  • +
  • -

#8 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: params string and foreach

Posted 17 July 2009 - 04:47 PM

View PostPremier2k, on 17 Jul, 2009 - 02:04 PM, said:

Now I have this:
{
ArrayList territs = new ArrayList();
foreach (ListBoxItem x in lbAssigned.Items)
{
	  string territ = x.ToString();
	  territs.Add(territ);
}
gbl_clijst.clijst.Add(new cdata(cname, cbonus, true, false, territs));
}



but passing territs still produces an error. Am I passing the right thing?

Prem.


The method should be written with this as you no longer need the params part because you are passing in an ArrayList not a series of strings.

public cdata(string cname, int bonus, bool cbP, bool cbO, ArrayList AssignedTList)
{
    // .....
}



What you would be doing with the params part to call the method would be passing in many strings like this method call.

cdata("some string", 10, true, false, "string1", "string2", "string3", "string4");


Was This Post Helpful? 0
  • +
  • -

#9 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: params string and foreach

Posted 19 July 2009 - 03:55 AM

I tried SixofElevens suggestion but I'm still getting errors.
My data code:
class cdata
	{
		public string cname;
		int bonus;
		bool cbP, cbO;

		public cdata(string cname, int bonus, bool cbP, bool cbO, ArrayList AssignedTList)
		{
			this.cname = cname;
			this.bonus = bonus;
			this.cbP = cbP;
			this.cbO = cbO;
			Debug.WriteLine("This collection contains " + cname + ", " + bonus + ", " + cbP + ", " + cbO + ", " + AssignedTList);
		}
	}

	static class gbl_clijst
	{
		static gbl_clijst()
		{
			Debug.WriteLine("** creating application scope instance");
			clijst = new List<cdata>();
		}

		static public List<cdata> clijst;
	}

and my SAVE code:
private void btnSaveCont_Click(object sender, RoutedEventArgs e)
		{
			if (tbName.Text == "")
			{
				MessageBox.Show("Please enter a continent name and ensure all fields are entered and territories are assigned", "Error");
			}
			else
			{
				string cname = tbName.Text;
				int cbonus = int.Parse(tbBonus.Text);

				if (cbOverride.IsChecked == true)
				{
					if (cbPartially.IsChecked == true)
					{
						foreach (ListBoxItem x in lbAssigned.Items)
						{
							string territ = x.ToString();
							territs.Add(territ);
						}
						gbl_clijst.clijst.Add(new cdata(cname, cbonus, true, true, territs));
					}
					else
					{
						foreach (ListBoxItem x in lbAssigned.Items)
						{
							string territ = x.ToString();
							territs.Add(territ);
						}
						gbl_clijst.clijst.Add(new cdata(cname, cbonus, true, false, territs));
					}
				}
				else
				{
					foreach (ListBoxItem x in lbAssigned.Items)
					{
						string territ = x.ToString();
						territs.Add(territ);
					}
					gbl_clijst.clijst.Add(new cdata(cname, cbonus, false, false, territs));
				}
			}
		}


When I try to save I get a error:
InvalidCastException
Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ListBoxItem'


Err, ok. I'm assuming it's complaining because I'm trying to convert from one type to another. How do I get round this? I thought I had done that correctly with x.ToString()?

Prem.
Was This Post Helpful? 0
  • +
  • -

#10 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: params string and foreach

Posted 20 July 2009 - 09:00 AM

In what line are you getting the error? Could you put a comment at the end of it?
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: params string and foreach

Posted 20 July 2009 - 09:20 AM

try this instead...

foreach (ListBoxItem x in lbAssigned.Items)
{
       string territ = x.Content.ToString();
        territs.Add(territ);
}


Was This Post Helpful? 0
  • +
  • -

#12 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: params string and foreach

Posted 21 July 2009 - 05:15 AM

View Posteclipsed4utoo, on 20 Jul, 2009 - 08:20 AM, said:

try this instead...

foreach (ListBoxItem x in lbAssigned.Items)
{
       string territ = x.Content.ToString();
       territs.Add(territ);
}


Ah, didn't see your response.
I've changed my code now and it seems to work!
This is my SAVE code:
 foreach (object x in lbAssigned.Items)
{
	 string territ = x.ToString();
	 territs.Add(territ);
}
gbl_clijst.clijst.Add(new cdata(cname, cbonus, true, false, territs));

and I have slightly modified my debug code:
String strVal = "";
foreach(object o in AssignedTList)
{
	 strVal += o.ToString();
	 strVal += ", ";
}
Debug.WriteLine("This collection contains " + cname + ", " + bonus + ", " + cbP + ", " + cbO + ", " + strVal);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1