2011-01-11 3 views
2

아래 코드를 사용하여 바이트를 KB/MB/GB로 변환하려고하지만 작동하지 않는 것 같습니다. 용량의 값은, 상기 코드는 생성 된 오류 60000000000.확장 메서드가 컴파일되지 않음 ('string'형식에 대한 정의가 없음)

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,         }; 
    } 

이다

는 " '문자열'BytesToGigabytes '의 정의없이 확장 메소드를 포함하지 않는'BytesToGigabytes '는 제 접수 "문자열"형식의 인수를 찾을 수 있습니다 (지시문이나 어셈블리 참조가 누락 되었습니까?) "

+1

아마도 문제는 아니지만 할당량 값이 60 * 10^9이면 Int32에 맞지 않습니다. Int64 (일명 long)를 사용해야합니다. – Niki

답변

5

희망이 도움 n은 32 비트 정수, 당신은 진수 사용해야합니다 :

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

는 또한 Int64를 사용하는 것이 가능할 것이다, 그러나 그 방법은 예 3 GB 대신 3.9 GB에 대한 반환 결과를 잘라 것입니다.

+0

안녕하세요 Guffa, 나는 당신의 해결책을 시도했지만 다음과 같은 오류를 던졌습니다 : " 암시 적으로 'decimal'유형을 'string' "으로 변환 할 수 없습니다." – cvandal

+0

@cvandal :'quota'는 문자열 멤버입니까? 그런 다음 값을 지정하기 전에 값을 문자열로 형식화해야합니다. – Guffa

+0

아, 그래, 내가 그리워 :) 완벽하게 작동합니다! – cvandal

2

Value은 문자열이고 확장 메서드는 Int32으로 선언 되었기 때문입니다. 확장 방법을 호출하기 전에 ValueInt32으로 변환해야합니다.

예 :

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() 
          }; 
} 

이 또한 확장 방법 Int64 선언되어야 함을 의미한다

public static double BytesToKilobytes(this Int64 bytes) 
+3

그건 불가능합니다. 값이 너무 커서 Int32에 들어 가지 않습니다. – Guffa

+0

@Guffa : 사실, 'Int64' 여야합니다. –

1

이벤트 인수에 오류가 매우 간단 뭐죠 모른 채.

문자열 형식에 대해 BytesToGigabytes 확장명이 없습니다.

그래서 query.Element ("quota")는 문자열을 반환합니다. 당신이 그것을 구문 분석하는 경우 (.. 아래 참조 int로 값을 int.Parse() 또는 int.TryParse() 그런 다음, 그래도.

0

오류의 몇 가지를 더 운이해야 .. 유 대신 1024으로 분할한다 ... 그리고 변환합니다.

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(),         }; 
    } 
수 내가 맞게 너무 커서

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

을 :

먼저 숫자로 구문 분석해야 할당량이 문자열이기 때문에이

+1

킬로바이트는 1000 바이트입니다. 당신이 말하는 것은 kibibyte입니다. OP가 실제로 킬로 바이트 대신 kibibytes를 원할 수도 있지만, 확실하지는 않습니다. 단위 킬로바이트는 일반적으로 두 장치에 사용됩니다. – Guffa

+0

@ Guffa ... 나는 ...에 대한 나의 사랑을 확신한다. 나는 결코 그것을 말하지 않을 때까지 kibibyte를 알고 있었다. 결코 끝내지 않는 학습은 끝났다. ... – scartag

관련 문제