|
hy everyon!
i do have a little problem. i do have a function which has a parameter input as string. the input could be a number (double) or a date (datetime).
in this function i have to check, which input it contains. because depending on the input i have to do different things.
so i have to realize an if-then statement like
if (isdateformat)
{
}
if (isdoubleformat)
{
}
when using the convert function then the result is of this type (but i guess, if it is a double, then it throws an exception). when casting, then it is always of this type (always true).
could someone tell me please how to check the input of the string, if it is a date or a double?
thanks.
stephan.
|
|
|
|
|
stephan_007,
You can use RegEx or DateTime.TryParse / Double.TryParse.
Regards,
Gareth.
|
|
|
|
|
what does the statement look like?
because
DateTime dt;
datetime.tryparse(input, dt);
returns an error, it does not like the dt statement
|
|
|
|
|
stephan_007 wrote: returns an error, it does not like the dt statement
Funnily enough, compilers don't have emotions. It neither "likes" nor "dislikes" an argument! Do you mean it throws an exception? do you think this exception might be usful to us in trying to answer your question?
Have you tried the documentation for DateTime.TryParse? If you did you'd see the second parameter is defined as an out parameter.
<br />
DateTime dt;<br />
datetime.tryparse(input, out dt);
|
|
|
|
|
ups stupid me
yes you are right, now it works 
|
|
|
|
|
Use DateTime.TryParse()
My idea of ideal life : Eat, Sleep, Repeat
|
|
|
|
|
hi,
you want to check data type of parameter.
solution:
string sf = "hello";
double d = 34.3;
if (sf is string)
MessageBox.Show("String");
else
MessageBox.Show("SF not string");
if (sf is double)
MessageBox.Show("double");
else
MessageBox.Show("SF not double");
if (d is string)
MessageBox.Show("string");
else
MessageBox.Show("dnot string");
if (d is double)
MessageBox.Show("double");
else
MessageBox.Show("d not double");
![Rose | [Rose]](https://codeproject.global.ssl.fastly.net/script/Forums/Images/rose.gif)
|
|
|
|
|
Why is the function using a string as parameter? That means that you have to convert all values to strings, then parse them back to the values again.
Make the parameter an object instead, then you can easily check the actual type of the value:
public int SomeFunction(object value) {
if (value is DateTime) {
DateTime d = (DateTime)value;
...
} else if (value is double) {
double d = (double)value;
...
} else {
throw new ArgumentException("Type "+value.GetType().Name+" is not accepted as parameter.");
}
}
Using an object parameter to accept value types means that the values will be boxed. Boxing is something that you generally want to avoid if possible, but in this case there is no common base type between DateTime and Double that you could use instead. Also, converting to a string and back is far worse than boxing.
Despite everything, the person most likely to be fooling you next is yourself.
modified on Friday, May 2, 2008 9:04 AM
|
|
|
|
|
I think I asked him that same thing a few days ago.
|
|
|
|
|
Hi All,
I need to write document for C# Code Standards.Anybody who is having idea about any article regarding the same?
With Regards
|
|
|
|
|
You mean like this[^] ? Second google hit, probably first hit if I searched CP directly.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
There are various books out there on the subject already. Our policy was just to use the standards in the book - it saved us a lot of time writing standards.
|
|
|
|
|
Colin Angus Mackay wrote: Our policy was just to use the standards in the book - it saved us a lot of time writing standards.
Very much true. It saves a lot of time and effort.
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson
|
|
|
|
|
|
|
Hello all. I need some assistance. If I have a form that has buttons (or any Control(s)) on it, and I want to be able to populate a datagridview at runtime by selecting the control name from a combobox, how do you call the GetValue(object, object[] index) method on it to get the value of the property. I have no problem getting the list of property names, but I cannot get the values!
This code works. All but the GetValue method. I keep getting a 'TargetException: Object does not match target type' error.
Please give me a code example of how to properly call this GetValue(object, object[] index) method.
Once again,
The method that updates the gridview(right now it is a textbox in this code; I will change it later) and the class that handles the properties:
private void myCboBox_SelectedIndexChanged(object sender, EventArgs e)
{
MyProperty Myproperty = new MyProperty(null, null);
//creates an int to hold the selected index:
int index = myCboBox.SelectedIndex;
//creates a type variable:
Type t = controls[index].GetType();
PropertyInfo[] myProps = t.GetProperties();
foreach (PropertyInfo p in myProps)
{
richTextBox1.Text += "\n" +
//This GetValue(object, object[] index)
//is what I need to know how to do:
p.Name + "\t\t\t\t" + p.GetValue(Myproperty, null);
}
}
}
public class MyProperty
{
public MyProperty(string aName, object aValue)
{
theName = aName;
theValue = aValue;
}
private string theName;
public string theNameProp
{
get { return theName; }
set { theName = value; }
}
private object theValue;
public object intTest2
{
get { return theValue; }
set { theValue = value; }
}
}
"If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra
|
|
|
|
|
When using GetValue , you must pass an object of the same type that you used to create the PropertyInfo object.
So this should work:
p.GetValue(controls[index], null);
Then you can fill the MyProperty object if you need to.
|
|
|
|
|
Hi,
I am trying to read log files that are updated by a daemon process on the server. The daemon process is an COM application writing to the files exclusively. How can I read the files? To simulate the workflow, I have created the following snippet, which reproduces the problem I am facing.
I get the exception : "The process cannot access the file 'C:\\Temp\\FileLockTest1.txt' because it is being used by another process."
static void Main(string[] args)
{
FileStream exclusiveWriter = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
for (byte counter = 0; counter < 100; counter++)
{
exclusiveWriter.WriteByte(counter);
}
FileStream sharedReader = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
sharedReader.Seek(0, SeekOrigin.Begin);
for (byte counter = 0; counter < 100; counter++)
{
Console.WriteLine(sharedReader.ReadByte());
}
exclusiveWriter.Close();
sharedReader.Close();
Console.ReadLine();
}
Thanks in advance,
- Johnson
|
|
|
|
|
JohnsonDesouza wrote: "The process cannot access the file 'C:\\Temp\\FileLockTest1.txt' because it is being used by another process."
This is because the file is opened by "exclusiveWriter" object and has not closed yet. You need to close it before opening the file for reading.
FileStream exclusiveWriter = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
for (byte counter = 0; counter < 100; counter++)
{
exclusiveWriter.WriteByte(counter);
}
exclusiveWriter.Close();
FileStream sharedReader = new FileStream(@"C:\Temp\FileLockTest1.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
sharedReader.Seek(0, SeekOrigin.Begin);
for (byte counter = 0; counter < 100; counter++)
{
Console.WriteLine(sharedReader.ReadByte());
}
sharedReader.Close();
Console.ReadLine(); You can also consider to use using block.
|
|
|
|
|
JohnsonDesouza wrote: How can I read the files?
To be clear here - you cannot. That's what exclusive lock means. Someone has shown how to remove the lock in C#, if this is a COM C++ program, you need to change that code to close the file.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi,
yes you can read log files from ongoing processes provided they are coded appropriately.
you'll have to change the code: by default a file writer opens the file denying the world to
see the file's content for as long as it is open. IMO you have three options:
1.
have the writer close the file when done; from then on any app can read the file.
2.
have the writer perform open-for-append, append, close for every write it wants to execute.
that way readers will succeed most of the time. This is my way of implementing logging.
It is not great performance wise, but it works great.
3.
have the writer open the file with FileShare.Read i.e. have it allow others to read while
the writer has the file open. There are two caveats:
a) there is some buffering going on, so the writer should perform the right Flush() operation
to force the data out, making it available for the reader(s).
b) the file may be in an intermittent state (not fully consistent) when the reader gets data
at the same time the writer is adding to it. So readers should be tolerant.
|
|
|
|
|
Hi Everyone,
I have a statusstrip control on my form and how do I show some text on it. I tried the text property but nothing is visible on the form. kindly help. I know this question may sound silly to some but then I am just a beginner
Regards,
LG
lgatcodeproject
|
|
|
|
|
Hi,
The status strip is a sort of menu / toobar. You have to add something to it,
like a ToolStripStatusLabel, and then assign your text to that label.Text.
In the forms designer, if you left click on the statusStrip (in the form),
you should see a little box with an arrow. Left click on that, and you get
a menu of items you can add (label, progressBar, dropDownButton,
splitButton). Click on one to get the item added.
Then go to the properties of the item you added and assign the name. I hope this would be helpful.
Regards,
Vinay
ComponentOne LLC.
www.componentone.com
|
|
|
|
|
Hello Vinay,
Thanks, it solved my purpose. Can we configure some other items (other than those provided in the dropdown)to this status strip in runtime?
Regars,
LG.
lgatcodeproject
|
|
|
|
|
One of my articles might be able to help you their: Custom ToolStrip Renderers[^]. You are able to add other controls using the ToolStripControlHost. You can browse most of the classes that can manipulate the ToolStrips by using the Object Browser.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios[ ^]
|
|
|
|