Instantiating a function - vb.net

This question is about a different instance that im trying to instantiate...
I have to get the "Read" function from my Cardreader class and return a string to it on the form1.vb .... Now i did what i can remeber but for some reason i'm having a problem with the brackets.... What can i do to fix this?
Form1.vb
ThisATM.getCardReader.Readr("TEST TEXT IS FUN")
CardReader.vb
Public Function Readr(ByVal card As KeyCard) As String
Return Read
End Function
Link for the image of the card reader function. I thought this link of the image of the code would be easier to understand.

The Readr function takes an KeyCard as parameter, not an string. So it seems like you have to create an instance to an KeyCard and use that as a parameter instead. In the code in the image you provided you are creating a keycard object, it seems like you should use that object in the Readr function like this:
Dim ThisKeyCard as new KeyCard("1234","5678","Mikki Monster")
Dim returnString as string=ThisATM.getCardReader.Readr(ThisKeyCard)

Related

How to get return value from function used in thread in vb. Net

Public function myfn1(byval pRequest as string) as string
Dim param(1) object
param(0)=pRequest
Dim T as new thread(Addresof myfn2)
T. Start(param)
End function
Private function myfn2(byval pReq as string) as string
'////some stuff here////
Return lstrResponse
End function
Here myfn1 is accepting requests from user. Sometimes the requests may be concurrent at a time. So I have used thread in myfn1. Myfn2 is actually processing the request and returning the response. So I am willing to get that response in myfn1 after the thread processed the task. What should I do? Or is there any other way out, Pls suggest
You should look into using the Async/Await structure : https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/async/
For Doing CPU bound work on a separate thread there are a couple options. I like using Task.Run() doc here: https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run?view=netframework-4.8
You can awaitthe task you create and get the result when it's done like:
SomeVariable = Await Task.Run(Function() FunctionName)

What are DShellFolderViewEvents and DShellFolderViewEvents_Event?

The names make it sound like some sort of event handler.
Both were identified as an interface from Shell32 using the code below:
I can't find either of these in the MSDN libraries. Google returned some pages with code where they appeared, but I didn't see anything that really described the interface.
Public Sub ListTypes()
Dim NS As String = "Shell32"
For Each t As Type In Assembly.GetExecutingAssembly().GetTypes()
If (t.IsClass Or t.IsInterface) And t.Namespace = NS Then
Debug.Print(t.Name)
End If
Next
End Sub
Based on the definition in ShlDisp.h it appears to simply be a thin wrapper around IDispatch with a different GUID.
MIDL_INTERFACE("62112AA2-EBE4-11cf-A5FB-0020AFE7292D")
DShellFolderViewEvents : public IDispatch
{
};
It seems to be used in obtaining event notification from the shell - Raymond Chen's blog has some example code.

Call instance method inline after New statement

How can i convert this code to VB.net
public void SetBooks(IEnumerable<Book> books)
{
if (books == null)
throw new ArgumentNullException("books");
new System.Xml.Linq.XDocument(books).Save(_filename);
}
in http://converter.telerik.com/ it says:
Public Sub SetBooks(books As IEnumerable(Of Book))
If books Is Nothing Then
Throw New ArgumentNullException("books")
End If
New System.Xml.Linq.XDocument(books).Save(_filename)
End Sub
But visual studio says "Syntax error." because of "New"
What is the keyword for this situation, i searched on Google but no result.
Actually, you can do it in one line with the Call keyword
Call (New System.Xml.Linq.XDocument(books)).Save(_filename)
You cannot initialize an object and use it in one statement in VB.NET (as opposed to C#). You need two:
Dim doc = New System.Xml.Linq.XDocument(books)
doc.Save(_filename)
In C# the constructor returns the instance of the created object, in VB.NET not.

How do you remove this text in vb net?

Whenever I use streamwriter to put a textbox value into a .txt file it goes like this:
System.Windows.Forms.TextBox, Text: dadadadaad what is wrong here?
Dim Na_1 As New IO.StreamWriter(folder & "\name.txt")
Na_1.WriteLine(TextName)
Na_1.Close()
You have flush the StreamWriter before you close it:
Na_1.Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
Source: MSDN
So this should be your code:
Na_1 As New IO.StreamWriter(folder & "\name.txt")
Na_1.WriteLine(TextName)
Na_1.Flush()
Na_1.Close()
Alternatively you can use the Using-statement which automatically flushes and closes the stream (this is the better practice):
Using Na_1 As New IO.StreamWriter(folder & "\name.txt")
Na_1.WriteLine(TextName)
End Using
Following the comments of Ric and ForkAndBeard I think I misunderstood the problem:
Your problem is that you are calling the TextNameobject of the TextBox-Class directly via Na_1.WriteLine(TextName).
Now since you can't write an object to a file, the runtime simply call the ToString()-method of the Class TextBox which inherits from Object.
The result will be following:
"System.Windows.Forms.TextBox, Text: "
or generally:
"YourNamespace.YourClass"
Source:MSDN
If you want to have the text of the TextBox, you have to call the property TextBox.Text of the object:
Na_1.WriteLine(TextName.Text);

Vb.Net Moq: Intercepting parameter and set to variable

I have a mocked object and i want to assign a variable with the parameter i'm calling it with:
Dim myMockedObject = new Mock(Of MyObject)()
Dim catchedVariable As MyEventArgs
myMockedObject.Setup(Sub(x) x.MyMethod(Of MyEventArgs)(It.IsAny(Of MyEventArgs)))
I need to find a way to fill the catchedVariable
Was not able to figure out a way to use an out parameter(Method is ByVal and don't want to change it just for testing).
Tried Moq method like CallBack but no succes there.
Got it working with the Callback:
_args As MyEventArgs
myMockedObject.Setup(Sub(x) x.MyMethod(Of MyEventArgs)(It.IsAny(Of MyEventArgs)())).Callback(Sub(x As MyEventArgs) _args = x)