Copying from an array skiping values

Help me please I'm begginer

Page 1 of 1

11 Replies - 546 Views - Last Post: 21 July 2009 - 02:06 PM Rate Topic: -----

#1 greatdragon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Copying from an array skiping values

Post icon  Posted 21 July 2009 - 08:58 AM

I have a array called "words":
ex:
words[0] = "something1";
words[1] = "something2";
words[2] = "something3";
words[3] = "something4";
words[4] = "something5";
words[5] = "something6";
etc..



but I want to add to a textBox just words[2] to the end, how can I do this?
I tried to make this like:
private void button3_Click(object sender, EventArgs e)
		{
			string myContent = textBox6.Text; 
			int last1;
			// definindo os separadores
			char[] delimiterChars = { ' ', ',', ':', '\t' };
			string[] words = myContent.Split(delimiterChars);
			// processo da array descricao
			last1 = words.Length;
			var subset = words.Skip(7);
			subset.ToString();
			// copiando
			comboBox1.Text = words[0];
			comboBox2.Text = words[2];
			comboBox3.Text = words[1];
			textBox1.Text = words[3];
			textBox3.Text = words[5];
			textBox8.Text = words[6];
			textBox9.Text = words[4];
			textBox2.Text = subset;
		   }

I think is wrong becouse I can't get the values, could you guys help me?
Thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Copying from an array skiping values

#2 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Copying from an array skiping values

Posted 21 July 2009 - 11:28 AM

Probably not getting any replies because this is VB? in the C# forum..

what's a Var? Where'd that Skip function come from?

Aet
Was This Post Helpful? 0
  • +
  • -

#3 greatdragon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Re: Copying from an array skiping values

Posted 21 July 2009 - 11:31 AM

no, this is c#
Was This Post Helpful? 0
  • +
  • -

#4 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Copying from an array skiping values

Posted 21 July 2009 - 11:35 AM

what namespace is "var" a member of?
Same question for the string? function of skip()?
I dont have either of those in my IDE ..
can I see the rest of the code?

Does it compile?
Was This Post Helpful? 0
  • +
  • -

#5 greatdragon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Re: Copying from an array skiping values

Posted 21 July 2009 - 11:37 AM

