Parallel one Optional AND one ParamArray in a sub - asp.net

I'm not so professional in asp .net vs2012 vb, and now I've a problem:
This is the declaration of the sub:
Public Sub DocFill(ByVal DocName As String, ByVal Optional OK As Boolean=False, ByVal ParamArray BmValues() As String)
And the error message is:
Error 7 'BmValues' is not declared. It may be inaccessible due to its protection level.
I tried change the order, but the last value always drop this error
Is possible to declare soehow parallely this two params or not?
Thanks for the help!

A possible solution is to cook up something like this:
Public Sub DocFill(ByVal DocName As String, ByVal ParamArray BmValues() As Object) ' Declare BmValues as an array of Object.
Dim OK As Boolean = False ' Declare the OK Boolean within the method.
If BmValues.Length > 0 Then ' Check whether BmValues contains anything.
If BmValues(0).GetType() is GetType(Boolean) Then ' Check whether BmValues' first value is a Boolean.
OK = CBool(BmValues(0)) ' If so, set OK to the first value in BmValues.
End If
...
End If
...
End Sub

Related

Task.Factory.StartNew does not work when run without debug mode

This is the first time i am implementing Task.Factory.StartNew, so i dont have much idea about it. I have a function that returns a boolean value and takes a parameter. Now I have called my function using Task.Factory.StartNew.
Task.Factory.StartNew(Function() Myfunction(stringvalue))
When i am in debug mode it works and does all the operation. But when i run it without debug, it does not work. I had placed a Response.Write inside that function and it did not execute that, so it just does not enter into that function.
Below is the function that shows what am i trying to do
My Button Click event
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Tasks.Task.Factory.StartNew(Function() Myfunction("Somevalue"))
End Sub
My function
Private Function Myfunction(ByVal str as String) as Boolean
Dim isSuccess as Boolean = False
Dim Id as Integer = SomeOtherFunction(str)
If Id > 0 Then
isSuccess = AnotherFunction(Id)
End If
Return isSuccess
End Function
If i use Tasks.Task.Factory.StartNew(Function() Myfunction("Somevalue")).Start(), it works and does what the function is expected to do, but i get exception saying - "Start may not be called on a task that was already started"
What am i doing wrong?

Calling Methods dynamically

I am trying to call methods dynamically and I have issues. Could some one please help
I have the following vb code
Module
Sub Main
if user-input = RP1 then
createRP1()
elseif user-input = RP2 then
createRP2()
end if
end sub
sub createRP1()
...
end sub
sub createRP2()
,...
end sub
End Module
The CreateRP1/CreateRP2 method does not have any arguments. There are some n number of reports. So I do not want to write all those if or switch conditions for this. I want to write some thing simple so I tried this
1 Dim type As Type = "["GetType"]"()
2 Dim method As MethodInfo = type.GetMethod("Create" + user-input)
3 If method IsNot Nothing Then
4 method.Invoke(Me, Nothing)
5 End If
Line 1 and 4 are not working
Line 4 is not working because "me" does not go with module. But how to rewrite 1? I saw this somewhere in StackOverflow site
You can get the Type of the current Module like this:
Dim myType As Type = MethodBase.GetCurrentMethod().DeclaringType
Since it's a Module, all of the methods are essentially Shared (e.g. static, non-instance methods), so you can just pass Nothing for the obj parameter of the MethodInfo.Invoke method:
Dim methodName As String = "Create" & userInput
Dim method As MethodInfo = myType.GetMethod(methodName)
method.Invoke(Nothing, Nothing)
However, rather than using reflection, you may also want to consider using a dictionary of delegates so that it would be more deterministic and type-checked at compile time. For instance:
Dim methods As New Dictionary(Of String, Action)
methods.Add("RP1", AddressOf CreateRP1)
methods.Add("RP2", AddressOf CreateRP1)
' ...
methods(userInput).Invoke()
I think the best solution would be to create a "Report" object through designing a class. Then you could easily make things on the fly.
function (byRef user-input As String) As Report
if not "s" & user-input = "s"
Dim user-report As Report = new Report(user-input)
end if
end function
Then your Report class
Private reportNumber As String
Private data As String
Public Sub New(byVal reportNumber As String)
Sub createRP(byRef repotNumber As String)
// use report # here to select the correct data...
// so if it was sql....
// "SELECT data FROM report_table where report_num =" & reportNumber
end Sub
end Sub

