If I create a class in VB called Test, I noticed I can instantiate it like:
Dim test As New Test
or
Dim test As New Test()
What is the difference?
There's no difference, they are the same, VB.NET will ignore the () since no parameters are being passed in.
There isn't one. Both versions will call the parameter-less constructor of the Test class. The second one just has parentheses.
Related
this is the Interface:
Namespace IBasBS100
Public Interface IBasBSMoedas <DispId(0)> <Obsolete("This method is obsolete. Use Actualiza(BasBE100.BasBEMoeda objMoeda, ref string strAvisos) instead.", False)>
Sub Actualiza(objMoeda As BasBEMoeda, Optional strAvisos As String = "")
<DispId(18)>
Sub Actualiza(objMoeda As BasBEMoeda, ByRef strAvisos As String)
<DispId(2)>
when i try to call the function that is not depracated it gives me the ambiguous error message:
Dim avisos As String
Dim bsmoedas As IBasBSMoedas = MotorPRI.Base.Moedas
bsmoedas.Actualiza(m, avisos)
I think its calling the depracated method.
Is there any way that i could explicitly call the non depracated method "Actualiza(objMoeda As BasBEMoeda, ByRef strAvisos As String)"?
PS: I can not change the interface, it's an external Library
I can only assume that the interface code was written in C# because that VB code won't compile. In VB, you don't explicitly state when passing an argument by reference, so the there's no way to differentiate between those two methods when calling one in VB code. In C#, you specify the ref or out keyword when passing an argument by reference, so there is no ambiguity in which overload you are calling, so it's allowed.
In short, you cannot call that interface method in VB. You can implement the interface in a class in VB and use a different name for one of the methods but you'd still have to call it through the class, not the interface. If you want to be able to use that interface then you're going to have to do it in C# code. Perhaps write a library in C# for the code that uses that interface and then consume that in your VB app.
I guess the other alternative to do it in VB would be to do so via Reflection. Far from ideal but I don't see another way.
Primavera v10 funtions and VisualBasic aren't compatible with each other. Some may work, most won't. I would recommend changing to C# since this is the "supported" language of primavera.
I implemented an extension method on Integer (this is a simplified example that also shows the error)
<Extension()>
Public Function IsPositive(ByVal item As Integer) As Boolean
Return item > 0
End Function
I then try to call the extension method on a datarow's item:
Dim dtMyTable As DataTable
dtMyTable = GetInfoFromDatabase()
If dtMyTable.Rows(0).Item("nCount").IsPositive() Then
This gives me the exception:
Public member 'IsPostive' on type 'Integer' not found.
I assume this is because dtMyTable.Rows(0).Item("nCount") is actually an object, not an integer. The exception seems to understand that that isn't the case, so I'm not sure why that's different, but it is.
However, if I try to call the same method as if it's just a regular method, it works without complaint
If IsPositive(dtMyTable.Rows(0).Item("nCount")) Then
I would rather call it the former way. I know it's possible to just save the value to a variable and then call the extension on that variable, but that seems like a needless extra step.
Is there any way to get the former method to work without adding an extra variable assignment every time I need to call it, or changing the extension method to work on Objects?
below code is c#
ctx.CreateStreamResponse(stream => new Session(_Sessions, stream).Process(),"video/mp4");
and i need to this code as VB.NET code. am converting as below
ctx.CreateStreamResponse(Function(stream) New Session(_Sessions, stream).Process(), "video/mp4")
But getting error
overload resolution failed because no accessible
"CreateStreamResponse" can be called with these arguments.
CreateStreamResponse needs 2 parameters
Stream (as my sample Function(stream) New Session(_Sessions, stream).Process())
content type (as my sample "video/mp4")
Anyone can help me, please
I believe the issue seems to be that the method which you pass into CreateStreamResponse should be a Sub not a Function. i.e:
ctx.CreateStreamResponse(Sub(stream) New Session(_Sessions, stream).Process(), "video/mp4")
CreateStreamResponse takes an Action(Of Stream) delegate as the first argument and a contentType of String as the second argument.
Thus you need to use Sub rather than a Function as in this case an Action delegate can only encapsulate methods that return void (sub procedures). Also, ensure that the Process method being invoked is also a Sub procedure.
If the problem persists then as suggested by Microsoft docs:
Review all the overloads for the method and determine which one you
want to call.
In your calling statement, make the data types of the arguments
match the data types of the parameters defined for the desired
overload. You might have to use the CType Function to convert one or
more data types to the defined types.
for more information see here
So what does this mean and how do I fix it?
This message occurs if I place the New keyword in the line(s) below. If I remove it, i get an error at runtime saying I need to use New. What am I doing wrong?
Dim oPS As AeccPointStyle = New AeccPointStyle
ops = oDescKey.PointStyle
Debug.Print(oPS.Name)
Debug.Print(oPS.MarkerSymbolName)
Also tried
Dim oPS As New AeccPointStyle
ops = oDescKey.PointStyle
Debug.Print(oPS.Name)
Debug.Print(oPS.MarkerSymbolName)
Thanks!
Update 1 - based on comment from Meta-Knight
1 -
Dim oPS As AeccPointStyle = Nothing
oPS = oDescKey.PointStyle
2 -
Dim oPS As AeccPointStyle = oDescKey.PointStyle
Both versions throw NullReferenceExceptions.
The empty constructor of AeccPointStyle is marked as friend, which means only classes inside its assembly can call it.
But looking at your code, I don't think you need to call New. Just set it to Nothing at first. Or even better, directly set your variable with the good value:
Dim oPS As AeccPointStyle = oDescKey.PointStyle
Edit about your NullReferenceException:
Typically, this type of exception is raised when you call a property of an object with a value of Nothing. In this case, if oDescKey was set to Nothing, such an exception would be raised.
If oDescKey does NOT have a value of Nothing, then the only thing that executes some code is the PointStyle property. So it's safe to assume that the PointStyle property throws a NullReferenceException. Try watching the oDescKey.PointStyle variable in the debugger, you should see that it throws an exception.
If your AeccPointStyle class is using a 'Friend' modifier,
ie it is defined as:
Friend Class AeccPointStyle
or the default constructor has the 'Friend' modifier,
ie:
Friend Sub New()
and the code you posted is not in the same assembly, you cannot call the constructor on this class. In order to get this to work, you must put your code in the same assembly as the AeccPointStyle class. Check out this page to learn more about the modifiers: more information about modifiers
My guess is the following: AeccPointStyle is declared in another assembly than the code sample in your question. The constructor (Sub New) of AeccPointStyle is declared a Friend, which means that it is reachable only within the same assembly.
You can solve this in two ways
Change Sub New so that it is Public
Provide a Shared Public Sub Create, that will create and return a new AeccPointStyle
AeccPointStyle doesn't have a public default constructor. The one you're trying to use is limited to other classes within the same assembly.
Here's some code I found online:
Dim oPointStyle As AeccPointStyle
Set oPointStyle = g_oAeccDoc.PointStyles.Add(strName)
Notice the PointStyles property (probably some sort of PointStylesColleciton) on g_oAeccDoc is instantiating and returning a new AeccPointStyle instance for you.
When using FRIEND as access modifier for your class, you need to make sure that both the class it-self and the class where you use it are in the same NAMESPACE, otherwise you will get this error message.
For other people encountering this issue, I had to do a one-way upgrade for an old project we were referencing. It upgraded it to .NET 4.0 as well, when it was 2.0/3.5. That caused the (also old) project that was an older version to get this error when referencing it. Changing the newly upgraded version back to .NET 3.5 did the trick for me. It's a very non-descript error!
I have a following code which works fine
MsgBox(AddSomething(Of String)("Hello", "World"))
Public Function AddSomething(Of T)(ByVal FirstValue As T, ByVal SecondValue As T) As String
Return FirstValue.ToString + SecondValue.ToString
End Function
Now we are redesigning the application to work with parameters of different types which will be provided through XML
<SomeValues>
<Add Param1="Somedata" Param2="SomeData" MyType="String"/>
<Add Param1="Somedata" Param2="SomeData" MyType="MyBusinessObject"/>
</SomeValues>
If I try to provide the following it gives error as Of accepts only type
''''Get DetailsFromXml --- MyType,Param1,Param2
MsgBox(AddSomething(Of Type.GetType(MyType))(Param1,Param2))
How to solve this issue.
Edit
The above example is given to make the question simple. Actual issue is as follows
I am using SCSF of P&P.
Following is per view code which has to be written for each view
Private Sub tsStudentTableMenuClick()
Dim _StudentTableListView As StudentListView
_StudentTableListView = ShowViewInWorkspace(Of StudentListView)("StudentTable List", WorkspaceNames.RightWorkspace)
_StudentTableListView.Show()
End Sub
Now I want to show the views dynamically.
Public Sub ShowModalView(ByVal ViewName As String)
Dim _MasterListView As >>>EmployeeListView<<<<
_MasterListView = ShowViewInWorkspace(Of >>>EmployeeListView<<<)("Employee List", WorkspaceNames.RightWorkspace)
_MasterListView.Show()
End Sub
So the part shown using the arrows above has to be somehow dynamically provided.
The point of generics is to provide extra information at compile-time. You've only got that information at execution-time.
As you're using VB, you may be able to get away with turning Option Strict off to achieve late binding. I don't know whether you can turn it off for just a small piece of code - that would be the ideal, really.
Otherwise, and if you really can't get the information at compile-time, you'll need to call it with reflection - fetch the generic "blueprint" of the method, call MethodInfo.MakeGenericMethod and then invoke it.
I assume that the real method is somewhat more complicated? After all, you can call ToString() on anything...
(It's possible that with .NET 4.0 you'll have more options. You could certainly use dynamic in C# 4.0, and I believe that VB10 will provide the same sort of functionality.)
In .Net generics, you must be able to resolve to a specific type at compile time, so that it can generate appropriate code. Any time you're using reflection, you're resolving the type at run time.
In this case, you're always just calling the .ToString() method. If that's really all your code does, you could just change the parameter type to Object rather than use a generic method. If it's a little more complicated, you could also try requiring your parameters to implement some common interface that you will define.
If all you are doing is ToString, then making the parameters object instead would solve the problem in the simplest way. Otherwise you are going to have to bind the type at run-time, which in C# looks like:
System.Reflection.MethodInfo mi = GetType().GetMethod("AddSomething");
mi = mi.MakeGenericMethod(Type.GetType(MyType));
object result = mi.Invoke(this, new object[] { Param1, Param2 });
Because it involves reflection it won't be fast though... but I assume that's not a problem in this context.