I have a public function still i am not able to call that function using the object?It says the function is not a member of the class - vb.net

I am using visual studio 2010
I have created a public function in one class.when i try to call the that function using the objects.I am getting an error that the function is not a member of that class.
BusinessLayer :Status.vb
Public Class Status
Public Function CheckPUServiceLine(ByVal pintScenario As Integer) As Integer
Dim objStatusDac As New DataAccess.StatusDac
Dim intResult As Integer
intResult = objStatusDac.CheckPUServiceLine(pintScenario)
Return intResult
End Function
End Class
Vb:
Dim objStatusBiz As New Status
intResultForServiceLineCheck = objStatusBiz.CheckPUServiceLine(1)
When i try to call the function CheckPuServiceLine I am getting an error that
the function CheckPUServiceLine is not a member of Status

This looks like a Namespace issue. Status is a common enough class name, that I wouldn't be surprised if you are loading the wrong Status class.
Try to be explicit about the Status class, using the namespace for the class library.
Dim objStatusBiz As New MyNamespace.Status
You can usually find out which Namespace you are using by clicking on the Status class and pressing F12 to go to the definition for the class (or right clicking and choosing to go to definition).

Related

Create class method with already existing name from a DLL

I come from this post : Create class method with already existing name, where I explain that I need to make code compatible with MS Access and PostgreSQL. My first thought was to create a new class for PostgreSQL support and rewrite the functions previously used. The solution brought could work, but the problem is that the MS Access database class is loaded from a DLL (Microsoft Shared\DAO\dao360.dll, DAO.DBEngine). How can I interface or override those DLL functions and get rid of this "ambiguous name detected" error ?
As said in the answer to your first post you can use the Implements feature to do this. Without further details and yout code it is just more than difficulut to tell where your issue is.
IDatabase could look like that
Option Explicit
Sub OpenDB(fileName As String)
End Sub
Function ReadData(lngNr As Long) As String
End Function
clsAccess like that
Option Explicit
Implements IDatabase
Dim m_Dbs As DAO.Database
Dim m_Rcd As DAO.Recordset
Dim m_Filename As String
Sub IDatabase_OpenDB(fileName As String)
m_Filename = fileName
Set m_Filename = OpenDatabase(m_Filename, , True)
End Sub
Function IDatabase_ReadData(lngNr As Long) As String
Set m_Rcd = m_dbs.OpenRecordset("qryTable", dbOpenSnapshot)
m_Rcd.Move lngNr - 1
IDatabase_ReadData = m_Rcd.Fields("fldName").Value
m_Rcd.Close
End Function
You need to add the references in Tools/References.

Can I create a global object and assign its value globally in VBA?

I've had no trouble creating a new object globally, but when I try to assign values to the object globally, a Compile Error pops up (Expected end of statement).
Just started with VBA, I feel like I'm going about this the wrong way. Couldn't find anything about this online.
I've listed the Class and Sub below.
Class
'Class name: FileManager
Option Explicit
Public FileName, Path As String
Public Printer As Integer
Module Variable declaration
Public Scripts As Worksheet
Public CDM As New FileManager
Public CDM.Printer = 1 '<--Where the error occurs

set and get properties on Shared Libraries

i created a comp.dll for my project and then add the dll to my projects.sln (multiple projects), for every project on projects.sln i added comp.dll, and created 1 mdlmain.vb
Module mdlMain
Public master_FA As New master_FixAsset
Public objComp As New component.clsFunction
End Module
and 1 clsmain.vb to call main form on that project.
in comp.dll i made a property :
Private pubUserName as String
Public Property userName As String
Get
Return pubUserName
End Get
Set(value As String)
pubUserName = value
End Set
End Property
i changed the properties through my login.vbproj (inside projects.sln), but when i tried to get the value of that properties (from fixasset.vbproj) it returns nothing. Is it because i declare New component.clsFunctionin fixasset.vbproj so the compilers (?) create another new object in my .sln ? How can i fix this ?

How to mock a method (custom behavior) with Rhino Mocks in VB.NET

