Why does a VB.Net function that returns string only actually return a single character? - vb.net

I'm calling a function that returns a string, but it's only actually returning the first character of the string it's supposed to be returning.
Here's a sample piece of code to recreate the issue I'm experiencing:
Public Function GetSomeStringValue(Value as Integer) As String
... Code Goes here
Return Some_Multicharacter_string
End Function
The function call looks like:
SomeStringValue = GetSomeStringValue(Value)
Why is this not returning the entire string?

Note: this answer was originally written by the OP, Kibbee, as a self-answer. However, it was written in the body of the question, not as an actual separate answer. Since the OP has refused repeated requests by other users, including a moderator, to repost in accordance with site rules, I'm reposting it myself.
After trying a hundred different things, refactoring my code, stepping through the code in the debugger many times, and even having a co-worker look into the problem, I finally, in a flash of genius, discovered the answer.
At some point when I was refactoring the code, I changed the function to get rid of the Value parameter, leaving it as follows:
Public Function GetSomeStringValue() As String
... Code Goes here
Return Some_Multicharacter_String
End Function
However, I neglected to remove the parameter that I was passing in when calling the function:
SomeStringValue = GetSomeStringValue(Value)
The compiler didn't complain because it interpreted what I was doing as calling the function without brackets, which is a legacy feature from the VB6 days. Then, the Value parameter transformed into the array index of the string (aka character array) that was returned from the function.
So I removed the parameter, and everything worked fine:
SomeStringValue = GetSomeStringValue()
I'm posting this so that other people will recognize the problem when/if they ever encounter it, and are able to solve it much more quickly than I did. It took quite a while for me to solve, and I hope I can save others some time.

Related

VB NET How to execute some code at the start and at the end of any function

I like to try to optimize my code, and I would like to measure the time taken by a function.
I have a class named Chrono. So I just have to add chrono.start at the beginning of the function, and chrono.stop at the end. My class chan also add the times it measure in a list, to then have average time, total time...
It works. Only problem is when there is exit sub or return in the middle of the function. Not really a problem, I just add a Try at the begginf of the function, and put my chrono.stop in the finally portion. I'm not sure it's really efficient, but it works.
So here is my question : I would like to have a function taking function name as parameter, that will automatically launch and stop my class when this function is called. I have heard of Reflection, but I have no idea how to use it. And it's really hard to search for this question in the internet (because the words are too common : "do something at the end of a function")
To resume, my code works, no problem. It's just constraining to add code to a function for just a short period of time (and sometimes forgot to remove it).
Thx (I'm french and hope I'm understandable)
This is how you can use reflection to call a method by name:
using System.Reflection;
public int InvokeMethod(string name)
{
int time1 = 1; //call your chrono.start here.
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(name);
theMethod.Invoke(this, new object[] { 1 });
int time2 = 10; //call your chrono.end here.
return time2 - time1;
}
However, there is a problem. How will you know what parameters to pass to the function? In the code above, I'm passing the integer 1 (new object[] { 1 }) just for example. So this code cannot be automated, but if you run it manually against each function one by one, then you can change that line to pass the correct arguments and make it work without having to modify your function.
This is to answer your question as how to call a function by name using reflection. However, it is much easier to call it using a delegate (or Fuc<T, tRsult> in .Net v3.5 and higher).

Using a function without returning a value visual basic

I'm making a batch development kit in visual basic and i need to be able to call a function that sets textboxes to a saved files text. How do i do this without returning? I tried this, and it lets me run the program, but gives me a warning, not an error. How do i go about doing this? Here is my little function design. P.S. I recently switched back to VB from Java and i'm so used to doing public void. Thanks in advance!
Public Function loadProject()
End Function
You want a Sub, which is the equivalent to the Java void method.
Public Sub LoadProject()
End Sub
It's not a bad Idea to just have a function that returns a value like a success statement just in case you need it. A call to the function doesn't have to accept or use the return value from the function.
You could even build a class with two values - txtpreviousvalue and txtnewvalue
Have your function return that type and fill an instance of the type with the respective values.
One day, if you need it, you'll have it.
P.S. I'm only posting this as an answer because the good answer posted by sstan is not marked as the answer; You should probably do that.

Use of dbms_assert.noop

What is actual use of dbms_assert.noop ?
Since this function performs no error checking and returns the input string as it was entered.
Read here on Pete Finnigan's blog where he discusses the dbms_assert.noop function. it mentions:
This (dbms_assery.noop), we can now clarify is used to mark a piece of code that is not to be tested by automated test tools (presumably Fortify) as this function does nothing except return the string passed unchanged.
By this, what I have understood that I can use this function where I am passing actual piece of code through a variable and I don't want it to be checked for SQL injection attacks.
This is one of the many uses of this function.

Write String.Join(Of T) in VB.Net

