Darn... I didn't know one could forget this much... if I have a string "2D6" and want to access the number before the "D" to use as an argument to a number of dice to roll, how do I do that properly?
More dice to the people!
Printable View
Darn... I didn't know one could forget this much... if I have a string "2D6" and want to access the number before the "D" to use as an argument to a number of dice to roll, how do I do that properly?
More dice to the people!
VB Code:
Module Module1 Sub Main() Dim myString As String = "2D6" Dim myNumber As Integer myNumber = Int32.Parse(Left(myString, myString.IndexOf("D"))) Console.WriteLine("myNumber = {0}", myNumber.ToString()) Console.WriteLine("Press <Enter> to exit ...") Console.ReadLine() End Sub End Module
Hmm... I do
and I end up with the error message:Code:Function GetDice(ByVal str As String) As Integer
GetDice = Int32.Parse(Left(str, str.IndexOf("D")))
End Function
'Public Overloads Property Left() As Integer' has no parameters and its return type cannot be indexed.
It seems the parser thinks I want to access the Left poperty of some object, and not call the Left() function... how do I get past that one?
one way of doing it
Code:myString = "2D6"
temp = myString.Substring(0, 1)
hmm..not sure why you got errors, i ran your code and it worked like a champ:
VB Code:
Module Module1 Sub Main() Dim myString As String = "21D8" Dim myNumber As Integer myNumber = GetDice(myString) Console.WriteLine("myNumber = {0}", myNumber.ToString()) Console.WriteLine("Press <Enter> to exit ...") Console.ReadLine() End Sub Function GetDice(ByVal str As String) As Integer GetDice = Int32.Parse(Left(str, str.IndexOf("D"))) End Function End Module
The code i used makes sure to grab any number of characters before the "D" as the number(eg 101, 55, or 666) and not just one character as myString.SubString(0,1) would return. But I guess it would be pretty hard to roll a single die and get a two or more digit roll :) so if you know you're always only going to need the first character as the number you can just use substring or left.
IT MUST GIVE ERROR!
left and right are PIXELS functions in .NET not string manip functions!
So is it safe to assume that(based on your response PT Exorcist) you tested the code and it didn't work? C'Mon duder, you didn't try the code, if you did you'd see that it for surely it works and has nothing to do with Pixels :)
OK, so for those concerned with using Left function then use this:
just keep in mind that Left worked with no probs!VB Code:
Module Module1 Sub Main() Dim myString As String = "21D8" Dim myNumber As Integer myNumber = GetDice(myString) Console.WriteLine("myNumber = {0}", myNumber.ToString()) Console.WriteLine("Press <Enter> to exit ...") Console.ReadLine() End Sub Function GetDice(ByVal str As String) As Integer GetDice = Int32.Parse(str.Substring(0, str.IndexOf("D"))) End Function End Module