-
May 30th, 2013, 09:18 AM
#1
Convert VB function to C++ / C code
Hello,
I want to convert the following VB function to c/c++ function. I don't know how to deal with VB's Variant variable in C/C++. For some reason, I need function to return a char*. I tried to use template but I don't know how to convert a template variable to char*. any idea?
Private Function PerformOperation _
(ByVal S1 As String, ByVal Operator As String, ByVal S2 As String) As String
Dim result As Variant
Dim V1 As Variant, V2 As Variant
If Asc(S2) >= 48 And Asc(S2) <= 57 And Asc(S1) >= 48 And Asc(S1) <= 57 Then
V1 = CDbl(S1)
V2 = CDbl(S2)
Else
V1 = S1
V2 = S2
End If
Select Case Operator
Case Is = "&&"
result = V1 And V2
Case Is = "||"
result = V1 Or V2
Case Is = ">"
result = V1 > V2
.......
Case Is = "=="
result = V1 = V2
Case Is = "!="
result = Not (V1 = V2)
Case Is = "-"
result = V1 - V2
Case Is = "/"
result = V1 / V2
End Select
PerformOperation = CStr(CDbl(result))
End Function
Here is what I did
template <class T>
char* PerformOperation (T V1 , char *Operator , T V2 )
{
T result;
if (strcmp(Operator,"&&")==0 )
result = V1 && V2;
else if (strcmp(Operator, "||")==0 )
result = V1 || V2 ;
else if (strcmp(Operator, ">" )==0 )
result = V1 > V2 ;
else if (strcmp(Operator, "<" )==0 )
result = V1 < V2 ;
..................
else if (strcmp(Operator, "==" )==0 )
result = (V1 == V2) ;
else if (strcmp(Operator, "!=" )==0 )
result = (V1 != V2) ;
else if (strcmp(Operator, "-" )==0 )
result = V1 - V2 ;
else if (strcmp(Operator, "/" )==0 )
result = V1 / V2 ;
return result; //error here
}
I got error "Cannot convert 'bool' to 'char *' on return result;
What should I do?
Thanks!
-
May 30th, 2013, 10:09 AM
#2
Re: Convert VB function to C++ / C code
Originally Posted by jwspring
Hello,
I want to convert the following VB function to c/c++ function. I don't know how to deal with VB's Variant variable in C/C++.
There is no such thing as "Variant" in C++. Also, please use code tags when posting code.
Forget about the translation line-by-line, and forget about VB right now.
What exactly do you want to do? Whatever that is, write it in C++. If you did that, there would be no "Variant" coming into play.
For some reason, I need function to return a char*
And what is this "char *" supposed to denote?
The reason for your error is exactly as stated -- a bool is not a char*. So again, tell us what you want to do, not how the old VB code did things.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 30th, 2013 at 10:13 AM.
-
May 30th, 2013, 10:19 AM
#3
Re: Convert VB function to C++ / C code
Originally Posted by jwspring
I tried to use template but I don't know how to convert a template variable to char*. any idea?
Code:
#include <string>
template <class T>
T PerformOperation (T V1 , const std::string& Operator, T V2 )
{
T result = T();
if (Operator == "&&")
result = V1 && V2;
else
if (Operator == "||")
result = V1 || V2 ;
//...
return result;
}
Note the usage of std::string, not char*. Second, note that result is assigned a default value at the beginning.
Even the approach above can be improved on, especially since all you're doing is a binary operation between two operands. This can be classed or grouped in a way where you're not writing endless if() statements.
Regards,
Paul McKenzie
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|