Following method, I am using to validate the Base64
Public Function ValidateBase64String(ByVal sString As String) As Boolean
If (sString.Length <> 4) Then
Dim b As Byte() = Convert.FromBase64String(sString)
Return True
Else
Return False
End If
End Function
I am passing "johnjohn" as a string to the method and it is returning that following string is the base64 string. What is the reason that instead of returning false it returns true.
Following method works almost every time
Console.WriteLine(ValidateBase64String("johnjohn"))-> it will always returns false
Public Function ValidateBase64String(ByVal value As string) As Boolean
Try
If value.Length <> 4 AndAlso (Base64Decode(value) IsNot Nothing AndAlso System.Text.RegularExpressions.Regex.IsMatch(Base64Decode(value), "^[a-zA-Z0-9\+/]*={0,3}$")) Then
Return True
End If
Catch ex As Exception
End Try
Return False
End Function
Public Function Base64Decode(ByVal base64EncodedData As String) As String
Try
Dim base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData)
Return System.Text.Encoding.UTF8.GetString(base64EncodedBytes)
Catch ex As Exception
End Try
Return Nothing
End Function
Related
I have a binding source which has datasource from datatable
The table may have fields where its value is Null
When I code this, I get error : 'Public Function xTo0Double() As Double' has no parameters and its return type cannot be indexed
txt_inv_tot.Text = xTo0Double(bs_payh.Current("_INV_TOT")).ToString("N2")
Function xTo0Double(obj As Object) As Double
If IsDbNullNothingEmpty(obj) Then
Return 0
Else
Return CDbl(obj)
End If
End Function
Function IsDbNullNothingEmpty(obj As Object) As Boolean
If obj Is Nothing
Return True
End If
If IsDBNull(obj)
Return True
End If
Try
If String.IsNullOrEmpty(obj)
Return True
End If
Catch ex As Exception
End Try
Return False
End Function
I Did not understand why it gives error... I let the VS IDE correct the error, and it corrected like this
Function xTo0Double(obj As Object) As Object
If IsDbNullNothingEmpty(obj) Then
Return 0
Else
Return CDbl(obj)
End If
End Function
But I want to return Double, not Object
All this coding to avoid using If Then End If in each time I want to check for field value before using it. for example this code works
If IsDBNull(bs_payh.Current("_INV_TOT"))
txt_inv_tot.Text = "0.00"
Else
txt_inv_tot.Text = CDbl(bs_payh.Current("_INV_TOT")).ToString("N2")
End If
But I don't want to use this way each time I want to read field value that could be Null
Is there is a way ?
Function checkIsAuthorized(ByVal users As String) As Boolean
Dim UserJobCode As String = dcChris.getJobCode(users)
Dim values As String = My.Settings.canResendJobcode
Dim usersCode As String() = values.Split(",")
Return usersCode.Contains(UserJobCode)
'If usersCode.Contains(UserJobCode) Then
' canResend = True
' Return True
'End If
'canResend = False
'Return False
End Function
The commented portion of the function works, I was just trying to make the code more efficient with the return statement. But it does not return a bool because the global is still reading false. Doing it the old way worked.
Any ideas?
Dim canResend As Boolean
Function checkIsAuthorized(ByVal users As String) As Boolean
Dim UserJobCode As String = dcChris.getJobCode(users)
Dim values As String = My.Settings.canResendJobcode
Dim usersCode As String() = values.Split(",")
Return usersCode.Contains(UserJobCode)
End Function
This is what I settled on. It works great for my needs. Thanks everyone.
I'm trying to do a Bing Image Search Application (Azure Version), and i can't make progress. The code language is vb.net. Basically what i'm doing is trying to edit this code, that actually worked. Any Solutions ?
Function ExecuteQuery() As Boolean
Dim esito As Boolean = False
Try
Dim query As String = System.Web.HttpUtility.UrlEncode("Inception Movie")
Dim skip As String = "10"
Dim urlBase As New Uri("https://api.datamarket.azure.com")
Dim accountKey As String = "tymv8z6jFSdo4eQ3vsS5r8SZAmFtA24e6dmfyvaLh3U"
Dim credentials As New NetworkCredential(accountKey, accountKey)
Dim dsc As New System.Data.Services.Client.DataServiceContext(urlBase)
dsc.Credentials = New NetworkCredential(accountKey, accountKey)
Dim urlSearch As Uri = New Uri(("https://api.datamarket.azure.com/Bing/Search/Image?Query=%27" + query + "%27&$skip=" + skip))
Dim webResults = dsc.Execute(Of WebResult)(urlSearch)
For Each result As WebResult In webResults
ListBox1.Items.Add(result.Title)
ListBox1.Items.Add(result.Description)
singleValue = singleValue + 1
Next
esito = True
Catch ex As Exception
esito = False
End Try
Return esito
End Function
Partial Public Class WebResult
Private _ID As Guid
Private _Title As [String]
Private _Description As [String]
Private _DisplayUrl As [String]
Private _Url As [String]
Private _MediaUrl As [String]
Public Property ID() As Guid
Get
Return Me._ID
End Get
Set(ByVal value As Guid)
Me._ID = value
End Set
End Property
Public Property Title() As [String]
Get
Return Me._Title
End Get
Set(ByVal value As [String])
Me._Title = value
End Set
End Property
Public Property Description() As [String]
Get
Return Me._Description
End Get
Set(ByVal value As [String])
Me._Description = value
End Set
End Property
Public Property DisplayUrl() As [String]
Get
Return Me._DisplayUrl
End Get
Set(ByVal value As [String])
Me._DisplayUrl = value
End Set
End Property
Public Property Url() As [String]
Get
Return Me._Url
End Get
Set(ByVal value As [String])
Me._Url = value
End Set
End Property
Public Property MediaUrl() As [String]
Get
Return Me._MediaUrl
End Get
Set(ByVal value As [String])
Me._MediaUrl = value
End Set
End Property
End Class
I now solved the puzzle, founded a simple way to do this thing just using an class file:
1 - Just download this file: http://www.getcodesamples.com/src/15958EA3/F43E1E1A
2 - Then add the same file to your project
3 - Add the following code:
Dim strBingKey As String = "xxxaccountkeyxxx"
Dim bingClass As New Bing.BingSearchContainer(New Uri("https://api.datamarket.azure.com/Bing/Search/"))
bingClass.Credentials = New NetworkCredential(strBingKey, strBingKey)
Dim query = bingClass.Image("Inception" + "Movie", Nothing, "en-us", "Off", Nothing, Nothing, Nothing)
Dim results = query.Execute()
For Each result In results
ListBox1.Items.Add(result.Title)
ListBox1.Items.Add(result.MediaUrl)
Console.WriteLine(result.Thumbnail) 'ThumbNail type, need to convert to use in result list
Next
Thank you all for the patience!
I wrote this regex function
Public Function ValidateEmailAddress(ByVal txtEmailAddress As String) As Boolean
Dim pattern As String
pattern = "^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*#[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$"
If Regex.IsMatch(txtEmailAddress, pattern) Then
Return True
Else
Return False
End If
End Function
And on-calling the function in a sub procedure:
If ValidateEmailAddress(txtEmailAddress.Text) = True Then
Else
MessageBox.Show("Email Not Valid")
End If
Sub Procedure
When i enter the email, correctly or incorrectly i get the same message i.e email not valid,in addition to that on stepping through the code after the If statement it ignores the return true. Please can anyone spot what the issue is here?
As an alternative to BigYellowCactus's answer :- You can use something like this - I think this is preferable to using regex:
Public Shared Function IsValidEmailAddress(ByVal emailAddress As String) As Boolean
Try
Dim address As New MailAddress(emailAddress)
Return True
Catch ex As Exception
Return False
End Try
End Function
This will return False with #abc.com and bill# whereas just checking for # will return True
I've been doing some code review and this code seemed weird to me since it doesn't have any return statement:
Protected Function AddZero(ByVal vsInput As String) As String
If Len(vsInput) = 1 Then
AddZero = "0" & vsInput
Else
AddZero = vsInput
End If
End Function
Visual Basic treats the function name as the return value and does not return until the end of the function. In the code, you can see that AddZero (the function name) is set to one of two values depending on the if condition. That is how you can determine what is returned.
In VB, you have an implicit return at the end of the function.
The function name gets assigned the return value, like this:
Protected Function AddZero(ByVal vsInput As String) As String
AddZero = "0" ' The return value is "0"
End Function
You can exit a function (return) like this:
Protected Function AddZero(ByVal vsInput As String) As String
If vsInput = "0" Then
AddZero = vsInput;
Exit Function
End If
AddZero = "0" ' The return value is "0"
End Function