|
-
Dec 16th, 2008, 03:10 PM
#1
Thread Starter
New Member
Left
I'm trying to select the two left characters of a string (a date 12/08/08). Help under Left Function says to use "Left(string,length)". When I enter Left into the code, a "definition" line comes up which says "Left() as integer Gets or sets the distance, in pixels, between the left edge of the control . . ." How do I get the function which returns the left number of characters in a string? When I type in Left(Fd(i,8),2), Left gets a squiggly blue underline and the message "Public property Left() has no parameters and its return type cannot be indexed", which I think means its looking to return a distance. Right gets the same warning. The Mid function doesn't show any warnings, although I haven't tried the code. What am I missing? Thanks
-
Dec 16th, 2008, 03:14 PM
#2
Re: Left
Left is a left-over function from VB6 (sorry, couldn't resist). While you can still use it, the word alone defaults to Me.Left, which is in reference to the form. You will need to do something like VisualBasic.Left, or whatever it is, but I forget the exact namespace, because Left has been replaced by substring:
string = <startstring>.Substring(0,2)
You just replace <startstring> with your string variable, and replace that initial string with whatever you want to do with those first two characters. Substring returns the string starting at the first argument and extending the number of characters in the second argument.
My usual boring signature: Nothing
 
-
Dec 16th, 2008, 03:16 PM
#3
Re: Left
That's because your form also has a property called Left. You either fully qualify the function name (Microsoft.VisualBasic.Left) or better yet, use String.Substring function.
Code:
'For example, you need the first 2 characters of whatever value myString variable holds, you do this:
Dim myString As String = "whatever"
Dim the2CharsINeed As String = myString.Substring(0, 2)
Edit: Shaggy beats me on this
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Dec 16th, 2008, 04:00 PM
#4
Re: Left
's bout time I snuck one in ahead of anybody.
My usual boring signature: Nothing
 
-
Dec 17th, 2008, 08:48 AM
#5
Thread Starter
New Member
Re: Left
Thanks so much for the info. The help function in 2008Express still mystifies me at times. Hopefully when I look for Substring it will guide me toward the new way to get what used to be Mid and Right in VB6.
Thanks again - you are very considerate of this sorta good VB6 user trying to transition into 2008.
-
Dec 17th, 2008, 09:18 AM
#6
Fanatic Member
Re: Left
Substring in and of itself is very simple to grasp.
Code:
'Where do you want to start in the string?
'Remember that the first position is 0.
Dim start_position as Integer = 0
'How many characters do you want to return?
Dim NumberOfChars as Integer = 5
Text1.Substring(start_position, NumberOfChars)
That is the basics of it. This will help with the Left() Function because you start at 0 and get the Left x number of characters. So if you wanted the left 2 characters of a string then you would do Text1.Substring(0,2).
Now, if you want to do Right() then you have to find the length of the string you are trying to use:
Code:
'Get the length of the string.
'The .Trim is optional, it is equivalent to Trim() from vb6. I prefer to use it
'alot because none of the data I work with is not whitespace tolerant.
Dim start_position as Integer = Text1.Length.Trim
'How many characters do you want to return?
Dim NumberOfChars as Integer = 5
'Do your math ahead of time and make sure that you are not trying to
'start at or return more characters than are in your string
If NumberOfChars > Text1.Length then NumberOfChars = 0
start_position -= NumberOfChars
if start_position < 0 then start_position = 0
'If you do not specify how many characters to return then by default it will
'return all characters from the starting point.
Text1.Substring()
Now I am not putting in alot of error handling, I am trying to keep this simple, you will have to figure out what kind of handling you want to add. Where this gets tricky is if you want to start with a specific character within a string and you start substituting Start_position for the index of said character and all that. Hopefully this will give you a good start on it!!
Good Luck to you!!
D
Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP
Please Rate If I helped you. 
Please remember to mark threads as closed if your issue has been resolved.
Reserved Words in Access | Connection Strings
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|