compile, anyways man I need a code that copy just a part of the array...
and I can't do it..
-edit-
oh sorry I can show the full code here is it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
		string result1, result2, result3, combo1, combo2, combo3, box1, box4, box5, box2, box3;
		private void Form1_Load(object sender, EventArgs e)
		{
			// Marcas
			comboBox1.Items.Add("Caloi");
			comboBox1.Items.Add("DaBomb");
			comboBox1.Items.Add("Dynamic");
			comboBox1.Items.Add("Pro-X");
			comboBox1.Items.Add("Redtek");
			comboBox1.Items.Add("GT");
			comboBox1.Items.Add("Schwinn");
			comboBox1.Items.Add("Blitz");
			// Modelos
			comboBox2.Items.Add("MTB");
			comboBox2.Items.Add("Speed");
			comboBox2.Items.Add("BMX");
			comboBox2.Items.Add("Freeride/DirtJump");
			comboBox2.Items.Add("Conforto");
			comboBox2.Items.Add("FullSuspension");
			comboBox2.Items.Add("Infantil");
			// Aros
			comboBox3.Items.Add("28");
			comboBox3.Items.Add("26");
			comboBox3.Items.Add("24");
			comboBox3.Items.Add("20");
			comboBox3.Items.Add("16");
			comboBox3.Items.Add("12");
		}

		int fSoma(int box1, int box2, int box3)
		{
			Int32.TryParse(textBox1.Text, out box1);
			Int32.TryParse(textBox1.Text, out box2);
			Int32.TryParse(textBox3.Text, out box3);
			return box1;

		}

		private void button1_Click_1(object sender, EventArgs e)
		{
			box1 = textBox1.Text;
			box2 = textBox2.Text;
			box3 = textBox3.Text;
			box4 = textBox8.Text;
			box5 = textBox9.Text;
			combo1 = comboBox1.Text;
			combo2 = comboBox2.Text;
			combo3 = comboBox3.Text;
			switch (combo1)
			{
				case "Caloi":
					result1 = "Caloi";
					break;
				case "DaBomb":
					result1 = "DaBomb";
					break;
				case "Dynamic":
					result1 = "Dynamic";
					break;
				case "Redtek":
					result1 = "Redtek";
					break;
				case "Pro-X":
					result1 = "Pro-X";
					break;
				case "GT":
					result1 = "GT";
					break;
				case "Schwinn":
					result1 = "Schwinn";
					break;
				case "Blitz":
					result1 = "Blitz";
					break;
			}
			switch (combo2)
			{
				case "MTB":
					result2 = "MTB";
					break;
				case "Speed":
					result2 = "Speed";
					break;
				case "Freeride/DirtJump":
					result2 = "Freeride/DirtJump";
					break;
				case "FullSuspension":
					result2 = "Full Suspension";
					break;
				case "BMX":
					result2 = "BMX";
					break;
				case "Conforto":
					result2 = "Conforto";
					break;
				case "Infantil":
					result2 = "Infantil";
					break;
			}
			switch (combo3)
			{
				case "28":
					result3 = "28";
					break;
				case "26":
					result3 = "26";
					break;
				case "24":
					result3 = "24";
					break;
				case "20":
					result3 = "20";
					break;
				case "16":
					result3 = "16";
					break;
				case "12":
					result3 = "12";
					break;
			}
			textBox6.Text = result1 + " " + result3 + " " + result2 + " " + box1 + " " + box5 + " " + box3 + " " + box4 + " " + box2;
		}

		private void button5_Click(object sender, EventArgs e)
		{
			textBox6.Visible = true;
			textBox6.BringToFront();
		}

		private void expandirToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			Form2 tool = new Form2();
			tool.Show();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void abrirDocumentoToolStripMenuItem_Click(object sender, EventArgs e)
		{
			string myContent;
			if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				StreamReader reader = new StreamReader(openFileDialog1.FileName);
				myContent = reader.ReadToEnd();
				reader.Close();
				textBox6.Text = myContent;
				saveFileDialog1.FileName = openFileDialog1.FileName.ToString();
			}


		}

		private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				StreamWriter writer = new StreamWriter(saveFileDialog1.FileName);
				writer.Write(textBox6.Text);
				writer.Close();
			}
		}

		private void button6_Click(object sender, EventArgs e)
		{
			//Pop.html
			textBox5.Text = textBox5.Text.Replace("$nomeproduto", box1);
			textBox5.Text = textBox5.Text.Replace("$quadro", box4);
			textBox5.Text = textBox5.Text.Replace("$descricao", box2);
			textBox5.Text = textBox5.Text.Replace("$fileend", box5);
			textBox5.Text = textBox5.Text.Replace("$nomeproduto", box1);
			textBox5.Text = textBox5.Text.Replace("$marca", result1);
			textBox5.Text = textBox5.Text.Replace("$modelo", result2);
			textBox5.Text = textBox5.Text.Replace("$aro", result3);
			textBox5.Text = textBox5.Text.Replace("$preco", box3);
		}

		private void salvarPophtmlToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				StreamWriter writer = new StreamWriter(saveFileDialog1.FileName);
				writer.Write(textBox5.Text);
				writer.Close();
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			textBox5.Visible = true;
			textBox5.BringToFront();
		}

		private void button3_Click(object sender, EventArgs e)
		{
			string myContent = textBox6.Text; 
			int last1;
			// definindo os separadores
			char[] delimiterChars = { ' ', ',', ':', '\t' };
			string[] words = myContent.Split(delimiterChars);
			// processo da array descricao
			last1 = words.Length;
			var subset = words.Skip(7);
			subset.ToString();
			// copiando
			comboBox1.Text = words[0];
			comboBox2.Text = words[2];
			comboBox3.Text = words[1];
			textBox1.Text = words[3];
			textBox3.Text = words[5];
			textBox8.Text = words[6];
			textBox9.Text = words[4];
			textBox2.Text = subset;
		   }

	}
}

This post has been edited by greatdragon: 21 July 2009 - 11:42 AM

