14 Replies - 4751 Views - Last Post: 13 June 2008 - 01:13 PM Rate Topic: -----

#1 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 09:22 AM

public int ObtDestino()
		{
			switch (listBox1.text) //Here is my question: Why doesn't this work? 
			{
				case "Cochabamba":
					destination = 35;
					break;

				case "Sucre":
					destination = 30;

				case "Tarija":
					destination = 40;

				case "Beni":
					destination = 20;
			}
		}



I can't use the .Text property of the listbox. What could make it work?

Is This A Good Question/Topic? 0
  • +

Replies To: Can I use a switch case with the .text value of a listbox?

#2 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 09:32 AM

you can but you should have a default case just in case something is typed wrong

public int ObtDestino()
        {
string val = listBox1.text;
            switch (val) //Here is my question: Why doesn't this work?
            {
                case "Cochabamba":
                    destination = 35;
                    break;

                case "Sucre":
                    destination = 30;
                    break; 

                case "Tarija":
                    destination = 40;
                    break; 

                case "Beni":
                    destination = 20;
                    break; 

                default
                     destination = 0;
                     break;

            }
        }


This post has been edited by zakary: 13 June 2008 - 09:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 09:36 AM

I can't use the control in my class. Why is that?

I already put System.Windows.Forms in the beginning of the class.
What went wrong?
Was This Post Helpful? 0
  • +
  • -

#4 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 09:37 AM

did you add a references to System.Windows.Forms
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 09:46 AM

The way I would do it is pass a reference to a ListBox control to the method, that way the method can, if need be, be reused elsewhere in your application, like so


public int ObtDestino(System.Windows.Forms.ListBox lst)
{
      string val = lst.Text;
      switch (val) //Here is my question: Why doesn't this work?
      {
           case "Cochabamba":
               destination = 35;
               break;

           case "Sucre":
               destination = 30;
               break; 

           case "Tarija":
               destination = 40;
               break; 

           case "Beni":
               destination = 20;
               break; 

           default
                destination = 0;
                break;

       }
}



The original way you had it this method could only be used for a single ListBox. Just my 2
Was This Post Helpful? 0
  • +
  • -

#6 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 10:54 AM

Crap I need some help with this.

I made a method to tell me where the car is going:

public string ObtDestino(System.Windows.Forms.ListBox comboBox1)
		{
			string dest = comboBox1.Text;
			int valpeaje;

			switch (dest)
			{
				case "Beni":
					valpeaje = 20;
					break;

				case "Cochabamba":
					valpeaje = 35;
					break;

				case "Tarija":
					valpeaje = 40;
					break;

				case "Sucre":
					valpeaje = 25;
					break;

			}

			return valpeaje;// I GET AN ERROR IN THIS LINE. WHY?
		}


I want the method to be called in the Form.cs and give me the value of "valpeaje". Like this:

private void peajeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			string placa = textBox1.Text;
			int peajedest = OPeaje.ObtDestino();


			
		}


How do I use a method stored in a class I created and make it return my value?!
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 11:01 AM

You've declared valpeaje as an int type, but the signature of your method is looking to return a string. If the return value needs to be an int you need to change the signature of your method to


public int ObtDestino(System.Windows.Forms.ListBox comboBox1)




Also, when you're getting an error you need to let us know what error you're receiving :)
Was This Post Helpful? 0
  • +
  • -

#8 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 11:36 AM

Oh my god. How did I make such a crappy mistake. Haha! Thanks for the tremendous help. :D
Was This Post Helpful? 0
  • +
  • -

#9 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 12:16 PM

Need a little bit more help.

public int ObtDest(ComboBox comboBox1)
		{
			swtich(comboBox1.Text);
			{
				case "Beni":
				PDestino=20;

			}
			
		}


I want this method to change the value of my variable PDestino according to what is the selected item of the combobox. What am I doing wrong?

EDIT: Nevermind I see my mistake. I used public "int" and then used the string "" in the case. That made a contradiction.

This post has been edited by papuccino1: 13 June 2008 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#10 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 12:26 PM

