简体   繁体   中英

how to read the values from the object variable in C#

I need to read the values from a variable which is of type object for e..g.., i have a variable called result as:

object result= h[key];

h[key] is a hash table which returns 5 values to result variable. How do I read the 1st value to my local variable of type string in C# script in SSIS package?

I can see only GetType , Equals , ToString() options for result variable.

Any help please?

there is the sample:

there is a sample; public void SQLLoop() { string bp,ap,ep,s,vs; LocationInfo info = new LocationInfo(); string connection = "Server=Sname;Database=Dname;Integrated Security=SSPI"; SqlConnection conn = new SqlConnection(connection);

    conn.Open();

   SqlCommand sqlcmd = new SqlCommand("SELECT Bp,Ap,EP,SL,VSr from Table1", conn);
   SqlDataReader rs=sqlcmd.ExecuteReader();


        while (rs.Read())
        {
            bp = rs.GetValue(0).ToString();
            ap = rs.GetValue(1).ToString();
            ep = rs.GetValue(2).ToString();
            s = rs.GetValue(3).ToString();
            vs = rs.GetValue(4).ToString();
            info.loadLocationInfo(ap, bp, ep, s, vs);
            h.Add(s, info);

        }
        conn.Close();

    }

public class LocationInfo
{
    String A;
    String B;
    String E;
    String S;
    String V;
    int id;

    public LocationInfo()
    {
    }

    public void loadLocationInfo(String a,String b,String e,String s,String v)
    {
        A =a ;
        B  =b ;
        E=e ;
       S =s;
        V = v;
    }


}

now

public void fun1() { var result = (object )h[subject]; ///read values from the hash table

}

You have to cast result to the class or interface you are expecting.

var result = (IExpectedObject)h[key];

Supposing you know the type of the result you can cast the object var result = (MyType) h[key]

EDIT: use this inside your function to get first value var result = ((LocationInfo) h[key]).A

Update: Ok well you have the LocationInfo class so do something like this:

LocationInfo result = (LocationInfo)h[key]; 

Then just make some properties on the LocationInfo class to retrieve your strings.

Your probably need to cast the object that is in the hashtable. So something like:

result = (Type)h[key];

Here is an example of how it would work:

Person1 = new Person("David", "Burris");
Person2 = new Person("Johnny", "Carrol");
Person3 = new Person("Ji", "Jihuang");

//The Add method takes Key as the first parameter and Value as the second parameter.

try
{
    MyTable.Add(Person1.Lname, Person1);
    MyTable.Add(Person2.Lname, Person2);
    MyTable.Add(Person3.Lname, Person3);

}
catch (ArgumentException ae)
{
    MessageBox.Show("Duplicate Key");
    MessageBox.Show(ae.Message);
}

So when you want to retrieve from the table you would do:

Person result = (Person)h[key];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM