Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am new to C# and I have a simple 2

Status
Not open for further replies.

GabeC

Programmer
Apr 17, 2001
245
US
I am new to C# and I have a simple need but I am unsure how to accomplish it.

I want to get the current day of the week. Ex. Monday, Tuesday, etc.

I use the following code to create a DateTime object but how do I set it to the current date and time?

Is this how I should go about getting the current date or is there a better way?
Code:
DateTime dateTime = new DateTime();
string currentDate = dateTime.ToString();

How do I properly use
Code:
DateTime.Now();
?


Thanks for the help,

Gabe
 
The Now property is a static one. That means that you can use it in the following way (and, in C#, only in this way):

DateTime today = DateTime.Now;

and, if you want

string currentDate = today.ToString();

There is a difference between methods, like ToString(), that are called with paranthesis, and properties that basically mean attributes that have get and/or set methods, and are called without paranthesis, like Now. And there is a difference between a static method or attribute or property, that can be called like Class.StaticMethod(), Class.StaticAttribute, Class.StaticProperty and the non-static equivalents, called like this:

Class object = new Class();
object.Attribute;
object.Method();
object.Property;

Well, maybe you knew some of that, and if you did I'm sorry about the repetition. These were the tricky things in your problem.
 
Thanks for the explanation alexboly.

The explanation helps a lot. I program in Java and VB but have just started tackling C#. I’m still catching on to when you need to create an object and when you can just reference an object and when to reference a method and when to reference a property. Your explanation has helped me make this distinction.

Can you explain to me why you don’t use NEW in this statement
Code:
DateTime today = DateTime.Now;

but you in this one.
Code:
SqlDataAdapter dataAdapter = new SqlDataAdapter(sSQL, sConn)

Thanks,

Gabe
 
You only need to call new when you are instantiating a class to an object. The Now property is static (meaning it's there even when you don't have an instance of a DateTime class), so you don't have use new on it.

You can create your own static methods. I have a class named RegistryUtil that I reuse in several solutions (I tweak it each time for the specific solution). I declare static properties for each of the registry values that application needs... things like the DB connect string.
Code:
public class RegistryUtil
{
  private const char _Delimit = '\x005c';  // Unicode hex for '\'
  private const string SYSTEMDB = "SOFTWARE\\ProdName\\0.1.0\\SystemDB";
  private const string SYSTEMDBSERVER = "SystemDBServer";
  private const string SYSTEMDBNAME = "SystemDBName";
  private const string SYSTEMDBUSER = "SystemDBUser";
  private const string SYSTEMDBPASSWORD = "SystemDBPassword";
  private const string SYSTEMDBEXTRA = "SystemDBExtra";

  private static void GetValue(string RegPath, string ValueName, string DefaultValue, out string RegValue) 
  {
    string[] RegString;
    string Result = string.Empty;

    RegString = RegPath.Split(_Delimit);

    RegistryKey[] rk = new RegistryKey[RegPath.Length + 1];
    rk[0] = Registry.LocalMachine;

    for(int i = 0;i < RegString.Length;i++)
    {
      rk[i + 1] = rk[i].OpenSubKey(RegString[i]);
      if (i  == RegString.Length - 1 )
      {
        Result = (string)rk[i + 1].GetValue(ValueName, DefaultValue); 
      }
    } 

    RegValue = Result;
  }

  ///		
  public static string SystemDBConnectString
  {
    get
    {
      string RegValue = string.Empty;
      string Connect = string.Empty;

      GetValue(SYSTEMDB, SYSTEMDBSERVER, &quot;&quot;, out RegValue);
      Connect += &quot;Server=&quot;;
      Connect += RegValue;
      Connect += Connect.Substring(Connect.Length - 1) != &quot;;&quot; ? &quot;;&quot; : string.Empty;
				
      GetValue(SYSTEMDB, SYSTEMDBNAME, &quot;&quot;, out RegValue);
      Connect += &quot;Database=&quot;;
      Connect += RegValue;
      Connect += Connect.Substring(Connect.Length - 1) != &quot;;&quot; ? &quot;;&quot; : string.Empty;

      GetValue(SYSTEMDB, SYSTEMDBUSER, &quot;&quot;, out RegValue);
      Connect += &quot;User ID=&quot;;
      Connect += RegValue;
      Connect += Connect.Substring(Connect.Length - 1) != &quot;;&quot; ? &quot;;&quot; : string.Empty;

      GetValue(SYSTEMDB, SYSTEMDBPASSWORD, &quot;&quot;, out RegValue);
      Connect += &quot;Password=&quot;;
      Connect += RegValue;
      Connect += Connect.Substring(Connect.Length - 1) != &quot;;&quot; ? &quot;;&quot; : string.Empty;

      GetValue(SYSTEMDB, SYSTEMDBEXTRA, &quot;&quot;, out RegValue);
      if (RegValue.Length > 0) 
      {
        Connect += RegValue;
        Connect += Connect.Substring(Connect.Length - 1) != &quot;;&quot; ? &quot;;&quot; : string.Empty;
      }

      return Connect;
    }
  }
}

That way, when I want to use it, I can do this:
Code:
  string DbConnect = RegistryUtil.SystemDBConnectString;
and it's really easy ... no need to create another variable to hold a RegistryUtil object.

Hope this helps.
Chip H.
 
Well, I think chiph explained it... If there is anything else that you don't understand, just shoot it. Sometimes it's very useful to explain such things to somebody.
 
That is a very good explanation. Thank you both.

I do have a few more questions though.

I am using Visual Studio.Net for my IDE. Is there a way with this tool and IntelliSense that I can tell what methods and properties are static? Is it any method or property that shows up in the list when typing something like the following?
Code:
DateTime.
I noticed the IntelliSense shows a different list with that line of code than if I type this:
Code:
DateTime dt = new DateTime();
dt.

Why do you use this line of code
Code:
private const char _Delimit = '\x005c';  // Unicode hex for '\'
instead of
Code:
private const char _Delimit = '\';

Why do you use
Code:
void
with the
Code:
out
to return a value in the
Code:
GetValue
function and use
Code:
string
in the
Code:
SystemDBConnectString
function? Don’t they basically do the same thing? I’m trying to understand why you use one technique in one instance and another technique in the second instance.

Thanks,

Gabe
 
>I am using Visual Studio.Net for my IDE. Is there a way >with this tool and IntelliSense that I can tell what >methods and properties are static? Is it any method or >property that shows up in the list when typing something >like the following?

>DateTime.

>I noticed the IntelliSense shows a different list with >that line of code than if I type this:

>DateTime dt = new DateTime();
>dt.

That is exactly the answer to your question. In C# you can access the static methods/properties/attributes only by using the name of the class. So when you write

DateTime.

you see the static methods/properties/attributes.
Of course, in your second example you see the methods/properties/attributes associated with an instance of the class, i.e. the non-static ones.
You have to understand that there is a logical difference between static and non-static components (the static ones were not introduced just for fun :) ).
Let's take the example for DateTime.Now. Suppose that it is not a static method and that you need in a program 1000 DateTime objects. Now, every time you do