public int ObtCobroNormal()
		{
			return ObtDest(????????)+ (PNumpasajero * 3);
		}

		public int ObtDest(ComboBox comboBox1)
		{
			switch (comboBox1.SelectedIndex)
			{
				case 0:
					PDestino = 20;
					break;

				case 1:
					PDestino = 35;
					break;

				case 2:
					PDestino = 25;
					break;

				case 3:
					PDestino = 40;
					break;

			}

			return PDestino;
			
		}


New problem. I want to use the value PDestino that is returned in the second method and use that return value in the first method.

What argument should I put inside the parenthesis in the first method?

This post has been edited by papuccino1: 13 June 2008 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#11 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 01:00 PM

What the hell does this mean?

"Error 1 No overload for method 'ObtDest' takes '1' arguments C:\Documents and Settings\Sergio\Mis documentos\Visual Studio 2008\Projects\Tarea N1 - 2008\Tarea N1 - 2008\CPeaje.cs 72 20 Tarea N1 - 2008
"
I get that when trying to use the ObtDest(????) method in another method.

What does it mean?

I want to use the returned value of ObtDest in the ObtCobroNormal method.

CRAP!

Plus here
Posted Image

Why is the control not available when I clearly placed "using System.Windows.Forms" at the very top of the class.

i'm so frustrated

This post has been edited by papuccino1: 13 June 2008 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 01:07 PM

I don't see where you have checkBox1 declared anywhere?
Was This Post Helpful? 0
  • +
  • -

#13 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 01:08 PM

How do I declare a control in a class? Should I have to?
Was This Post Helpful? 0
  • +
  • -

#14 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 01:09 PM

Post your current code.

Yes, you need to declare the control in a class, if you are not passing that control into the classes methods.
Was This Post Helpful? 0
  • +
  • -

#15 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Can I use a switch case with the .text value of a listbox?

Posted 13 June 2008 - 01:13 PM

class CPeaje
	{
		#region "Atributos"
		string Placa;
		int Destino;
		string Tipov;
		int Numpasajero;
		#endregion



		#region "Propiedades"
		public string PPlaca
		{
			get { return Placa; }
			set { Placa = value; }
		}

		public int PDestino
		{
			get { return Destino; }
			set { Destino = value; }
		}

		public string PTipov
		{
			get { return Tipov; }
			set { Tipov = value; }
		}

		public int PNumpasajero
		{
			get { return Numpasajero; }
			set { Numpasajero = value; }
		}
		#endregion




		#region "Constructores"
		public CPeaje(string Plac, int Dest, string Tipo, int Nump)
		{
			PPlaca = Plac;
			PDestino = Dest;
			PTipov = Tipo;
			PNumpasajero = Nump;
		}
		#endregion



		#region "Metodos"
		public void Guardar(string Plac, int Dest, string Tipo, int Nump)
		{
			PPlaca = Plac;
			PDestino = Dest;
			PTipov = Tipo;
			PNumpasajero = Nump;
		}

		public int ObtCobroNormal()
		{
			return ObtDest(I WANT THE VALUE OF OBTDEST return HERE).

		}

		public decimal ObtRebaja()
		{
			return ObtCobroNormal() * 10 / 100;
		}

		public decimal ObtRebaja(CheckBox checkBox1, NumericUpDown)
		{
			if (checkBox1.Checked)
				return ObtCobroNormal() * Porcentaje / 100;
			
		}

		public int ObtTipov(RadioButton op1)
		{
			if (op1.Checked)
				return ObtCobroNormal() * 20 / 100;
			else
				return ObtCobroNormal() * 5 / 100;				

			
		}

		public int ObtDest(int PDestino, ComboBox comboBox1)
		{
			switch (comboBox1.SelectedIndex)
			{
				case 0:
					PDestino = 20;
					break;

				case 1:
					PDestino = 35;
					break;

				case 2:
					PDestino = 25;
					break;

				case 3:
					PDestino = 40;
					break;

			}

			return PDestino;
			
		}
	   



		#endregion

	}

This post has been edited by papuccino1: 13 June 2008 - 01:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1