Was This Post Helpful? 0
  • +
  • -

#6 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Copying from an array skiping values

Posted 21 July 2009 - 12:08 PM

"var" and "Skip" are specific to C# 3.0, which is available in Visual Studio 2008, and there is a way to get it to run in VS 2005 as well but it's not included by default. var is known as an implicit type, which allows you to declare a variable and assign anything to it, and the compiler will know what type it is automatically, i.e. var collection = new List<string>();

"Skip" is known as an Extension method for arrays and will return a new subset of the array after skipping x number of elements.

If you're trying to convert the remaining strings in the subset array to one big string, make sure you join them:

var subset = words.Skip(7);
string result = String.Join(" ", subset);
... OR TRY THIS:...
string result = String.Join(" ", words, 7, words.Length);
...
textBox2.Text = result;



You can pass any string as the first parameter to String.Join and have your strings delimited by this string. You can even pass an empty string if you want everything concatenated. Was this what you were asking for?
Was This Post Helpful? 0
  • +
  • -

#7 greatdragon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Re: Copying from an array skiping values

Posted 21 July 2009 - 12:23 PM

yes but this code doesn't work :/
first:

Quote

Argument': cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'string[]' E:\c#\projetos\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 223 46 WindowsFormsApplication1


second:

Quote

ArgumentOutOfRange was Unhandled

I think we just need a way to convert System.Collections.Generic.IEnumerable<string>' to 'string[]
thanks anyways!

This post has been edited by greatdragon: 21 July 2009 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#8 greatdragon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Re: Copying from an array skiping values

Posted 21 July 2009 - 01:08 PM

please guys, someone can help me? :<
Was This Post Helpful? 0
  • +
  • -

#9 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Copying from an array skiping values

Posted 21 July 2009 - 01:26 PM

var subset = words.Skip(7).ToArray();
string result = String.Join(" ", subset);
... OR TRY THIS:...
string result = String.Empty;
if (words.Length > 7)
    result = String.Join(" ", words, 7, words.Length-7);
...
textBox2.Text = result;


Was This Post Helpful? 1
  • +
  • -

#10 greatdragon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 08-November 06

Re: Copying from an array skiping values

Posted 21 July 2009 - 01:35 PM

thank you!
Was This Post Helpful? 0
  • +
  • -

#11 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

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

Re: Copying from an array skiping values

Posted 21 July 2009 - 01:36 PM

View Postgreatdragon, on 21 Jul, 2009 - 09:58 AM, said:

I have a array called "words":
ex:
words[0] = "something1";
words[1] = "something2";
words[2] = "something3";
words[3] = "something4";
words[4] = "something5";
words[5] = "something6";
etc..



but I want to add to a textBox just words[2] to the end, how can I do this?
I tried to make this like:
private void button3_Click(object sender, EventArgs e)
		{
			string myContent = textBox6.Text; 
			int last1;
			// definindo os separadores
			char[] delimiterChars = { ' ', ',', ':', '\t' };
			string[] words = myContent.Split(delimiterChars);
			// processo da array descricao
			last1 = words.Length;
			var subset = words.Skip(7);
			subset.ToString();
			// copiando
			comboBox1.Text = words[0];
			comboBox2.Text = words[2];
			comboBox3.Text = words[1];
			textBox1.Text = words[3];
			textBox3.Text = words[5];
			textBox8.Text = words[6];
			textBox9.Text = words[4];
			textBox2.Text = subset;
		   }

I think is wrong becouse I can't get the values, could you guys help me?
Thanks in advance :)


I'm not sure I understand what you are looking for. You always want to add words[2] to the end of textBox2? Or do you want to add a word from the array based on some condition?

If you always want to add words[2] to the end of the textbox just use this:

textBox2.Text = textBox2.Text + words[2];



That would add what is in the array to what is in the textbox. If that isn't what you want correct me. :)
Was This Post Helpful? 1

#12 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Copying from an array skiping values

Posted 21 July 2009 - 02:06 PM

Thanks for setting me straight on those methods and types. I haven't gotten to .NET 3.0 yet.. still using 2.0

Aet
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1