# Visual basic help



## shuggans (Feb 4, 2011)

I am writing code that requires a string  variable to start with http:\\www..
using * as wildcards doesnt work in vb i just found out, so can someone help with this block of code?
        If TextBox_Homepage.Text = "Http:\\*" Then
            If TextBox_Homepage.Text = "Http:\\www.*" Then
                Var_Homepage = TextBox_Homepage.Text
            Else : Var_Homepage = "www." & TextBox_Homepage.Text
            End If
        Else : Var_Homepage = "http:\\www." & TextBox_Homepage.Text
        End If


----------



## meirb111 (Feb 4, 2011)

if your vb version is 2005 or higher use :

If TextBox_Homepage.text.StartsWith("Http:\\",1)= true Then
If TextBox_Homepage.Text. StartsWith("Http:\\www.",1)=true Then
...


----------



## Kreij (Feb 4, 2011)

Or you could strip off the first 7 (or 10) characters as a substring and validate it ...

if (TextBox_HomePage.Text.Substring(0, 7).Equals("Http:\\") = true) Then ...

Accomplishes same thing as what meirb111 posted.

@Meirb111 : I don't see a option to call StartsWith using an int as the second parameter.
Only StartsWith(string), StartsWith(string, CultureInfo), StartsWith(string, boolean, CultureInfo)


----------



## meirb111 (Feb 4, 2011)

Kreij said:


> Or you could strip off the first 7 (or 10) characters as a substring and validate it ...
> 
> if (TextBox_HomePage.Text.Substring(0, 7).Equals("Http:\\") = true) Then ...
> 
> ...




if you are talking about ",1" its short for: StringComparison.CurrentCultureIgnoreCase
if you use 1 it will compare low case letters and upper case letters the same


----------



## FordGT90Concept (Feb 4, 2011)

Personally, I'd do this:

```
If TextBox_Homepage.Text.ToLower().StartsWith("http://") Then
  If TextBox_Homepage.Text.ToLower().StartsWith("http://www.") Then
```


----------



## meirb111 (Feb 4, 2011)

FordGT90Concept said:


> Personally, I'd do this:
> 
> ```
> If TextBox_Homepage.Text.ToLower().StartsWith("http:\\") Then
> ...



read above


----------



## FordGT90Concept (Feb 4, 2011)

Edit: Visual Studio will accept "1" so long as Option Strict is off (it automatically casts it to StringComparison).  It is bad programming practice to reference any enumerator by it's numeric value because future iterations of the .NET framework could change the numbers associated with the values and/or they may add an Integer argument to StartsWtih which would make "1" call the wrong method.

Proper (Option Strict safe) is: StartsWith("http://", StringComparison.CurrentCultureIgnoreCase)


----------



## meirb111 (Feb 4, 2011)

FordGT90Concept said:


> Integer is not valid (unless you cast it to StringComparison). It would have to be StartsWith("http://", StringComparison.CurrentCultureIgnoreCase).



what are you talking about 1=StringComparison.CurrentCultureIgnoreCase
its short way always works vb 2005  and higher


----------



## FordGT90Concept (Feb 4, 2011)

See my edit.  C#.NET and F#.NET are strict all the time.


----------



## meirb111 (Feb 4, 2011)

FordGT90Concept said:


> See my edit.  C#.NET and F#.NET are strict all the time.



when you start a new project Option Strict is off by default
so it will work ,if you use it on good for you i use as the default
and like it that way.


----------



## Kreij (Feb 4, 2011)

I just have to add my 2 cents on this. 
We all code the way we like, and are comfortable with, but as coders we must consider 4 things when writing code (even a single line).
1) Does it work as intended?
2) Is it reliable?
3) Is it easily maintainable?
4) Is it efficient?

Let's examine using a hard-coded numeric value to denote an enumeration.
1) Does it work as intended? *Yes*
2) Is it reliable? *No*. If the enumeration or method call changes the code will at best fail, at worst will return wrong results which may be very hard to debug.
3) Is it easily maintainable. Only if you know what the specific type of the second parameter is, and have the enumerated values memorized. So *No*.
4) Is it efficient? *No*. Having to call additional methods (in this case a narrowing conversion) to perform a given task is always less efficient.
(Performance Tips and Tricks in .Net Application)

No offense meant to anyone, but we should strive as programmers to meet the above 4 criteria for our code whenever possible. 
We are all guilty of bad practices (I tend to access databases from UI classes instead of writing a complete data layer to manage DB access) because it's easier.
Easier is not always better when you look at the long term ROI of a project (You may not, but you can be certain your boss will).
You will especially appreciate someone using best practices if you get the dubious distinction of maintaining their code.

Just a few tips from yer Old Uncle Kreij (who is just as guilty of taking the easy way out sometimes.  )


----------

