|
I don't love it. I can see it degenerating quickly in spaghetti-code; but then I generally avoid standard Tuples anyway in favor of interfaces.
I don't think there are more concurrency issues than you would see with a standard property accessor. There are definitely new gotchas when implementing property update events.
I think this is just another functional feature being stitched in. I don't think this really helps out OOP at all.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
Nathan Minier wrote: I generally avoid standard Tuples anyway in favor of interfaces. Thanks; I would be interested in seeing a code sample using an Interface that is an alternative to use of a Tuple.Nathan Minier wrote: There are definitely new gotchas when implementing property update events. That seems right; the fact you can't make the objects in a ValueTuple immutable means at some point an internal value can be changed, and then, the ValueTuple may be out of synch with whatever sources it was built from.
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
modified 20-Jun-17 13:04pm.
|
|
|
|
|
BillWoodruff wrote: Thanks; I would be interested in seeing a code sample using an Interface that is an alternative to use of a Tuple.
It's not any more complicated than you think.
interface IExample{
int Number {get;set}
string Text {get;set;}
}
Tuple<int,string> ExampleTuple;
IExample ExampleImplementation;
I've just always felt that Tuples are a quick workaround when you don't have a proper system (or module) design. I think they're fine for proof-of-concept, but avoid them like the plague in production code for maintainability reasons.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
First gotcha is that you omit XML documentation on a member. Your fullname, does it include the birthnames, does it include a title, what?
How is it more readable than returning a known structure? And does it justify the money saved on 'typing'?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
fyi: I'm not evangelizing for use of ValueTuples, I'm inquiring about them.Eddy Vluggen wrote: you omit XML documentation on a member. The post is a short, simplified, example intended to elicit discussion.Eddy Vluggen wrote: How is it more readable than returning a known structure? I do not make the claim it is more readable in every circumstance.Eddy Vluggen wrote: does it justify the money saved on 'typing'? It is useful to know that is not obvious.
thanks, Bill
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
modified 20-Jun-17 12:54pm.
|
|
|
|
|
BillWoodruff wrote: fyi: I'm not evangelizing for use of ValueTuples, I'm inquiring about them. I know; I'm playing my part in being sceptical
BillWoodruff wrote: The post is a short, simplified, example intended to elicit discussion. I look forward to seeing it in the wild; anything that can be abused will be.
BillWoodruff wrote: I do not make the claim it is more readable in every circumstance. My apologies for attacking you; I already assumed you weren't referring to "every" circumstance, and also assumed that it would be the main reason to introduce this change to the syntax.
As it looks, for this moment, it is merely a shortcut to prevent one from declaring a complete struct. Which means it is incompatible with older compilers and that some coders will not recognize the new syntax, without the only obvious benefit being that it conveys more information using less symbols (aka, more readable).
Or, in simpeler terms; you asked "when" you'd use the syntax, and I replied with the question "what's in it for me if I do?". It is the basic Garfield-question, "what do I gain with this". Answering that question often answers the "when".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi, Eddy, Skepticism is good fertilizer for the growth of judgement
I, too, see the downsides of using this feature, particularly in a team setting where everyone is not "on the same page."
I think you might find Mads Torgensen's comments on the mutability of ValueTuple interesting: quoted in InfoQ article by Jonathan Allen: [^]Quote: An interesting note about ValueTuple is that it is mutable. Mads Torgersen explains why,
The reasons why mutable structs are often bad, don't apply to tuples.
If you write a mutable struct in the usual encapsulated way, with private state and public, mutator properties and methods, then you are in for some bad surprises. The reason is that whenever those structs are held in a readonly variable, the mutators will silently work on a copy of the struct!
Tuples, however, simply have public, mutable fields. By design there are no mutators, and hence no risk of the above phenomenon.
Also, again because they are structs, they are copied whenever they are passed around. They aren't directly shared between threads, and don't suffer the risks of "shared mutable state" either. This is in contrast to the System.Tuple family of types, which are classes and therefore need to be immutable to be thread safe."
|
|
|
|
|
Aw, a little devil in those details
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Currently, I do not like Tuples because they remove so much information (what's Item1 ?). But with these new Value Tuples, things are changing.
Your example above is a typical textbook example, where I do not see the full power of Value Tuples. In my opinion, their major advantages are with more functional styles of programming, e.g. with Linq.
|
|
|
|
|
Thanks, Bernhard; exactly the type of response I hoped to get.Bernhard Hiller wrote: Your example above is a typical textbook example, where I do not see the full power of Value Tuples. Yes, the example is simple, by design.Bernhard Hiller wrote: ... their major advantages are with more functional styles of programming, e.g. with Linq. I think we all might benefit from a better example illustrating this. I guess you are referring to using Linq to iterate-over or transform some IEnumerable collection of ValueTuples ?
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
|
|
|
|
|
I can possibly see them being useful internally within a single project. But for shared code, a traditional class or struct would still be the better option.
Code, code and more code.: Exploring Tuples as a Library Author[^]
C# 7: Dynamic types and Reflection cannot access Tuple fields by name[^]
C# 7 ValueTuple types and their limitations | Joseph Woodward, Software Developer[^]
NB: Your example won't compile. You're missing the types for the middle and last constructor arguments, and the FullName property doesn't have a setter.
public Name(string first = null, string middle = null, string last = null) { ... }
...
public (string first, string middle, string last) FullName
{
get
{
return (first: FirstName, middle: MiddleName, last: LastName);
}
set
{
FirstName = value.first;
MiddleName = value.middle;
LastName = value.last;
}
}
You'd probably also want to declare a deconstructor on the class:
public void Deconstruct(out string first, out string middle, out string last)
{
first = FirstName;
middle = MiddleName;
last = LastName;
}
That would let you pick out parts of the class:
var (_, _, lastname) = nametest2;
c sharp7 deconstruction demystified · Surfing the code[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, Richard; that's even more than the type of response I hoped to get !
Corrected those code errors you spotted.Richard Deeming wrote: I can possibly see them being useful internally within a single project. But for shared code, a traditional class or struct would still be the better option. I can't really visualize how ValueTuples could be used in shared code. Perhaps studying the links you posted will enable me to understand that more clearly.
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
|
|
|
|
|
I find that when I "try to reduce my typing" (too soon), the design of whatever I'm building starts to suffer.
My mantra these days: "granularity" (in which case, "more" is better).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hi,
I have a string variable which the value is html code.
I like to check to see if certain word (in this case EEST) exist in the first element as you can see in the sample. Then I like to remove that element and its content from the string.
<p class="myclass">Some text here, 26 APRIL 2017 AT 9:00 AM EEST. some other content here as well></p>
<p>much other elements here as well</p>
then the output becomes:
<p>much other elements here as well</p>
I was wondering, how I can do such task.
Thank you.
|
|
|
|
|
Try a regex:
public static Regex regex = new Regex(
"\\<p\\sclass=\"myclass\"\\>.*?\\sEEST.*?\\>\\</p\\>",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
); Then just use Replace:
string result = regex.Replace(inputText,"");
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You are amazing! Thanks!
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi, I am using Windows media player in my project, if I call media player from my main form its playing, I have written code that when I click Wmp it goes to main form but when I do this it is showing "Attempted to read or write protected memory. This is often an indication that other memory is Corrupt". Can anyone provide solution for this, Thanks in advance .
Mainform code:
Frmads frm=new frmads(this);
Frm.show();
This.hide();
Ads Form code:
If(e.newstate = =2)
{
Frmmain.Activate();
Frmmain.show();
This.close();
}
|
|
|
|
|
Please do not post the same question in multiple places - it wastes peoples time and effort.
You already have this in QA so posting here just duplicates work.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello
I have a small problem. I have this in my XAML file
<Image
x:Name="MyPic"
HorizontalAlignment="Left"
Source="..\imgs\xxxx.jpg">
Now I want to change the picture during runtime. I found some examples how to do that but I don't want to load the picture from the hard disk. When I give my programm away I don't want to have put the image files to it too. It would be much nicer if the pictures would be inside my programm as resource and if I could exchange them during runtime from the resource. I tried to add the picture to the resource and then load from there:
m_Img = Properties.Resources.ResourceManager.GetObject("MyPicture") as Image;
MyPic.Source = m_Img.Source;
But m_Img is null
What am I doing wrong?
|
|
|
|
|
Might be a good thing if you describe exactly how you bind/put the images to/in the Resource bag ... and/or show relevant code.
See if this is useful: [^].
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
|
|
|
|
|
Hi,
I have a weird problem, hopefully someone can shine a light on it?
The user gets a list of defects on a product from the database. I put them in a List<drumdefect>, like so:
while (rdr.Read())
{
DrumDefect defect = new DrumDefect()
{
AVI_Defect = new AVIDefect()
{
SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
},
MVI_Defect = new Defect()
{
Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
},
FVI_Defect = new Defect()
};
list.Add(defect);
}
Then I import them in a datagridview like so:
_SelectedDrum.Defects = database.GetList();
dataGridViewDefects.DataSource = null;
dataGridViewDefects.DataSource = _SelectedDrum.Defects;
dataGridViewDefects.Refresh();
The user can add a defect, so I add one like this:
DrumDefect defect = new DrumDefect()
{
AVI_Defect = new AVIDefect()
{
SequenceNumber = -1,
Code = "# 0",
Name = "NODEFECT",
Severity = DefectSeverity.NODEFECT
},
MVI_Defect = new Defect()
{
Code = "# 0",
Name = "NODEFECT",
Severity = DefectSeverity.NODEFECT
},
FVI_Defect = new Defect()
};
_SelectedDrum.Defects.Add(defect);
it all works fine, except when the database list is empty, the user adds a defect, and you click with the mouse in the datagridview (but only at the click), then the application crashes with this error:
System.IndexOutOfRangeException occurred
Message=Index -1 does not have a value.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.get_Current()
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)
at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Xerox.ODB2.twister.Program.Main() in c:\Users\y952hrym\Documents\CS_Projects_2\Twister\Twister\Twister.View\Program.cs:line 18
InnerException:
Thanks
modified 15-Jun-17 4:22am.
|
|
|
|
|
Do you have an event handler method set up for the click event?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
yes i have, but it does not reach the breakpoint.
private void dataGridViewDefects_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
}
in fact i have the following event procedures, but none of them is the source of the problem (breakpoint not hit when the error occures)
this.dataGridViewDefects.CellMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects_CellMouseClick);
this.dataGridViewDefects.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects_CellMouseDoubleClick);
this.dataGridViewDefects.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewDefects_RowEnter);
this.dataGridViewDefects.Enter += new System.EventHandler(this.dataGridViewDefects_Enter);
this.dataGridViewDefects.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridViewDefects_KeyDown);
this.dataGridViewDefects.Leave += new System.EventHandler(this.dataGridViewDefects_Leave);
|
|
|
|
|
Are any of the methods in the stack trace yours, or are they all framework?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|