|
Colin Angus Mackay wrote: If you want to pass a reference to a property you might find that delegates work for you (but I've not tried).
What do you mean by that, Colin? I don't understand as delegates can't be used on properties.
|
|
|
|
|
Judah Himango wrote: What do you mean by that, Colin?
Really? Okay - I knew that properties were just syntactic sugar for a getter and setter method so I thought you might be able to use delegates. Note my disclaimer "but I've not tried".
|
|
|
|
|
Yeah, it's a real shame you cannot use a delegate over a property. I often use delegates to give me the names of methods used in unit testing:
viewMock.Expect(ViewCloseMethodName);
...
string ViewCloseMethodName
{
get { return new Function(this.view.Close).Method.Name; }
}
Sadly this doesn't work for properties, meaning my unit testing mocks are prone to failure if I happen to rename a property.
|
|
|
|
|
Judah Himango wrote: I don't understand as delegates can't be used on properties.
You can work around that using reflection, but the end result is really ugly.
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
I'm actually looking for a way get the string name of a property through code. I have the property sitting there typed, i.e.
IFoo someType;
someType.SomeProperty;
Is there a way to get the string name of the property? Obviously I can just do string propName = "SomeProperty" , but that instantly breaks the moment I change the name of the property.
|
|
|
|
|
Judah Himango wrote:
Is there a way to get the string name of the property? Obviously I can just do string propName = "SomeProperty", but that instantly breaks the moment I change the name of the property.
AFAIK no. I was trying to find that capabilty unsuccessfully in my decent into delegate hell.
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
Too bad they don't let us access the get_ and set_ methods directly. I'd really like to be able to do this:
ThreadStart ts = get_Prop;
string Prop
{
get { ... }
}
|
|
|
|
|
Same here.
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
using System.Reflection;
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.GetPropertyName(typeof(MyTestClass),3));
}
private string GetPropertyName(Type myType, int nPropToGet)
{
int nPropCounter = 0;
foreach (PropertyInfo pi in myType.GetProperties())
{
nPropCounter+=1;
if (nPropCounter == nPropToGet)
return pi.Name;
}
return null;
}
-- modified at 18:04 Tuesday 21st November, 2006
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
Eric, it's true that would solve the string-based reflection problem. However, it would introduce a different problem which is even more severe than the string-based reflection problem, right? I mean, if I add a new property to my class, this solution would break because the property index would change.
Really what I'm looking for is getting the name of a property when I actually have access to the property.
view.SomeProperty.Name
Or something a little more delegate-oriented:
new Function(view.set_SomeProperty).Method.Name
either would suffice just fine! Seems really odd we cannot do this. If you use unit testing with mock objects, you'll find this is in your face the whole time: you'll say something like:
myMockView.Expect("set_MyProperty");
If I or another dev comes along and renames the property, the unit test breaks.
|
|
|
|
|
I've never done unit testing, and I'm new to C#. I'm studying reflection right now, and I'm a bit surprised that I can't seem to find a way to get PropertyInfo for a property directly (as you mentioned). I feel like I must be missing something...but I'll defer to your experience.
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
There is one possibility I see: if you're inside the property, one could do MethodInfo.GetCurrentMethod() to retrieve the method while inside the getter or setter of the property. Outside of that, there's no way I see that doesn't rely on either string-based reflection or index-based reflection, both of which have problems when modifying existing code.
|
|
|
|
|
It would be possible to implement in the language, but I don't think that there are so many that are interrested in developing it in that direction.
Using out/ref parameters is the old, procedural, way of handling data. Properties is the new, object oriented, way of handling data.
Also, if it was implemented, it would implicitly use a temporary variable anyway, so there is no real gain. On the contrary, it would hide what's actually going on, probably causing quite some confusion along the way.
If I for example have a property that will display it's value on the screen, and I send that property to a method using ref, you could expect that every time the method changes the value, the property would change and the value would be displayed on the screen. That is of course not so, as the method only would be changing the temporary variable, and the property would only be changed after the method returns.
---
b { font-weight: normal; }
|
|
|
|
|
the current situation can result in really ugly code though.
dataobject.someProperty = formObject.recomputeValue(dataobject.someProperty, otherValue1, otherValue2, ...);
or
<br />
int temp = dataobject.someProperty;<br />
formObject.recomputeValue(out temp, otherValue1, otherValue2, ...);<br />
dataobject.someProperty = temp;<br />
vs
formObject.recomputeValue(out dataobject.someProperty, otherValue1, otherValue2, ...);
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
dan neely wrote: the current situation can result in really ugly code though.
dataobject.someProperty = formObject.recomputeValue(dataobject.someProperty, otherValue1, otherValue2, ...);
Why do you think that the code is ugly? If the method returns a value, the return value is a good place to put it.
dan neely wrote: or
int temp = dataobject.someProperty;
formObject.recomputeValue(out temp, otherValue1, otherValue2, ...);
dataobject.someProperty = temp;
That should actually be:
int temp;<br />
formObject.recomputeValue(out temp, otherValue1, otherValue2, ...);<br />
dataobject.someProperty = temp;
There is no reason to assign anything to a variable that is going to be used as an out parameter.
Using out/ref parameter and properties are two completely different approaches, so it's not at all a bad thing that they are separate in the code.
dan neely wrote: vs
formObject.recomputeValue(out dataobject.someProperty, otherValue1, otherValue2, ...);
If that would be allowed, it would be implemented exactly as the code with the temporary variable, with the only exception that the the temporary variable doesn't have a name.
Allowing properties as out parameters would only serve to hide what's actually going on. In a language like VB that might be desired, but hardly in C#.
Shorter code is not always better code.
---
b { font-weight: normal; }
|
|
|
|
|
Guffa wrote:
Why do you think that the code is ugly? If the method returns a value, the return value is a good place to put it.
Because langague limitations are forcing me to stuff a parameter in two locations, not just one.
Guffa wrote: That should actually be:
Actually it should've been:
int temp;<br />
formObject.recomputeValue(ref temp, otherValue1, otherValue2, ...);<br />
dataobject.someProperty = temp;
In this case the recompute does depend on the original value.
Guffa wrote: If that would be allowed, it would be implemented exactly as the code with the temporary variable, with the only exception that the the temporary variable doesn't have a name.
Allowing properties as out parameters would only serve to hide what's actually going on. In a language like VB that might be desired, but hardly in C#.
Shorter code is not always better code.
The intent of properties is to wrap the datahiding and validation of get/set methods, and allow them to be used like a public variable. This is the most severe failing of the itent. Having used property names and reflection to work around the limitation once before in a major code block, if i have to do it again, I'll be using public data members instead.
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
Hi,
I have been getting this error when trying to open a connection within transactionScope of .net 2.0.
"The transaction has already been implicitly or explicitly committed or aborted."
I have visual studio 2.0 Professional and am using C# for class library that I am working on. I get this error when trying to open the connection to SQL Server 2000.
Any help would be greatly appreciated.
if it helps here is my code. myID.GetNextID(); is the method where error occurs. I am opening a sql server connction and execute the stored procedure in this method. When i try to open the connection, that is where the error occurs
"The transaction has already been implicitly or explicitly committed or aborted"
public void Execute()
{
MyID myID = new MyID()
DataTable myRecords = GetRecords();
foreach (DataRow dr in myRecords.Rows)
{
using (TransactionScope scope = new TransactionScope())
{
int idint= myID.GetNextID();
string summaryUpdateQuery = "UPDATE tblSummarySample SET ID= " + idint+ " WHERE ServiceID= " + dr["mySampleID"].ToString();
UpdateSummaryRecord(summaryUpdateQuery);
string detailUpdateQuery = "UPDATE tblDetailSample SET TransID = " + idint+ " WHERE ServiceID= " + dr["mySampleID"].ToString();
UpdateMyRecords(detailUpdateQuery);
scope.Complete();
}
}
}
Thanks
Needy
|
|
|
|
|
Your code doesn't show what is happening. What is myID.GetNextID() doing? There is very little database access code in the snippet you provided so it is difficult to gauge the likely state of the database at the point of failure.
Also, your code is susceptable to a SQL Injection Attack, you may wish to consider using parameterised queries at a minimum to offset the chance of an attack.
|
|
|
|
|
I found the solution to the problem.
go to Administrative tools
Component Servers
MyComputer (right click)
go to properties
MSDTC TAB
Click on Security Configuration Button
Make sure Network DTS Access, Allow Remote Access, Allow Inbound, Allow Outbound, Enable Transaction Internet Protocol (TIP) Transactions are CHECKED.
Most important of all is No authentication Required option. This option should be selected otherwise even if all of the above check boxes are selected, the exception will not go away.
Somehow error description is completely different than "NO Authentican Required" radio button.
Anyway, I would wanted to post the solution so others can benefit.
Thanks
Needy
|
|
|
|
|
bemahesh wrote: I found the solution to the problem.
Are you sure?
bemahesh wrote: Most important of all is No authentication Required option. This option should be selected otherwise even if all of the above check boxes are selected, the exception will not go away.
Does it really solve your problem or does it just make the exception "go away"? What makes it go away? How does this solution work?
I don't mean to be difficult, but it looks to me that you've found some controls to fiddle with and in a certain state your exception goes away, but I don't see an explanation of why it makes the exception go away.
Unless you can explain why this works, and why it will continue to work, you've effectively built your house on the sand.
If you do know why this works, then you might want to explain to better educate those that come after you.
|
|
|
|
|
well, I am not an expert at TransactionScope. I just started using it for first time ever using .net 2.0. I may not be able to answer your questions since it seems your knowledge in the area may be greater than me. However, I was googaling for exception message and found out that if these settings are checked but didn't mention No authentication Required. I tried checking the check boxes but couldn't get transaction scope to work.
I was still getting the exception. At the end, I tried checking the No authentication requiered option (radio button). and, that worked (what i mean by working or GO AWAY is that when i try to open the connection it indeed opens the connection and doesn't throw the exception stating "the transaction.....blah blah blah".
I tried executing the update statements and they indeed executed as expected.
I may be ignorent or missing some important implications you may have suggested (in terms of security?). Please educate me further.
I will be waiting for further input
thanks a lot
Thanks
Needy
|
|
|
|
|
This settings are to make windows safer. Check more on this [here in my post].
|
|
|
|
|
Hi there,
I do xslt transformation and i've got sth like that:
(where ds - is my dataset, output.txt - is my result
---------------------------------------------------------------
//Create a new XslTransform object.
XslTransform xslt = new XslTransform();
//Load the stylesheet.
xslt.Load(Application.StartupPath + @"\Temp\xsltFile.xsl");
//Create a new XPathDocument and load the XML data to be transformed.
ds.WriteXml(Application.StartupPath + @"\Temp\officedata.xml");
XPathDocument mydata = new XPathDocument(Application.StartupPath + @"\Temp\officedata.xml");
//Create an XmlTextWriter
XmlWriter writer = new XmlTextWriter(Application.StartupPath + @"\Outbox\output.txt" , System.Text.Encoding.ASCII);
//Transform the data.
xslt.Transform(mydata,null,writer, null);
writer.Flush();
writer.Close();
-----------------------------------------------------
How do the same thing without creating temp file officedata.xml? (create XpathDocument directly from ds.)
and how load xslt file from string instead of xsltFile.xsl?
Does anyone have an idea?
Many thanks,
Kamil
|
|
|
|
|
Hi All,
A Windows Forms application (not asp.net)
I have 4 class library projects which contain classes and controls and forms which are naturally grouped together. 3 are c#, one is c++ with some unmanaged code in it.
My main application is a windows forms project with references to the other 4 applications in order to use their components.
I'm now trying to add a preferences system, using the .net 2.0 built in application settings stuff.
But, this leads me to a problem. If I define my settingsbase derived class in my main windows forms project, then the other projects know nothing about it, and i cannot give them a reference to the main project because it would cause a circular reference.
The only way around this i can see would be to implement yet another project with just the settings stuff in, because then all 5 current projects can reference it. But i'm not sure i want to do this. Is there another way??
Do other people run into these circular reference problems when trying to organise their code into multiple projects?
I guess i could join all the code into just two projects (a c# one and a c++ one), but that goes against the values of code reuse!
The other solution is to have each project implement and manage it's own settings, and save them from the main project - but surely this results in 4 separate settings files?
Thanks in advance for any ideas
Jon
|
|
|
|
|
Hi. I'm having trouble with setting the focus with WindowsForms. I'm trying to achieve the Windows OnScreenKeyBoard functionllity - achieving some action without getting the focus. I use the SendKeys.SendWait method to send keys to the window in focus, but in my case it is always the form containing the keyboard that is in focus
Can anyone give me a hint?
|
|
|
|
|