|
Hello,
Nouman Bhatti wrote: to be more safe am.Items[a].ToString()
I'm not working with the StreamWriter to often so I'm currious.
What does it make more safe to pass the object as string?
All the best,
Martin
|
|
|
|
|
Yeah... I think the ToString() method is implicitly called.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Hi
Is there any easy way to draw Digital Signal by using pixel level drawing in C#?
|
|
|
|
|
have you considered looking at WPF in .NET 3.0 for this ? I am assuming by digital signal you are implying something like a Sine or Cosine wave?
|
|
|
|
|
You probably mean Digital Signature? Just look at the system.drawing namespace.
Input would just need to associate the device (whatever it is) to defined events in your objects.
|
|
|
|
|
Hi all,
What is more efficient/better coding practice:
1. Copying objects using the new keyword and a copy constructor
2. Using a Copy member routine which essentially does the same thing as the copy constructor (except we don't actually allocate memory for it using the new keyword).
My gut tells me that (1) is most likely to be more inefficient (since memory has to be allocated and the 'old' value of the object has to be deallocated) but I'd like to confirm this.
cheers!
|
|
|
|
|
jozsurf wrote: Copying objects by value
You mean a deep copy? i.e. all contained objects wether they are value types or reference tpyes are copied? Or a shallow copy where the references to reference types are just copied rather than the values?
jozsurf wrote: 2. Using a Copy member routine which essentially does the same thing as the copy constructor (except we don't actually allocate memory for it using the new keyword).
You mean a static method that does the copy by passing by value?
I'm not very clear about what your suggesting here. If you give some more details I'll be happy to help
|
|
|
|
|
Hi,
if you have one instance of a class, and you suddenly need a second instance that initially
has the same value, you can go either way:
- instantiate the class with what you call a "copy constructor", i.e. a regular constructor
that takes its own type as an input argument and copies values from it (shallow copy);
- or create an instance method that creates a "clone" (=again a shallow copy), by first
instantiating a new instance, then copying values into it. Several CLI classes offer
such a Clone method, e.g. Array and ArrayList
I havent encountered a Copy method yet, one that takes two objects and copies values
from one into the other (DirectX has some Copy methods, but IMO they should have been called
Clone instead).
The cost for both approaches would be the same; they both create a new object and
initialize it. Nowhere in this an object gets deallocated.
Hope this helps.
|
|
|
|
|
Thanks both for your replies. Apologies for being unclear, as my class consists only of primitive/value types (i.e. bools, structs), I only need to do a shallow copy (I suppose a deep copy could be implemented by some form of reflection?).
The following snippet of code is what I'm trying to do:
<br />
<br />
public class SimpleClass<br />
{<br />
public SimpleClass ()<br />
{<br />
a = false;<br />
b = 0;<br />
}<br />
public SimpleClass (SimpleClass s)<br />
{<br />
a = s.a;<br />
b = s.b;<br />
}<br />
public Copy (SimpleClass s)<br />
{<br />
a = s.a;<br />
b = s.b;<br />
}<br />
public bool a;<br />
public int b;<br />
}<br />
<br />
and so my original question was: is there a performance difference between doing
<br />
SimpleClass a = new SimpleClass();<br />
foreach (SimpleClass y in ListOfSimpleClasses)<br />
x.Copy(y); <br />
and
<br />
SimpleClass a = new SimpleClass();<br />
foreach (SimpleClass y in ListOfSimpleClasses)<br />
a = new SimpleClass(y);<br />
Hope this is a little clearer.
cheers!
|
|
|
|
|
Hi,
When taking your examples literally (except for x.Copy which I read as a.Copy),
there is a performance difference, since the second case creates a lot of new objects,
whereas the first does not.
But none of them make any sense: the first copies all existing elements of ListOfSimpleClass
into a single one, one after another without using it at all; the second creates a lot
of objects, each one a copy from a list element, again without using them.
If you need the objects to exist next to each other, the second case is the right one;
if you need the objects only one at a time, there is no need to do the "new..." stuff;
if you only need to read the values anyhow, there is no need to do a Copy at all: just
read the members of y itself.
Hope this helps.
|
|
|
|
|
Apologies for the lousy example. I was trying to give an 'abstract' example and was a bit rushed for time. Guess it's better to spend a bit more time giving a proper example rather then screwing about with 'abstract' (ultimately incomprehensible) examples
What I need this for is to save some application settings as well as be able to load up default settings. If using simple assignment (=), which copies the reference rather then the value, we run the risk of changing the default values which should really be a static, constant value.
1. Load up default values
m_settings = SavedData.DefaultSettings;
2. Change the settings
3. Save the settings
SavedData.Settings = m_settings;
I'm sure you can see how I ended up changing both SavedData.Settings and SavedData.DefaultSettings, and indeed have made both data structures point to one and the same thing!
And so I realised that I needed to copy-by-value; which brought up my original question. I thought it would be a bit weird to do a m_settings = new SettingsClass(SavedData.DefaultSettings); everywhere I want to do a value copy; of course there is also the potential of a difference in performance (although in my case probably negligible, which is why I wanted to bring it up to check).
cheers!
|
|
|
|
|
Hi,
m_settings = SavedData.DefaultSettings;
is quite misleading, it seems to copy a lot of settings, but since it really is a ref type
its just copying the reference as you pointed out. This does not feel good.
Of course, if you have two labels (label1 and label2) on a form, then label1=label2;
also does not change any of the labels, it simply throws away the ref to one of them.
m_settings = new SettingsClass(SavedData.DefaultSettings); is a big no no for me, it does
not reveal the intention.
m_settings = SavedData.DefaultSettings.Clone(); is what I would do, it is much more intuitive.
It resembles list2=list1.Clone(); when you need a copy of an ArrayList so you can modify
list2 while keeping list1 intact.
As I have pointed out in a previous reply, the Clone() method is a combination of
a "new" statement and a copy operation (together, you might call that a copy constructor,
but AFAIK that is not a regular notion in C#)
I am not quite sure I understood it all, the following points are still unclear to me;
I did not need the answers to answer your question, but a better understanding might
(or might not) allow me to propose another approach altogether.
1. the settings (either actual, or default) are stored in a class (SettingsClass) ?
2. you need at least two instances of it, one for defaults, one for actuals ?
3. a ref to both instances, and a lot of other stuff, are stored in SavedData ?
4. you want to be able to modify the default settings ?
5. where did you obtain the default settings ? I guess from a file ? XML ?
6. are you going to save everything (all of SavedData) at once and in one location ?
7. if default settings can be changed, what is the meaning of settings ?
8. is SettingsClass just a list of value types, or does it also contain references
(e.g. strings) ?
9. do you need more than one clone ? I would expect it contains all settings, so you
start of with the defaults, copy once, then modify at will, but your message seems
to suggest you often need a copy ??
Cheers
|
|
|
|
|
Thanks for your reply, Luc. I wasn't aware of the Clone method and I'm afraid that I've probably reinvented the wheel by introducing a Copy method.
1. the settings (either actual, or default) are stored in a class (SettingsClass) ?
Yes.
2. you need at least two instances of it, one for defaults, one for actuals ?
Yes. As we want to be able to revert back to the default settings at any time.
3. a ref to both instances, and a lot of other stuff, are stored in SavedData ?
Correct.
4. you want to be able to modify the default settings ?
Nope.
5. where did you obtain the default settings ? I guess from a file ? XML ?
It's actually hardcoded in the setting class default constructor.
6. are you going to save everything (all of SavedData) at once and in one location ?
Yes the 'current' version of SavedData is written to an XML file at the end of the session. The default version of SavedData is simply recreated at the start of each session.
7. if default settings can be changed, what is the meaning of settings ?
8. is SettingsClass just a list of value types, or does it also contain references
(e.g. strings) ?
Just value types (primitive types and structs). Funny you should mention strings there, although I think my confusion with strings is to do with the fact that strings behave like value types sometimes (i.e. with the "=" operator and the .Equals() method)
9. do you need more than one clone ? I would expect it contains all settings, so you
start of with the defaults, copy once, then modify at will, but your message seems
to suggest you often need a copy ??
Well I may need to copy it many times i.e. the user may choose to revert to default, change something, revert back to default, change something, revert back to default...ad infinitum. So my local copy may be one of three values: a modified version, the default version or the saved version but it has to be independent of the default and saved references.
Hope I understood your questions correctly.
|
|
|
|
|
Hi,
as the default settings are hardcoded and not obtained by parsing an XML file
(hence cheap to reproduce), seems to me the following scheme would do just fine:
1. have class "Settings" with a single constructor, returning a Settings object with
everything set to its default value.
2. do settings=new Settings(); when you need the default settings;
3. do settings=new Settings(); followed by whatever you like, when you want modified
settings
4. do again settings=new Settings(); when you need to revert to the default settings.
In doing so, you dont see any Copy() or Clone() operation; you simply start from the
default, and modify what needs being modified.
One slight modification could be to provide a Clear() method, which sets all
settings in a Settings instance to their default values (obviously now the
constructor would simply call Clear() itself). This allows you to reuse a Settings
object, if you consider this relevant.
[Edit: I guess Reset() is a better name]
One other idea would be to turn Settings into a struct (assuming you stick with
values and structs, again containing only values and structs, etc ad infinitum).
Now settings1=settings2 would really copy everything.
Nevertheless, I do prefer the former approach, because I like "new Settings()";
it shows you get a new object with settings, and it does not limit the members to value types.
(in my apps a lot of settings tend to be strings).
Hope this helps
-- modified at 23:42 Tuesday 17th July, 2007
|
|
|
|
|
Thanks for your suggestions and help Luc!
|
|
|
|
|
Memo to self, unrelated to this thread:
fprintf stops at the first null.
|
|
|
|
|
Hello everyone,
I was wondering if it is possible to give a Version Number to a Window Application and then prior to installation check to see if that version has already been installed. Then base on the result of this make either fresh installation or replace the old version with the new one?
I would appriciate if you given me some hint where to start or point me to a tutorial. Thank you very much and have a great day.
Khoramdin
|
|
|
|
|
Assembly asm = Assembly.LoadFile(System.IO.Path.GetFullPath("Path to your assembly"));
if (asm != null)
{
Console.WriteLine(asm.GetName().Version);
} As for setting the version number, you will have to do it in your AssemblyInfo.cs file.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Hello Vikram,
Thanx for the help. I was wondering how this code is going to check to see if there is a previous version installed during the installation of the Windows Application?! I am just a little bit confused. Could you be kind to elaborate on this? Unless I am missing something here!
Thank you very much and have a great day.
Khoramdin
|
|
|
|
|
As to where your app has been installed (after all, the user *could* install it in C:\Temp\Khoramdin's App), you will have to save that information in the registry during your installation. Once you know where it's been installed, you can use the sample I provided to determine if the client's version is the latest.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
If you use the versioning stuff in AssemblyInfo.cs and use the Visual Studio Setup Projects then it'll handle it all for you.
The VS Setup stuff isn't to flexable though and can turn into quite a pig if you want to do anything more advanced.
The VS Setup Projects are based on MSI, you can read up more on that if you want to use it to it's full extent.
If your doing it all manually then what you need to do is create a registry entry to give the location of the installed app. Your setup can then get the version number, or the app could include the version number in the registry too so the installer doesn't have to bother.
I'v also heard good things about NSIS[^], but I've not played with it so you'd have to rtead up and see if it supports what you want.
|
|
|
|
|
as per the previous poster if you use VS Setup project to do the previous installed version detection is very easy. Once you have created a setup msi project
If you access the Properties pane (F4) of the setup msi project in solution explorer, there is a property called RemovePreviousVersions, you can set this to True or False. Also there are two other properties you might want to look at which are the UpgradeCode and the Version.
Another way to do installation is WIX- Windows Installer XML. Alot of people are using this as well to generate MSI setups as it offers more flexibility on customizing the MSI.
|
|
|
|
|
Hi,
I have a DataGrid and two of the columns returned from the query are DateTime data types. I expect one of the columns to show just the Date while the other to have the Date and Time. However, the latter also shows only the Date. How do I fix this?
TIA.
-- modified at 20:50 Monday 16th July, 2007
I am a SysAdmin, I battle my own daemons.
|
|
|
|
|
In SQL, you may write like that
SELECT date1,date2 from YourTable
So, you can fix like that.
SELECT CONVERT(NVARCHAR(8),date,101),date2 from YourTable
then, you will get only date for 1st column without changing anything on UI.
|
|
|
|
|
Hi Michael,
I didn't realize it is to be done in the SQL script. This is exactly what I'm looking for. Big Thanks!
I am a SysAdmin, I battle my own daemons.
|
|
|
|
|