开发者

Extension Method not compiling (No defintion for type 'string')

I'm trying to convert bytes into KB/MB/GB using the code below, however, I can't seem to get it working. The value of quota, is 60000000000.

    public static double BytesToKilobytes(this Int32 bytes)
    {
        return bytes / 1000d;
    }

    public static double BytesToMegabytes(this Int32 bytes)
    {
        return bytes / 1000d / 1000d;
    }

    public static double BytesToGigabytes(this Int32 bytes)
    {
        return bytes / 1000d / 1000d / 1000d;
    }

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
            return;

        XDocument xDocument = XDocument.Parse(e.Result);

        listBox1.ItemsSource = from query in xDocument.Descendants("service")
                               select new Service
                    开发者_高级运维           {
                                   type = query.Attribute("type").Value,
                                   id = query.Element("id").Value,
                                   plan = query.Element("username").Value,
                                   quota = query.Element("quota").Value.BytesToGigabytes,                                   };
    }

The error the above code produces is:

"'string' does not contain a definition for 'BytesToGigabytes' and no extension method 'BytesToGigabytes' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)"


As the quota is a string, you have to parse it into a number first:

quota = Decimal.Parse(query.Element("quota").Value).BytesToGigabytes()

As the number is too large to fit in a 32 bit integer, you have to use a Decimal:

public static Decimal BytesToGigabytes(this Decimal bytes) {
  return bytes / 1000m / 1000m / 1000m;
}

It would also be possible to use an Int64, but then the method would truncate the result, returning for example 3 GB instead of 3.9 GB.


This is because Value is a string, while the extension methods are declared for Int32. You will need to convert the Value to an Int32 prior to invoking the extension method.

Example:

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    Func<string, Int64> convertToInt64 = s =>
    {
       Int64 result;
            // replace 0 with whatever default you want
       return Int64.TryParse(s, out result) ? result : 0;
    };
    if (e.Error != null) return; 
    XDocument xDocument = XDocument.Parse(e.Result); 
    listBox1.ItemsSource = from query in xDocument.Descendants("service") 
                           select new Service 
                           { 
                               type = query.Attribute("type").Value, 
                               id = query.Element("id").Value, 
                               plan = query.Element("username").Value, 
                               quota = convertToInt64(query.Element("quota").Value)
                                           .BytesToKilobytes()
                           };
}

This also means that the extension methods should be declared for Int64:

public static double BytesToKilobytes(this Int64 bytes) 


Without knowing whats in your event args, the error is fairly straightforward.

There is no extension for a BytesToGigabytes for a type of string.

So query.Element("quota") is returning a string. If you parse it (int.Parse() or int.TryParse() then you should have more luck.


A couple of errors though .. u should divide by 1024 instead ... and convert value to Int .. see below.

 public static double BytesToKilobytes(this Int32 bytes)
    {
        return bytes / 1024d;
    }

    public static double BytesToMegabytes(this Int32 bytes)
    {
        return bytes / 1024d / 1024d;
    }

    public static double BytesToGigabytes(this Int32 bytes)
    {
        return bytes / 1024d / 1024d / 1024d;
    }

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
            return;

        XDocument xDocument = XDocument.Parse(e.Result);

        listBox1.ItemsSource = from query in xDocument.Descendants("service")
                               select new Service
                               {
                                   type = query.Attribute("type").Value,
                                   id = query.Element("id").Value,
                                   plan = query.Element("username").Value,
                                   quota = Convert.ToInt32(query.Element("quota").Value).BytesToGigabytes(),                                   };
    }

Hope this helps

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