How to share a variable value with a webmethod?

I have this code:
Dim main_id As int
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
main_id =1
End Sub
<WebMethod()> _
Public Shared Function BindPersonnel(ByVal product_id As String) As String
Dim project_id AS int16
project_id=main_id 'this doesn't work
End Function
On page load, I set the value of variable main_id and I need a way to somehow share the main_id with webmethod function, how can this be done? I have not tried session variable and I don't want to use session variable. I found this link Accessing a common variable in WebMethod and jQuery but I can't figure out how this is going to solve my problem. I also read some posts about using hidden field, which requires me to go two trips. I would love to avoid that.
WebMethods are independent of the other variables on the page. If you want to access main_id, you could declare it as Private Shared main_id As Integer, but that would then cause all of your users to have access to the same ID value, which you probably don't want.
Perhaps the easiest way to do this is to store the value in SessionState, and enable sessionstate access in the WebMethod. On the other hand, this removes the asynchronous-like functionality that you are looking for (it may not be an issue).
SessionState will give you the ability to have a per-session value (avoiding the use of the Shared solution mentioned above).
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("main_id") = 1
End Sub
<WebMethod(EnableSession:=True)>
Public Shared Function BindPersonnel(ByVal product_id As String) As String
Dim project_id As Integer = CInt(HttpContext.Current.Session("main_id"))
End Function

Entity Framework - passing different object sets as a parameter in a function

I have the following code:
Private Sub setDropdowns()
Using licensingModel As New licensingEntities
SetUpDropdowns(licensingModel.tblLookup_Country, "CountryName", "CountryName", country)
SetUpDropdowns(licensingModel.tblLookup_Country, "CountryName", "CountryName", bizCountry)
SetUpDropdowns(licensingModel.tblLookup_Salutation, "SSalutation", "SSalutation", salutation)
SetUpDropdowns(licensingModel.tblLookup_OrgType, "OrgType", "OTAuto", organisationType)
End Using
End Sub
and the sub SetUpDropdowns:
Private Sub SetUpDropdowns(ByVal entity As IObjectSet(Of EntityObject), ByVal textColumn As String, ByVal valueColumn As String, ByVal combo As RadComboBox)
combo.DataSource = entity
combo.DataBind()
End Sub
My problem is that i dont know how to define the parameter type for the sub. Because they are different types of objectSets being passed each time, I thought IObjectSet(Of EntityObject) would work, but it gives me the following error:
Unable to cast object of type 'System.Data.Objects.ObjectSet1[licensingModel.tblLookup_Country]' to
type
'System.Data.Objects.IObjectSet1[System.Data.Objects.DataClasses.EntityObject]'‌
Would anyone have a solution for this?
can you not just use object as your parameter?

Why am I getting InvalidCastException passing a string to a VariantType parameter?

I have:
Protected Sub SepInsert(ByVal mriId As VariantType,
ByVal aeId As VariantType,
ByVal absId As VariantType)
...
End Sub
and want to call it with a DropDownList selection like this:
Protected Sub cmdNewPrelinkedMri_Click(ByVal sender As Object,
ByVal e As System.EventArgs
) Handles cmdNewPrelinkedMri.Click
SepInsert(ddlMriUnassigned.SelectedValue, -1, -1)
End Sub
where the ddl selected value is, of course, a string (e.g., "0412B0").
I am getting an InvalidCastException saying: Conversion from string "0412B0" to type 'Integer' is not valid. Why does it want to convert to 'Integer' when the parameter is declared as VariantType?
What am I misunderstanding or doing wrong? Thanks,
Chris
Some statement in SepInsert is trying to force mriId into an int. Show the whole code if you can't find it.

Resources