user2065612
user2065612

Reputation: 471

Why am I getting "Cannot convert error"?

I have this code in a new class:

Object[] temp_arr = new Object[5];
                temp_arr[0] = csFiles;
                temp_arr[1] = mainUrl;
                temp_arr[2] = levels;
                temp_arr[3] = currentCrawlingSite;
                temp_arr[4] = sitesToCrawl;

The variables csFiles and currentCrawlingSite are both List<string>.

Then in the bottom I did:

public class WebCrawlerProgressEventHandler : EventArgs
{
    public List<string> csFiles { get; set; }
    public string mainUrl { get; set; }
    public int levels { get; set; }
    public List<string> currentCrawlingSite { get; set; }
}

protected void OnProgressEvent( Object[] some_params)
{
    if (ProgressEvent != null)
        ProgressEvent(this,
                      new WebCrawlerProgressEventHandler()
                      {
                          csFiles = some_params[0],
                          mainUrl = some_params[1],
                          levels = some_params[2],
                          currentCrawlingSite = some_params[3]
                      });
}

Now I'm getting an error on all the lines inside the new WebCrawlerProgressEventHandler() the same error for each line:

Error 2 Cannot implicitly convert type 'object' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

But if I remove the line: public List<string> currentCrawlingSite { get; set; } and the line: currentCrawlingSite = some_params[3] there are no errors.

I don't get it. csFiles and currentCrawlingSite are both List<string> type variables. Why, when I add the currentCrawlingSite, am I getting the error/s?

Upvotes: 0

Views: 287

Answers (5)

CloudyMarble
CloudyMarble

Reputation: 37566

Assuming the data you are assigning is a List<string> you need to cast it to this type:

new WebCrawlerProgressEventHandler()
{
    csFiles = (List<string>)some_params[0],
    mainUrl = some_params[1],
    levels = some_params[2],
    currentCrawlingSite = (List<string>)some_params[3]
});

Upvotes: 0

sloth
sloth

Reputation: 101052

Because the compiler does not and can not know that the first and forth element of the Object-array happens to be List<string>.

You have to cast them accordingly:

csFiles = (List<String>)some_params[0]

etc. or better, use an appropriate type to pass your data around instead of a simple Object-array if possible.

Upvotes: 2

Dmitrii Dovgopolyi
Dmitrii Dovgopolyi

Reputation: 6301

It is strong typing protection. You should explicitly cast an object to List<string> to ensure that you don't just mix up different types at the compile time

csFiles = (List<string>)some_params[0]

You don't have an error on mainUrl = some_params[1] because every object have ToString() method and the default behavior is to call this method for implicit type convertion.

J. Richter writes in his book:

For example, C# doesn’t require any special syntax to cast an object to any of its base types, because casts to base types are considered safe implicit conversions. However, C# does require the developer to explicitly cast an object to any of its derived types since such a cast could fail at runtime.

Upvotes: 0

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98750

currentCrawlingSite is an instance of List<string>, some_params is an instance of object array.

You can't implicitly convert these two types. You have to use explicit conversation. You can use them like;

currentCrawlingSite = (List<string>)some_params[3];

is equal also

currentCrawlingSite = some_params[3] as List<string>;

You can read much more details from Casting and Type Conversions (C# Programming Guide)

Upvotes: 0

dotNET
dotNET

Reputation: 35400

The point here is that OnProgressEvent is sending you an Object array, so you're trying to assign an Object to List<string> type variable in that "culprit" line, thereby implicit invoking a cast operation. You should really do an explicit cast there to solve your problem.

The new and preferred syntax in C# is:

csFiles = some_params[0] as List<String>;

Upvotes: 1

Related Questions