|
Yes; curious ... one usually needs to take deliberate steps in order to clone another object; particularly if a deep clone is required.
(Except when there's a systemic anomaly in the matrix).
|
|
|
|
|
I'm kind of curious here. When are you interested in calling a clone to get a shallow copy? Id just create a pointer instead then, wouldn't you?
In making a deep copy always end up doing this:
Class MyClasss
Implements ICloneable
Private pName As String
Public Property Name() As String
Get
Return pName
End Get
Set(ByVal value As String)
pName = value
End Set
End Property
Private pNAmes As New List(Of String)
Public Property Names() As List(Of String)
Get
Return pNAmes
End Get
Set(ByVal value As List(Of String))
pNAmes = value
End Set
End Property
Public Function Clone() As Object Implements ICloneable.Clone
Dim result As New MyClasss
result = Me.MemberwiseClone
' If I have any properties of collection type
' I need to create a copy of each list
Dim TempNames As New List(Of String)
For Each str As String In Names
TempNames.Add(str)
Next
result.Names = TempNames
Return result
End Function
End Class
But I would sure like to know why the MemberwiseClone can't clone lists or collections like this. I also bet there is a more generic way of implementing it though.
|
|
|
|
|
I think you'd benefit by posting this as a separate question.
cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
I tend to use shallow copies in my CRUD operations when I'm not interested in creating an entire graph of objects and just need access to one object's instance values: e.g. for change tracking / undoing changes; creating and initializing binding sources; etc.
A reference to an existing object used for another purpose would not work in this case.
I suspect MemberwiseClone() is only intended to be a primitive for shallow copies because there are a number of ways to do deep copies, including reflection and serialization / deserialization:
static object Clone( object parent ) {
XmlSerializer serializer = new XmlSerializer( parent.GetType() );
object clone = null;
using ( MemoryStream stream = new MemoryStream() ) {
serializer.Serialize( stream, parent );
stream.Position = 0;
clone = serializer.Deserialize( stream );
}
return clone;
}
|
|
|
|
|
I like the serialization/deserialization methodf as I'm often also interested in saving classes to file, but I cant help by feeling that it's a bit strange. If I say clone this class; I would think it would be easy to say, just create an exact independent copy of this class.
I also found a good description of the pro/cons of the different ways to clone a class:
http://developerscon.blogspot.no/2008/06/c-object-clone-wars.html[^]
As for the MemberwiseClone method, I don't really know the story behind it, but I seem to remember that it would give you an error in the previous versions of .NET (2.0?), If you tried to access a property of collection type. Now it just gives you a pointer to the original class.
I also realized that Bill was right. Should have started a new thread, but I just wondered what people did, so It didn't feel like a 'real' question.
|
|
|
|
|
only type the integer value in the text box,in case type any float value or character value means create a label than indicate "the value is not to integer format".c sharp windows application it's all under in one
modified 24-Sep-15 12:58pm.
|
|
|
|
|
See this[^] post (assuming you're talking WinForms).
Member 11982934 wrote: plz help me it's urzent Not to me, it isn't. Remember, CP is staffed by volunteers who take time out of their day to help others. Marking a question "urgent" doesn't reduce the urgency of my own job.
/ravi
|
|
|
|
|
|
I have to agree with Ravi - I checked with everybody here and it's not on anybodies top 100 "things to do today" list. So it's not urgent at all, and I'll get round to it one day. Real soon, probably. Well, maybe soon. Could be Christmas...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Only allow digits in the TextBox (use a mask and / or an event handler) ... This avoids the whole question of whether it is an integer, float, negative, etc.
|
|
|
|
|
Hi, I have inherited UserControl to MySubControl to add a common core base for a new set of controls.
However, when events are added in VS2013 designer, MyControl the events attached to MYControl do not fire.
i.e.
this.MyControl = new MySubControl .MyControl ();
this.MyControl .Load += new System.EventHandler(this.MyControl );
Can anyone point me in the right direction..
Thanks
public class MySubControl : UserControl
{
}
public partial class MyControl : MySubControl
{
}
|
|
|
|
|
Um...that's some odd code. Does it compile?
Normally, you would add the handler by pointing at a method - not at an instance.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
A 'Load EventHandler in an inherting UserControl, created at design-time, or run-time in code ... will work.
A 'Load EventHandler in a UserControl named 'MyControl which inherits from a UserControl, 'MySubControl will fire after you have created an instance of 'MyControl by:
1. design-time: drag-drop from ToolBox to a Form or other Container Control
or
2. run-time: you have created an instance in code
And you then execute your program.
To help you beyond this, I think we need more details from about what you are doing.
Please show code that you are using that demonstrates what you describe.
Are you only creating instances of 'MyControl ?
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
A user control still needs to be added to a Window's .Controls collection before it will display (in a Window) and fire the .Load event.
|
|
|
|
|
That's an excellent point to stress, Gerry !
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Thanks for the response guys, I have sorted it now.
I had a sendmessage in an area of code which seemed to block all events from firing back. Moved to somewhere more sensible.
|
|
|
|
|
Hi Gautham,
Nice to see your article on Selenium webdriver using C#
I have gone through one of your article-(Automated Testing Of Web Pages Using Selenium-Web Driver).In that i have few doubts
1.How to add the all(chrome,iedriver exe) files folder to enviornment variables in my local machine.Please take a look and help me on this issue.
2.I have written n ran code like below,getting error as source code not found.please see the screen shot also.
public static IWebDriver InitializeDriver()
{
//if (WebDriver == null)
// {
//string DRIVER_PATH = @"C:\Web drivers\IEDriverServer.exe";
string browser = Convert.ToString(ConfigurationManager.AppSettings["BrowserType"]);
switch (browser)
{
case "ie":
WebDriver = new InternetExplorerDriver(@"C:\Web drivers\IEDriverServer.exe");
return WebDriver;
//break;
//case "FF":
// WebDriver = new FirefoxDriver(DRIVER_PATH);
// break;
case "chrome":
WebDriver = new ChromeDriver(@"C:\Drivers\BrowserDrivers\chromedriver");
return WebDriver;
//break;
default:
WebDriver = new FirefoxDriver();
return WebDriver;
//break;
}
// }
}
|
|
|
|
|
I haven't written an article on Selenium so your message obviously isn't directed at me. I'm pretty sure none of the regulars here have either.
When you have a question about an article, the correct place to ask your question is on the forum at the end of the article. Don't post in a random place hoping that the author will stumble across it.
|
|
|
|
|
Hello,
Apologies for what surely is a very basic question. I'm new to C# and need to write some easy functions for an application we use at work. This application uses a special DateTime as it needs to accommodate unknown date parts, e.g. UNK/May/2015. Ideally I would like to know the difference in months between two dates, but for a start I'm happy with just the year difference (the year will always be known). I've written the following function which throws no errors but it always returns 0. I can see while debugging that StDt.Year and EnSt.Year are integers with the correct year, but it doesn't seem to calculate the PackYears. Any help is greatly appreciated!
[DesignerFunction("Calculates the pack years")]
public static int fnCalculatePackYears(
[DesignerParameter("Start Date (date)")] PFDateTime StDt,
[DesignerParameter("End Date (date)")] PFDateTime EnDt)
{
int PackYears = 0;
int StDtYr = StDt.Year;
int EnDtYr = EnDt.Year;
PackYears = (EnDt.Year - StDt.Year);
return PackYears;
}
Thanks in advance!
Nicole
|
|
|
|
|
Without knowing more about your PFDateTime class, we can't really help you. Except to say why are you reading the year values into variables, and then ignoring them?
So start with the debugger.
Put a breakpoint on your return statement, and run your app in the debugger. When it hits the breakpoint, it will stop. Look at the values of PackYears (it'll be zero if you are right), at StDtYr and EnDtYr I'm just guessing that they are the same value...and probably zero.
So look at the inputs, and see what they contain.
We can't do any of that for you - we can't run your code!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Member 12008340 wrote: I can see while debugging that StDt.Year and EnSt.Year are integers with the correct year Then you should also be able to see the result of the calculation (what are the two years' values?). Are you sure this is the exact code you are debugging?
|
|
|
|
|
Good afternon
I am saepul from indonesia
i have question
where my question is
how sintax make automatic number where that automatic number
include year moth day and counter number
thanks very much
bestregard
saepul
|
|
|
|
|
Where are you getting your counter number from? The reason I ask this is because if you rely on a program to maintain a running total then you are probably going to have problems if the application crashes and restarts - you'll get duplicated numbers and it seems likely that this isn't what you want. Is the number meant to be an identity field in a database?
|
|
|
|
|
STOP SHOUTING.
Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Good Afternonn,
I am saepul from INDONESIA
I have question about view data from database,
where my question is
how sintax to call data from database to datagridview
where that sintax in class and that sintax i call on the form
thanks
bestregards
saepul
|
|
|
|