Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:59:21+00:00 2026-06-14T21:59:21+00:00

In VBA I simply would use a variant-Type for this, but how can I

  • 0

In VBA I simply would use a variant-Type for this, but how can I reduce this somewhat repetitive code to get to a better switch case block?

switch(command){
case CMD1:
    RESPONSE1 rsp1;
    memcpy(&rsp1,pPkt,sizeof(rsp1));
    break;
case CMD2:
    RESPONSE2 rsp2;
    memcpy(&rsp2,pPkt,sizeof(rsp2));
    break;
case CMD3:
    RESPONSE3 rsp3;
    memcpy(&rsp3,pPkt,sizeof(rsp3));
    break;
case CMD4:
    RESPONSE4 rsp4;
    memcpy(&rsp4,pPkt,sizeof(rsp4));
case CMD5:
    RESPONSE5 rsp5;
    memcpy(&rsp5,pPkt,sizeof(rsp5));
default:
    break;
}

My goal would be to eliminate those 5 response variables and use only one – however, I need the 5 different types because of the memcpy.

I am aware, that there is something like cast, but I think this is more problematic, because these RESPONSE# are structs of different size – wouldn’t I have to reallocate memory for that?

Edit:

Thanks to mux, I found, that there is already a union for that in my code (adapting something from someone else)!

typedef union
{
  RESPONSE1 rsp1;
  RESPONSE2 rsp2;
  RESPONSE3 rsp3;
  RESPONSE4 rsp4;
  RESPONSE5 rsp5;
}
RESPONSE_UNION;

And this is what I am using now:

RESPONSE_UNION ru;

switch(command){
    case CMD1:
        memcpy(&ru.rsp1,pPkt,sizeof(ru.rsp1));
        break;
    case CMD2:
        memcpy(&ru.rsp2,pPkt,sizeof(ru.rsp2));
        break;
...

Really a great help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:59:22+00:00Added an answer on June 14, 2026 at 9:59 pm

    You could use a union and you might also want to define an enum for the response type:

    typedef enum { 
        RESPONSE_1,
        RESPONSE_2,
        RESPONSE_3,
        RESPONSE_4
    } ResponseType;
    
    typedef struct {    
        ResponseType type;
        union {
            RESPONSE1 rsp1;
            RESPONSE2 rsp2;
            RESPONSE3 rsp3;
            RESPONSE4 rsp4;
        };
    } Response;
    

    In your code you use it like this:

    Response r;
    switch (command) {
        case CMD1:
            r.type = RESPONSE_1;
            memcpy(&r.rsp1, pPkt, sizeof(r.resp1));
            break;
        case CMD2:
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

VBA (and I assume VB) has a Variant type which I believe takes up
I have a simple inputbox in my VBA code. I would like to set
I am trying to get a simple VBA code as part of a Macro
I simply want to fill-up cells in my spreadsheet from a VBA function. By
In VBA Help for the RefersTo Property, they give this example of listing all
I have a fairly simple spreadsheet application with some VBA code that performs simple
i have a formula that looks like this in VBA: ActiveCell.FormulaR1C1 = =LOOKUP(ETG_C,RC[-10],RC[-8]) what
I am attempting what I thought would be a fairly simple vba statement to
This is a continuation of the question excel different SUM.IF array function , But
I'm relatively experienced with Object oriented programming, but this is my first time ever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.