开发者

How to access the first value from an object in C#3.0

开发者 https://www.devze.com 2022-12-27 20:13 出处:网络
How can I access the first value from object t = (object) wsf.LinEst(y.ToArray(), x.ToArray(), false, true);

How can I access the first value from

object t = (object) wsf.LinEst(y.ToArray(), x.ToArray(), false, true);

The output is

object[1..5, 1..2]}
    [1, 1]: 0.17134831460674155
    [1, 2]: 0.0
    [2, 1]: 0.019612696690686725
    [2, 2]: -2146826246
    [3, 1]: 0.95020429009193053
    [3, 2]: 0.82746057986828336
    [4, 1]: 76.328205128205056
    [4, 2]: 4.0
    [5, 1]: 52.261235955056179
    [5, 2]: 2.7387640449438开发者_Python百科226

I need to get only [1,1] 's value i.e. 0.17134831460674155

How to get that.

The linEst return an object only.

I am using C#3.0

Thanks

Thanks


Well, it looks like it's something you can enumerate which returns doubles - although it may not actually implement IEnumerable<double>. Try this (with a using System.Linq; directive at the top of your code):

IEnumerable t = (IEnumerable) wsf.LinEst(y.ToArray(), x.ToArray(), false, true);
double first = t.Cast<double>().First();

However, that won't work if actually it's returning something like an int[][]. Could you tell us more about what it's really returning? Yes, it's declared to just return object, but it's clearly returning something more than that. If you know it will always return an object[,] with 1-based indexes, you could cast to that:

object[,] t = (object[,]) wsf.LinEst(y.ToArray(), x.ToArray(), false, true);
double first = (double) t[1, 1];

If you don't know the lower bounds of the array, you can ask for them programmatically of course... but the LINQ way will probably be simpler.


You can force a cast to the appropriate type.

Instead of doing:

object t = (object) wsf.linEst(etc.)

try

int[,] t = (int[,]) wsf.linEst(etc.)

This is because the linEst function is not simply returning an instance of the Object class.

The output you've included in your question seems to imply that the debugger is able to determine the runtime type of your object t so try and use that information to do the proper cast.

Either that, or wsf.linEst isn't just returning an object and you are adding that (object) text which is forcing the return value to be cast to fit into the object t variable

0

精彩评论

暂无评论...
验证码 换一张
取 消