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

  • Home
  • SEARCH
  • 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 8800605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:38:00+00:00 2026-06-14T00:38:00+00:00

I’m trying to get make method in which I can assign values to multiple

  • 0

I’m trying to get make method in which I can assign values to multiple variables. I don’t know how many variables I will have, but I know their type. The variables can all be of different types though.

I have googled quite a bit about this, and am fairly sure that this can’t be done with managed code. (Correct me if I’m wrong though)

I don’t know much (read: anything) about unsafe code. Can it be done that way? Maybe pass in an array of pointers to the variables I want to initialise and do it that way?

I am basically looking for a way to pass an arbitrary number of mixed type variables to a method and assign their values inside the method… Is it pie in the sky?

EDIT 1:

Here is some code which I hope illustrates what I would like to achieve:

private void SomeMethod()
{
    string a = string.empty;
    int b = 0;
    double c = 0;

    object[] testObject = new object[] { b, c };

    SetVariables(ref testObject);
}

public static void SetVariables(ref object[] Variables)
{
    for (int i = 0; i < Variables.Length; i++)
        Variables[i] = // The value the variable needs to have
}

After SetVariables has executed, a, b and c would be say:

a = "Some text"
b = 123
c = 1564.653

I am looking for a way to access the variables passed in as the parameter to SetVariables and modify their value. I guess that would mean accessing their storage location in memory, hence my thinking that I might need pointers?

EDIT 2:

My question here should give a better indication of the context in which I am trying to do this.

I will only be dealing with basic types: string, int, double and bool for the variables I want to assign to.

The data I am assigning from is in text format and has a variable number of fields. Each field should map to one of the input variable in the order in which the variables are passed in. If the order does not match there will be a misasignment at best or a type error at worst, but it is up to the user to make sure that the variables match the data.

  • 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-14T00:38:01+00:00Added an answer on June 14, 2026 at 12:38 am

    What you are trying to do doesn’t work well in C#. It is really a language thing, not a “managed vs un-managed” thing.

    It usually doesn’t come up that much, because in your sample code, you do know that you have 2 variables. You had to type them to enter them into the array.

    If SetVariables() is going to be called from a lot of places in the code with different numbers of parameters, you could make a bunch of overloads of the method (with 1 parameter, 2 parameters, 3 parameters, etc).

    But really to be honest, typically when you run into this case it is because you are doing something in an un-wise way.

    Thinking more about it, how would the line:

    Variables[i] = // The value the variable needs to have
    

    would be expected to work in this scenario. How does it know what to set an arbitrary variable to, since it can be of any type? Also, what stops it from messing up the variable assignments if I change the call from passing in variable b, c to reversing them c, b?

    Logically, it starts to fall apart.

    I guess what I’m trying to say is that C# doesn’t support that very well, but it typically doesn’t need to, because it rarely makes sense.


    Trying to think of a working solution anyway; I would just have SetVariables just return the values, but not take in the parameters. It should be the other function’s job to assign its variables. If you need to know the types, then just pass the types:

    public object[] GetValues(params Type types)
    {
        var result = new object[types.length];
        for (int i = 0; i < types.length; i++)
        {
            if(types[i] == typeof(string))
                result[i] = "foo";
            if(types[i] == typeof(int))
                result[i] = -1;
        }
        return result;
    }
    
    public void DoStuff()
    {
        var data = GetValues(typeof(string), typeof(int), typeof(string));
        string foo1 = (string)data[0];
        int someNumber = (int)data[1];
        string foo2 = (string)data[2];
    }
    

    Its ugly, but it works…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

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.