Class with same code and send data [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have this C# class to send the data from DriveInfo Class to my window form:
using System;
public class FileSystemInfo
{
public string CheckTotalFreeSpace()
{
System.IO.DriveInfo DInfo = new System.IO.DriveInfo(#"C:\");
return DInfo.TotalFreeSpace.ToString();
}
public string CheckVolumeLabel()
{
System.IO.DriveInfo DInfo = new System.IO.DriveInfo(#"C:\");
return DInfo.VolumeLabel.ToString();
}
}
I want to send huge data from one class (see my example) into my form class (maybe labels or ListBox Control), by using a a good way to solve this issue. Also I don't want to put this line of code into separate c# class method:
System.IO.DriveInfo DInfo
The basic issue for me is: deal and show many info about my computer so I need to put all this info into one structure or something else.

You may use Lazy class:
using System.IO;
public class FileSystemInfo
{
private readonly Lazy<DriveInfo> dInfo =
new Lazy<DriveInfo>(() => new DriveInfo(#"C:\"));
public string CheckTotalFreeSpace()
{
return dInfo.Value.TotalFreeSpace.ToString();
}
public string CheckVolumeLabel()
{
return dInfo.Value.VolumeLabel.ToString();
}
}

Related

Nested list getter and setter c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this nested list :
public List<List<string>> pcList = new List<List<string>>();
How can I create getters and setters for it?
I have tried everything out there on the internet and nothing seems to work
Thx!
EDIT:
So, In class Pc I have this code:
class PC
{
public List<List<string>> pcList = new List<List<string>>();
public List<string> subList = new List<string>();
}
so the pcList is the "parent" list and the sublist is actually the place where I add each pc with its info.
I have a method where I populate the lists. Then I want to use an object of this class in another class called X, let's say.
I have tried to simply access the lists by using object.ListName but it doesn't work.
You can use auto property - just add {get;set;} (note that usually properties are upper case)
public List<List<string>> PcList {get;set;}
public List<List<string>> pcList { get; set; } = new List<List<string>>();
This is a variable.
If you want a property declare it like:
public List<List<string>> pcList {get;set;}

How can I get current time by using a static method? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I couldn't get current time by using static method, How can I imporve my method to get current time?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Console.WriteLine(timeNow.TimeNow);
}
}
public class timeNow
{
public static string TimeNow
{
get
{
return DateTime.Now.ToString("yyyy/MM/dd HH:MM:ss");
}
}
}
I motified code for easier to test, What I got is wrong current time, For example, right now is 11/29/2018 09:52 in my time zone. What I got from this code is 2018/11/29 09:11:25.
Try this
return DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
Issue was with the "HH:MM:ss". It was "MM" which is month. Refer Link for details on format strings

Get some unique name per each run of the console application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Not sure how to ask this. I am writing a CONSOLE app in C#. With each run I plan to create an output txt file. However I need some expression that will give an unique name with each run ( and also this name must remain the same through out the run ) I can use YYYYMMDDSS but this can change with each call. So you see what I mean. It should be something like Session ID ( I don't know what method/function will give me this ). Help please ? ( I am new to C# )
If you want the identifier to change on each execution, but be consistent for the life of the program, you probably want to generate it once and store the result.
One way is to store it in a readonly static property:
public static class RuntimeIdentifier
{
public static string Value { get; } = DateTime.Now.ToString("yyyyMMddhhmmss");
// Notice this is NOT the same as:
//
// public static string Value { get { return DateTime.Now.ToString("yyyyMMddhhmmss"); } }
//
// which will return a NEW value each time you access it
}
Another way would be to use Lazy<T>, which computes a value the first time you access it, and returns the same value each time after that.
public static class RuntimeIdentifier
{
public static Lazy<string> _identifier = new Lazy<string>(() => DateTime.Now.ToString("yyyyMMddhhmmss"));
public static string GetValue()
{
return _identifier.Value;
}
}
If it's important that the value be generated immediately when the app is started, you can generate and save it explicitly:
class Program
{
public static void Main()
{
RuntimeIdentifier.Value = DateTime.Now.ToString("yyyyMMddhhmmss");
// ...
}
}
public static class RuntimeIdentifier
{
public static string Value { get; set; }
}
Your scenario is not totally clear but if you are just trying to create a txt file with unique name each time you run your application than you should use Path.GetRandomFileName() for that matter. Or just look how it's implemented and adapt it to your needs.

How to check how many instances created for a type in c#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I check that number of instances created for a particular type and how much memory each instance occupied. Please give an example.
As suggested in the comment i will add an example
public class Myclass
{
private static long Count;
public Myclass()
{
Interlocked.Increment(ref Count);
}
}
This will only work for your own class, In this way you can't find out the number of instances of System.String for example.
To find out the size of a class, You should use this using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public class MyClass
{
public int MyField;
public int MyField;
}
And:
int sizeInBytes = Marshal.SizeOf(typeof(MyClass)); //return 8
Also you have here a list with size of int, byte, etc.

Object reference cannot fix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Error CS0120 An object reference is required for the non-static field,
method, or property 'Blip.Sprite' HeliBoatMissions
namespace heliBoatMissions
{ }
public class Radar
{
void Marker()
{
new Blip(new Vector3(-699.4645f, -1448.289f, 5.000523f),
Blip.Sprite = BlipSprite.Helicopter);
}
}
I Think this is your issue
namespace heliBoatMissions
{ }
Change it to this
namespace heliBoatMissions
{
UPDATE
Also I noticed that you do not assign this
new Blip(new Vector3(-699.4645f, -1448.289f, 5.000523f),
to a variable. Nor do you end it with a semicolon.
I think something like this might be what you're after?
namespace heliBoatMissions
{
public class Radar
{
public object BlipSprite { get; private set; }
void Marker()
{
var v = new Blip(new Vector3(-699.4645f, -1448.289f, 5.000523f));
v.Sprite = BlipSprite.Helicopter;
}
}
}

Categories

Resources