1

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

}

1
  • Change (object) to (LocationInfo), then just make some properties to get the values. Commented Apr 28, 2011 at 14:48

3 Answers 3

1

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

the type is excepting is also a object type and the value returned by hash table is also a object now how can i read the value from these object. for e..g., the return value of hash is "Stack" "overflow" two values then how can i read those values into my local variables
if you return "Stack" "overflow" than you can do var result = ((List<Object>)h[key]).First() or something similar with IEnumerable
i added the sample what i want to achieve basically
1

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];

3 Comments

the type is excepting is also a object type and the value returned by hash table is also a object now how can i read the value from these object. for e..g., the return value of hash is "Stack" "overflow" two values then how can i read those values into my local variables
Do you have more of the code we can look at? Like how you are adding to the hash table? I need to see what you are doing when you add to this hash table.
i added the sample what i want to achieve basically
1

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

var result = (IExpectedObject)h[key];

5 Comments

the type or namespace could not found
IExcpectedObject was just an example. You have to put in the ACTUAL type in your code that you expect result to be.
the type is excepting is also a object type and the value returned by hash table is also a object now how can i read the value from these object. for e..g., the return value of hash is "Stack" "overflow" two values then how can i read those values into my local variables
Whatever the type that INITIALLY goes into the hash will be the type you want to cast to on the way out. If it's not strongly typed and you can just throw anything in there, you might be in trouble. You'll probably have to resolve the type through reflection which will be messy. Can you strongly type the hashtable?
i added the sample what i want to achieve basically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.