Convert VB function to C++ / C code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Convert VB function to C++ / C code

  1. #1
    Join Date
    Jun 2006
    Posts
    40

    Question 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!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Convert VB function to C++ / C code

    Quote Originally Posted by jwspring View Post
    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.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Convert VB function to C++ / C code

    Quote Originally Posted by jwspring View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center