|
The approach you are using is probably not ideal. The only thing I can think of is to eliminate the members and add an indexor.
Ideally, however, each property should access an individual member. It is the most logical method.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
You can't get the variable name from the object, as you are not accessing the variable. You are just accessing an object that the variable happens to be referencing. There is no backwards reference from the object to the variable.
If you want the DataItem object to know the name of the variable, you have to store the name in the object.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi
I am using reflection to write properties in some code. But, I don't need to fetch readonly properties. How can I check whether a property is readonly or not in .NET?
Please advise. Thanks
Pankaj
|
|
|
|
|
You can use the second line specifying the BindingFlags with a SetProperty which excludes all properties that don't include a set declaration on the property.
static IEnumerable<PropertyInfo> GetWritableProperties<T>()
{
Type type = typeof(T);
return type.GetProperties(BindingFlags.SetProperty);
}
I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder
|
|
|
|
|
Which is preferred, to pass a reference or use a return value
private DateTime SetToHour(DateTime timeCheck, int hours)
{
return timeCheck;
}
or
private void SetToHour(ref DateTime timeCheck, int hours)
{
}
I have done some searching online and asked around work, no definitive answer has come. Use ref for this and return for that. Mostly seems to personal choice. Thoughts???
this thing looks like it was written by an epileptic ferret
Dave Kreskowiak
|
|
|
|
|
Hi,
IMO a return value is the normal approach.
It has some drawbacks:
- you can return only one thing;
- if the return thingy is a reference type, then the API choice implies you must
create an object of that type, which you may or may not want to impose on the API users.
Hence, a ref parameter is acceptable and useful when:
- the function has to produce more than one thing;
- or the function is supposed to stuff some data in a pre-existing struct.
|
|
|
|
|
Thanks, answered my question succinctly.
this thing looks like it was written by an epileptic ferret
Dave Kreskowiak
|
|
|
|
|
In my opinion I'd use the non-ref method. The reason is, that many classes in the .NET framework are immutable, like string . Even more important, the DateTime class also uses return values (AddHours , AddMonths , etc.), which means that DateTime objects are immutable, too.
In C# 3.0 you could also use extension methods, for example like this:
public static class DateTimeExtensions
{
public static DateTime SetToHour(this DateTime source, int hours)
{
if ((hours < 0) || (hours > 23))
throw new ArgumentOutOfRangeException("Hours must be between 0 and 23");
return source.AddHours(hours - source.Hour);
}
}
and call it like that:
DateTime t = DateTime.Now;
Console.WriteLine(t);
t = t.SetToHour(24);
Console.WriteLine(t);
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
>>The reason is, that many classes in the .NET framework are immutable, like string.
Im not quite getting in what way you mean that would affect anything?
Its perfectly possible to pass a "ref string"
Even if the instance of a string is immutable doesnt mean that the variable reference to that instance is immutable...
Or do you just mean that the api would be more standard because other methods on immutable types work that way?
(In that case I agree , rule of least surprise)
|
|
|
|
|
Roger Alsing wrote:
Its perfectly possible to pass a "ref string"
Even if the instance of a string is immutable doesnt mean that the variable reference to that instance is immutable...
Absolutely correct - what I meant is that the .NET library methods for the string and DateTime objects (among others) do not modify the variable but rather return a new instance. So if you want to add another method to the DateTime class (by using extension methods or simply a utility class) I might be a good idea to do it in the same way like the original DateTime methods do, by returning a new instance.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I developed a C# console program to control a camera's PTZ, and I call remote procedures from a GUI to move left/right/up/down. As I have 2 WindowsXP computers my C# RPC's work great. QUESTION: I also need to drive this PTZ from a computer running Windows, but the coding environment is JAVA. What does the JAVA coder need to do in order to call my functions? thanks, xlthim
|
|
|
|
|
Couple ways to do this. A popular one seems to be utilizing Mono's IKVM[^].
Life, family, faith: Give me a visit.
From my latest post: "When Constantine severed the Hebrew origins of this faith in Messiah, a new religion was officially created. It is this religion that Orthodox Jews fear their relatives and friends becoming members of."
Judah Himango
|
|
|
|
|
I am trying to use a DLL which was originaly written in fortran. Allthough this DLL is suposed to be in C, a lot of its functions do not work in C# (some does). I believe it has fortran version as well as C version. Has anyone played with Fortran DLL in C#. I get the following errors during compilation.
AccessViolationException
BadImageFormatException
DllNotFoundException
YOu can go to this link http://www.originlab.com/index.aspx?s=8&lm=117&pid=251[^]
download the "examples in zip form". They have a simple DLL,FortranDLL, which contains basic functions such as double SUMFT(double, double), which returns the sum of two doubles. Can any one make it work? What do i need to make it work?
|
|
|
|
|
I believe you can P/Invoke into most (all?) Fortran calls. Have you tried this? The Numerical Algorithms Group shows how to call their Fortran dll from C#[^], perhaps that will get you off to a good start.
If this fails or doesn't work for certain calls, but you can access it via C++, you could write a C++/CLI (managed C++) dll that talks to the Fortran dll, then have your C# code talk to the C++/CLI dll. There are articles on this site that show you how to write simple C++/CLI dlls to call native code.
Life, family, faith: Give me a visit.
From my latest post: "When Constantine severed the Hebrew origins of this faith in Messiah, a new religion was officially created. It is this religion that Orthodox Jews fear their relatives and friends becoming members of."
Judah Himango
|
|
|
|
|
How may I stop a running method if it takes longer than 10 seconds. I want a procedure called hello() to be called 200 times in a loop & I want the method to be aborted & the loop to be broken if the hello() methods, fails to respond in 10 seconds. How may I do this?
|
|
|
|
|
semiramisContraA,
You've already asked that question Here[^]
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
I know but unfortunately I didn't get any useful answers!
|
|
|
|
|
Do you want to break the loop if the total time is longer than 2000 seconds, or do you want each Hello call to time out after 10 seconds? Can your code handle some Hello calls running concurrently?
Semicolons: The number one seller of ostomy bags world wide. - dan neely
|
|
|
|
|
If you posted it somewhere already and you didn't get any un-useful answers, and you repost on the same site that exact same question then you will get the same un-useful answers and aggrivated codeprojectians as well.
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
Discounted or Free Software for Students:
DreamSpark - downloads.channel8.msdn.com
MSDN Academic Alliance - www.msdnaa.com
|
|
|
|
|
I Think you are wrong, the second time I posted the message, I got the answer, I was looking for.
In this site as the posting load is very high, your post gets buried under other ones very soon, so many people who may know the answer do not get the chance to read it, by reposting a request you double the chance.
|
|
|
|
|
Start the method in it's own thread, then if it doesn't respond for a while you could end it.
I'm not sure why you'd want to do this though, what would cause the method not to respond?
My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Thank you for your response, I've tried multi threading, but as I do not want to use of static functions, it doesn't work.
I'm writing a code which is about to measure the vulnerability of a web service facing dos(denial of service) attacks. So I'm calling a method of a specific service 500 times to see if it is able to handle the requests. So you see that I should be able to abort the calls if the service, fails to answer.
|
|
|
|
|
have a look at the backgroundworker class.
|
|
|
|
|
I'm getting an InvalidOperationException with the following message:
"Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function."
Essentially, the context is this:
Initially, I do something like this:
BindingSource bds = new BindingSource(dataSet1, dataTableString1);
dataGridView1.DataSource = bds;
But, say I perform an operation on the datagrid which makes me want to reload the entire datagrid. What I do is something like this:
dataGridView1.DataSource = null;
dataGridView1.DataBindings.Clear();
dataSet1.Clear();
sqlDataAdapter1.Fill(dataSet1);
BindingSource bds = new BindingSource(dataSet1, dataTableString1);
dataGridView1.DataSource = bds;
The exception is thrown on the first statement when the data source is set to null. I've looked around and it seems like various situations will cause this issue, but not enough to give me a good idea on how to fix it.
|
|
|
|
|
Cyrilix wrote: dataGridView1.DataSource = bds;
That should be sufficient to replace the datasource.
|
|
|
|