How can I get current time by using a static method? [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 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

Related

Correct Description of "class" [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 5 years ago.
Improve this question
public class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.WriteLine(test.GetType()); // -> it´s Test, of course
int a = 5;
Console.ReadLine();
}
}
public class Test
{
public string Name;
}
In the above case
the Type of "test" is Test, right?
And in this case the Type of "a" is int, named primitive data type,
public is an access modifier
So my Question now is, what is class then? A keyword? A primitive Datatype? Neither, Nor?
Please, don't explain my, what a class is doing or what's the difference between class or object, I know a class is like a construction plan.
Class is a keyword that signifies to the C# compiler that a class is being declared. See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

Class with same code and send data [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 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();
}
}

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

How can I to check a property will be renewed or its value will be changed [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 8 years ago.
Improve this question
How can I to check a property will be renewed or its value will be changed.
public MyClass A
{
get{}
set
{
// find if programmer set A=new MyClass();
}
}
It's not all clear what you want exactly, maybe something like this:
MyClass _a;
public MyClass A
{
get { return _a; }
set
{
// find if programmer set A=new MyClass();
if (value != _a)
{
// different value
}
}
}
For more complex scenario follow the suggestion of Raphaël Althaus and take a look at INotifyPropertyChanged.

Categories

Resources