-2

I am currently splitting a string from a textbox that the user will fill with three numbers. Those numbers i want to be saved as seperate integers. Any help as to how to do this? Thanks for any help!

string[] count = txtPoemInput.Text.Split('/');  //Splitting values for keyword numbers

int Poem, Line, Word;

count[0] = Poem.ToString; // Example
count[1] = Line;          // Example
count[2] = Word;
Jaskier
  • 1,075
  • 1
  • 10
  • 33
jack mc
  • 1
  • 2
  • 2
    So do you want co convert `string` to `int` or `int` to `string`? And what have you already tried? – dymanoid Dec 20 '18 at 15:29
  • I want to take my text box which is type string and split it into 3 different integer variables. I have tried Int32.Parse ive tried starting the variables as a string and then changing it to Ints. – jack mc Dec 20 '18 at 15:32

1 Answers1

0

Here is what you need to do. Use Convert.ToInt32

Poem = Convert.ToInt32(count[0]);
Line = Convert.ToInt32(count[1]);
Word = Convert.ToInt32(count[2]);
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • Ok I'll try that thank you! – jack mc Dec 20 '18 at 15:32
  • 1
    `Convert.ToInt32` will throw an exception if the text entered is not numeric (or, at least, can't be parsed as a string). Use `int.TryParse` instead if you want to be defensive about it, or wrap it in a `try catch`: –  Dec 20 '18 at 15:37
  • my textbox is a masked text box so only numeric values can be entered but thanks – jack mc Dec 20 '18 at 15:43
  • This is how I would go about it (opinion, I know). I don't see any easier way to go about converting the `string` values to `int`. This should work fine as long as the values being passed are numeric- which as OP has stated, they are. – Jaskier Dec 20 '18 at 16:01