1

Here is my code:

public static string HDDData()
{
    string Timestampy = null, TotalSpace = null, SpaceLeft = null, 
    PercentageLeft = null;
    TotalSpace = GetTotalFreeSpaceGB();
    SpaceLeft = GetTotalSpaceGB();
    PercentageLeft = GetTotalFreeSpacePercentage();
    Timestampy = Timestamp();
    var HDD = new HDDFormat
    {
        TotalSpace = TotalSpace,
        SpaceLeft = SpaceLeft,
        PercentageLeft = PercentageLeft,
        Timestamp = Timestampy
    };
    return HDD;
}

This is the HDDFormat:

public class HDDFormat
{
    public string TotalSpace { get; set; }
    public string SpaceLeft { get; set; }
    public string PercentageLeft { get; set; }
    public string Timestamp { get; set; }
}

The data you get are correct strings. (.ToString() doesn't work either)

5
  • 1
    You getting class type information. You need to overwrite ToString method in HDDFormat class. How to: Override the ToString Method Commented Nov 26, 2018 at 13:06
  • what exactly isint converting to string? When you convert to string your custom designed objects you need to override the object ToString method like this public override string ToString() and then define how exactly it should be converted to string. Commented Nov 26, 2018 at 13:07
  • Should your method return a HDDFormat or string? How should that string be formatted? Commented Nov 26, 2018 at 13:07
  • 1
    Dod you mean public static HDDFormat HDDData()? Commented Nov 26, 2018 at 13:07
  • I would like to return it as a HDDFormat, but I still need to get data back from it Commented Nov 26, 2018 at 13:11

4 Answers 4

2

you must change the return type of your method. Should not be string but of Type HDDFormat

Otherwise you should make and override of ToString like, e.g:

public override string ToString()
{
   return $"{TotalSpace },{SpaceLeft },{PercentageLeft },{Timestamp }";
}

And use like:

...
 return HDD.ToString();
Sign up to request clarification or add additional context in comments.

Comments

0

just create a method that Return a String

for example:

    using System;

    public class Program
   {
    public static void Main()
    {


        HDDFormat disk = new HDDFormat();

        disk.TotalSpace = "500gb";
        disk.SpaceLeft = "120gb";
        disk.PercentageLeft = "60%";
        disk.Timestamp = "2018-10-11 ";

        Console.WriteLine(disk.HDDformat());

       }
     }

     public class HDDFormat
     {
      public string TotalSpace { get; set; }
      public string SpaceLeft { get; set; }
      public string PercentageLeft { get; set; }
      public string Timestamp { get; set; }

     public string HDDformat(){
      return TotalSpace + SpaceLeft + PercentageLeft + Timestamp; }
      }

1 Comment

OMG, this hurts my eyes. Please, please, PLEASE overwrite ToString() for HDDFormat.
0

Strings are primarily about outputting Stuff towards the user. So it depends entirely on the (G)UI. The code looks different for WinForms, WPF/UWP, Console, ASP.Net.

There is a ToString() Method. It is defined in Object and thus inherented by any class. The default implementation will only print the classes name (YourNamespace.HDDFormat) but you are free to overwrite it (see Reniuz link). But primarily this one is for string dumps, wich are usefull primarily for debugging/minimalistic user interfaces.

If this is about transmitting it via XML, then there are proper Attributes and infarces for that. See Serialisation.

Comments

0

your Method HDDData is supposed to return a type of string. But you are returning the Class HDDFormat.

either you change the return type to HDDFormat like this:

public static HDDFormat HDDData(){
    ...
}

or you make your class inherit the String class like this:

public class HDDFormat : String
{
    public string TotalSpace { get; set; }
    public string SpaceLeft { get; set; }
    public string PercentageLeft { get; set; }
    public string Timestamp { get; set; }
}

but you'll have to overwrite some functions when doing that. Last option would be to return just the specific string you want to access.

return HDD.PercentageLeft;

I would like to return it as a HDDFormat, but I still need to get data back from it

Then you should use the first option. Afterwards you can access its properties.

HDDFormat myFormat = HDDData();
MessageBox.Show(myFormat.PercentageLeft);

2 Comments

Severity Code Description Project File Line Suppression State Error CS0509 'Elastic.HDDFormat': cannot derive from sealed type 'string'
When did you get that Error? Using which of my suggestions?

Your Answer

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