如何将Int32转换为对象[] [英] How to Convert Int32 to object[]

查看:88
本文介绍了如何将Int32转换为对象[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助:怎么做-

Pls help: how to do this-

Int32 UserID;

object[] parameters = new[] { UserID };


---


---

Error: Cannot implicitly convert type 'int[]' to 'object[]'

推荐答案

VS从创建数组时使用的变量类型(在本例中为userID)派生数组的类型,有两种解决方法:

VS is deriving the type of the array from the type of the variable used in its creation (in this case userID), there are 2 ways around this, either:

object[] parameters = new[] { (object)UserID };







or

object[] parameters = new object[] { UserID };



就个人而言,我更喜欢第二个选项.



Personally, i prefer the 2nd option.


为您提供一些建议:
private Int32 UserID;
private Int32 AdminID;
private List<Int32> ListOInt32s;
private List<object> ListOObjects;

private void MakeIDS(Int32 uID, Int32 aID)
{
    UserID = uID;
    AdminID = aID;

    // anonymous type
    // exists only in the scope of this method call
    var IDS = new {UserID, AdminID};

    // generic strongly typed
    ListOInt32s = new List<Int32> {UserID, AdminID};

    // generic typed as Object
    ListOObjects = new List<object> {UserID, AdminID};
}

private void button1_Click(object sender, EventArgs e)
{
    MakeIDS(4,5);
}


这可能会有所帮助,

It might be helpful,

static void Main(string[] args)
{
    Int32 one = 1, two = 2;
    object[] myArray = { one, two };
}


:)


这篇关于如何将Int32转换为对象[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