How can I mock one method with RhinoMocks in VB.Net? The reference I found is in C#:
Expect.Call(delegate{list.Add(0);}).IgnoreArguments()
.Do((Action<int>)delegate(int item) {
if (item < 0) throw new ArgumentOutOfRangeException();
});
SharpDevelop converts this to:
Expect.Call(Function() Do
list.Add(0)
End Function).IgnoreArguments().Do(DirectCast(Function(item As Integer) Do
If item < 0 Then
Throw New ArgumentOutOfRangeException()
End If
End Function, Action(Of Integer)))
But that doesn't work either (it doesn't compile).
This is what I want to do: create a new object and call a method which sets some properties of that method. In real-life this method, will populate the properties with values found in the database. In test, I would like to mock this method with a custom method/delegate so that I can set the properties myself (without going to the database).
In pseudo-code, this is what I'm trying to do:
Dim _lookup As LookUp = MockRepository.GenerateMock(Of LookUp)()
_luvalue.Expect(Function(l As LookUp) l.GetLookUpByName("test")).Do(Function(l As LookUp) l.Property = "value")
Unfortunately you're attempting to do both a Sub lambda and a Statement Lambda. Neither are supported in VS2008 (but will be in the upcoming version of VS). Here is the expanded version that will work for VB
I'm guessing at the type of m_list
Class MockHelper
Dim m_list as new List(Of Object)
Public Sub New()
Expect(AddressOf CallHelper).IgnoreArguments().Do(AddressOf Do Hepler)
End Sub
Private Sub CallHelper()
m_list.Add(0)
End Sub
Private Sub DoHelper(ByVal item as Integer)
if item < 0 Then
Throw New ArgumentOutOfRangeException
End If
End Sub
End Class
I have never mocked something w/ both a delegate and a lambda so I can't give a full solution to this problem, but I did want to share some example code for the usual "AssertWasCalled" function in Rhino Mocks 3.5 for vb developers because I spent some time trying to grok this... (keep in mind the below is kept simple for brevity)
This is the method under test - might be found inside a service class for the user object
Public Sub DeleteUserByID(ByVal id As Integer) Implements Interfaces.IUserService.DeleteUserByID
mRepository.DeleteUserByID(id)
End Sub
This is the interactive test to assert the repository method gets called
<TestMethod()> _
Public Sub Should_Call_Into_Repository_For_DeleteProjectById()
Dim Repository As IUserRepository = MockRepository.GenerateStub(Of IUserRepository)()
Dim Service As IUserService = New UserService(Repository)
Service.DeleteUserByID(Nothing)
Repository.AssertWasCalled(Function(x) Wrap_DeleteUserByID(x))
End Sub
This is the wrap function used to ensure this works w/ vb
Function Wrap_DeleteUserByID(ByVal Repository As IUserRepository) As Object
Repository.DeleteUserByID(Nothing)
Return Nothing
End Function
I found this to be a very nasty solution, but if it helps someone w/ the same issues I had it was worth the time it took to post this ;)

Dynamic properties for classes in visual basic

I am a vb.net newbie, so please bear with me. Is it possible to create properties (or attributes) for a class in visual basic (I am using Visual Basic 2005) ? All web searches for metaprogramming led me nowhere. Here is an example to clarify what I mean.
public class GenericProps
public sub new()
' ???
end sub
public sub addProp(byval propname as string)
' ???
end sub
end class
sub main()
dim gp as GenericProps = New GenericProps()
gp.addProp("foo")
gp.foo = "Bar" ' we can assume the type of the property as string for now
console.writeln("New property = " & gp.foo)
end sub
So is it possible to define the function addProp ?
Thanks!
Amit
It's not possible to modify a class at runtime with new properties1. VB.Net is a static language in the sense that it cannot modify it's defined classes at runtime. You can simulate what you're looking for though with a property bag.
Class Foo
Private _map as New Dictionary(Of String, Object)
Public Sub AddProperty(name as String, value as Object)
_map(name) = value
End Sub
Public Function GetProperty(name as String) as Object
return _map(name)
End Function
End Class
It doesn't allow direct access in the form of myFoo.Bar but you can call myFoo.GetProperty("Bar").
1 I believe it may be possible with the profiling APIs but it's likely not what you're looking for.
Came across this wondering the same thing for Visual Basic 2008.
The property bag will do me for now until I can migrate to Visual Basic 2010:
http://blogs.msdn.com/vbteam/archive/2010/01/20/fun-with-dynamic-objects-doug-rothaus.aspx
No - that's not possible. You'd need a Ruby like "method_missing" to handle the unknown .Foo call. I believe C# 4 promises to offer something along these lines.