DateTime varn = new DateTime();

you will have initialized in the memory a new object of type DateTime named varn that will contain an object of type DateTime named Now that represents the current date and time. You will have for each object the date and time when it was created.
On the contrary, by being a static property, when you access it, the class reads the system date&time and initializes the property that is sent to you. This happens in the get() part for the property.
Now, if Now would have been an attribute and not a property, its value had been with no sense. Why? Because a static attribute is initialized when the class is used for the first time, and you have to give a value for it. So by accessing directly the attribute, you would only see the value given by the developper of the class.
So, if we summarize:
1) static properties/fields/methods correspond and can be accessed only by a class;
2) non-static properties/fields/methods correspond and can be accessed only by instances of a class (i.e. objects);
3) there is a logical difference between static properties/fields/methods and their non-static correspondents;
4) there is a difference between static fields, static properties and static methods.

I would advice you to read the C# language reference from MSDN online in order to clarify these problems.

I will leave chiph to answer to your second question. It's his example :)
 
private const char _Delimit = '\x005c'; // Unicode hex for '\'

instead of

private const char _Delimit = '\';

I used the hexadecimal constant because it's possible for there to be several different kinds of backslashes in Unicode that all happen to look the same, but have different Unicode values. By doing it this way I can guarantee to get the correct backslash.

Why do you use void with the out to return a value in the GetValue function and use string in the SystemDBConnectString function? Don’t they basically do the same thing? I’m trying to understand why you use one technique in one instance and another technique in the second instance.

I inheirited that code from someone else, and they liked using out parameters more than having the function return a string. :) I could have rewritten it as:
Code:
  private static string GetValue(string RegPath, string ValueName, string DefaultValue)
Also, the SystemDBConnectString is a property, not a function or a method, and they must have return values.

The interesting question that you haven't asked, is why does the private method GetValue have to be static when it's not visible outside the class anyway?

The answer is that a static method can only call other static methods. It's possible for instance methods to call static methods (but sometimes dangerous - there can be interactions), but for a static method to call an instance method, an instance has to exist. Or take another case, where there are 15 instances of an object -- which one does it call?

And so static methods can only call other static methods.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top