Reputation:
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)
Upvotes: 1
Views: 5780
Reputation: 414
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; }
}
Upvotes: 0
Reputation: 109
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);
Upvotes: 0
Reputation: 14389
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();
Upvotes: 2
Reputation: 9804
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.
Upvotes: 0