I have a simple code in C#:
Console.WriteLine(string.Join<char>("", ""));
And I can't convert it to VB.Net. Even reflector show me code in VB like:
Console.WriteLine(String.Join(Of Char)("", ""))
But it can't be compiled becouse I have an starge error:
Error 1 Expression expected.
It looks like VB.Net don't have this generic method at all.
Both project use Net Framework 4.
Why this error happened?
UPD:
I've create a custom class and copy Join(Of T) declaration to it:
Class string2
Public Shared Function Join(Of T)(ByVal separator As String, ByVal values As System.Collections.Generic.IEnumerable(Of T)) As String
Return "1"
End Function
End Class
Console.WriteLine(string2.Join(Of Char)("", ""))
It works
UPD2:
My compilation string, where you can see that I'm using Net4:
http://pastebin.com/TYgS3Ys3
Do you have a code element named String somewhere in your project?
Based on the answer you have added to this question (where you indicate that changing String to [String] appears to have solved the problem), I guessed that this may be the result of a naming collision.
I was able to duplicate the error you are seeing -- "Expression expected" -- by adding a module to my project called String and defining a (non-generic) Join method from within that module.
This may not be the specific scenario you find yourself in. But the fact that the code works for you with [String] is, to me, very compelling evidence of a simple namespace collision.
Based on the documentation for the "Expression expected" error, I'm guessing you haven't included the entire section of code where this error is appearing for you.
Do you have a lingering operator such as + or = somewhere?
(The VB.NET code you posted is indeed equivalent to the C# code above it and should compile no problem. This is why I suspect the real issue lies elsewhere.)
String.Join<T>(string, IEnumerable<T>) is useful with LINQ, for standard joins is better to use the String.Join(string, string()) overload.
In C#, "" as Char produces an empty Char (\0). Writing the same thing ("") in VB produces an empty string which is not the same as an empty char. In order to produce an empty character, you'll have to write New Char().
Your VB code therefore becomes:
Console.WriteLine(String.Join(Of Char)(New Char(), New Char()))
Edit
I just checked and it appears String.Join does not support the format you're specifying.
Instead, it goes as follows:
Join(separator As String, value As String()) As String
Your code should be as follows:
Console.WriteLine(String.Join("", New String() {""}))
String.Join(Of Char)(str1, str2) wasn't added til .net 4, it seems. That's why your custom class worked -- it had the declaration, but the String class in the framework you're actually using doesn't.
Check your settings and references to make sure you're targeting .net 4 all around -- cause that's the only thing that seems able at this point to stop the call from working.
Here the solution:
Console.WriteLine([String].Join(Of Char)("", ""))
Why this problem occurs only with generic method? I wish I know...

In VB6, how do I call a COM object requiring a pointer to an object?

I'm having trouble with a .NET Assembly that is com visible, and calling certain methods from VB6.
What I have found is that if the parameters are well defined types, (e.g. string), calls work fine. If they are higher level objects, it raises a runtime error '438' suggesting that the property or method is not present. I suspect that this is a question of having the correct signature on the call, but I can't see how to do this correctly.
I believe that I've done everything correct on the .NET side (ComVisible, public interfaces, etc. and even have it down to a simple enough case).
Looking at the output from the typelib viewer, I have the following:
dispinterface ISimple {
properties:
methods:
[id(0x60020000)]
void Add([in] ISimpleMember* member);
[id(0x60020001)]
ISimpleMember* Create();
};
OK. So I have 2 methods in my ISimple interface. One takes an ISimpleMember (Add), whilst the other, returns an ISimpleMember.
The corresponding code in VB looks like this:
Dim item As ISimpleMember
Dim simple As simple
Set item = New SimpleMember
item.S1 = "Hello"
item.S2 = "World"
Set simple = New simple
simple.Add (item) <---- This raised the run time error 438
Set item = simple.Create <---- This works fine, returning me an ISimpleMember
I've tried a couple of things:
1. Dim item as SimpleMember (makes no difference)
2. simple.Add(ObjPtr(item)) - Syntax error
3. simple.Add(ByRef item) - Syntax error
Basically, The run time error is the same as if I had
simple.AMethodThatIHaventWritten()
Also, If I browse References in the VB6 Environment, The Add method is well defined:
Sub Add(member As SimpleMember)
I've found the answer I believe. It was very simple:
When calling a SubRoutine, I shouldn't put the name in braces. the call should have been:
simple.add member
rather than
simple.add(member)
If I change it to a function (i.e. return a value rather than void) the braces are necessary
This seems to work
(Probably) The top 3 VB6 coding mistakes made by devs who now mainly code in C#, Javascript etc. Are:-
Placing ; at the end of lines. Its a syntax error very easily spotted and picked up the compiler.
Not placing Then on the other side of an If condition expression. Again its a syntax error.
Calling a method without retrieving a value and yet using ( ) to enclose the parameter list. With multiple parameters this is a syntax error and easily found. With only one parameter the use of ( ) is interpreted as an expression. Its the result of the ( ) expression which is passed as parameter. This causes problems when ByRef is expected by the callee.