|
When calling the increment operator, the object is assigned a new reference, that reference has the new values.
Example. We have a class IntWrapper
public class IntWrapper
{
public int IntValue
{
get;
set;
}
} and we want to use it like this
IntWrapper a = new IntWrapper();
IntWrapper b = a;
a++;
Console.WriteLine("a = {0}, b = {1}", a.IntValue, b.IntValue); Here are two possible implementations for the increment operator overload in our class, the second is the correct way
public static IntWrapper operator ++(IntWrapper instance)
{
instance.IntValue++;
return instance;
}
public static IntWrapper operator ++(IntWrapper instance)
{
IntWrapper result = new IntWrapper();
result.IntValue = instance.IntValue + 1;
return result;
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Your first sentence appears to be on-topic, but I'm not sure of the relevence of the rest.
None of the three C# books I have give guidance on overloading the increment and decrement operators. MSDN doesn't seem to either.
The Microsoft and ECMA specifications merely say it's not like in C++ (as in the quote I posted), and that's no help to me because I only dabbled in C++.
In my case, I expected it to be difficult, but because it is actually simple, it became difficult and confusing to me. "Think our minds must be too highly trained, Majikthise." -- Vroomfondel
MSDN and mainstream books should explain how simple it is rather than expecting the reader to assume it is.
"
When overloading the increment and decrement operators, do not set the value; simply return the new value, the compiler will take care of setting the value and returning the new or old value as appropriate.
return ( x + 1 ) ;
return ( x = x + 1 ) ;
"
P.S. I just checked my Rational struct and I see that I have it correct there.
|
|
|
|
|
Yeah, the compiler takes care of it, and it is unexpectedly easy if you're using value types [structs]. If you're using reference types then how to implement it so the results are what's expected gets more tricky... see the posts below.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Huh? Both a and b refer to the same instance, so shouldn't a++ affect b?
[EDIT] You're right. I can see why that's happening (x++ is compiled down to x = IntWrapper.Increment(x)), and I guess it fits in with the semantics of the increment operator (operand changes to a new value), but it certainly is confusing when dealing with reference types.
modified on Tuesday, February 24, 2009 2:32 PM
|
|
|
|
|
I know - this is a wierd one. I thought more about it straight after I posted but didn't edit the post at all because I wanted to see what the reaction was to confirm what I thought, or to flame me down!
Personally, I would never use ++ or -- on reference types. It's not that obvious how it will affect things. Those operators are about changing values so I keep them for structs only.
The main rule I leaned about operator overloading is if it's not 100% obvious what something will do and affect other objects - either document it very clearly or don't do it!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I don't hold myself responsible for any post I make after 7 pm.
|
|
|
|
|
|
I try not to wake up before 10AM but sure, whatever
(Actually for this entire year so far I have been up at 6 on work days which sucks)
|
|
|
|
|
hi,
how i can create a 2d graph in c# like the performance graph in the task manager?
|
|
|
|
|
The Graphics class has some methods like DrawLine() that allow you draw shapes during run time.
You can get a Graphics object by using this.CreateGrahpics() or making one yourself and assigning to the form later with this.CreateGrahpics() .
If you use it with the correct values, I suppose you can make a performance graph.
Of course that's just the graph, to create the background simply us the BitMap class to read a file and then use the Graphics object's DrawImage() method to display it.
You should use double buffering, which simply means drawing to a bmp in memory and
whne it's done, drawing the content on a 2nd BitMap and passing that to the Graphics object.
You can try make it work with the other methods of the Graphics class, ike DrawRectangle() or FillRectangle() .
|
|
|
|
|
Try using ZedGraph[^]. It's pretty good and should give you more than enough.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
I have a datagrid incliding: MedicineName, Contry, Quantity, .., Total.
When I want to type a word ( ex: "B" ) then appearing a list conntaint: "B1","B2","B6" ( note that "B1 B2 B6 " exist in list) then it allow to choose a item from the list. The list is disappeared and also that all has value which chosen.
Thank you very much!!
|
|
|
|
|
Hi
Is it possible to convert String var to String[]?
|
|
|
|
|
Putting the string as an array of that string?
string[] arr = new string[] { str };
Or splitting the string?
str.Split(' ');
Eslam Afifi
|
|
|
|
|
Hi
I used the following code to list all the folders in a given path:
Here sp is a string which contains the list of all the folders...
DirectoryInfo dire = new DirectoryInfo(sss);
if (dire.Exists)
{
String[] folder;
folder = Directory.GetDirectories(sss);
foreach (string foldername in folder)
{
FileInfo fil = new FileInfo(foldername);
sp = fil.ToString();
}
}
Now to list all the files in all folders contained in sp,is it right to
convert sp to string[] and proceed this way....it still gives me the files in
in only 1 folder
String[] gi = new String[] { sp };
foreach (String pil in gi)
{
DirectoryInfo di = new DirectoryInfo(pil);
if (di.Exists)
{
String[] files;
files = Directory.GetFiles(pil);
foreach (String filename in files)
{
FileInfo fil2 = new FileInfo(filename);
listBox2.Items.Add(fil2.ToString());
}
}
}
|
|
|
|
|
What you want to do exactly???
Do you want to display all the files name in the list control or folders Names??
|
|
|
|
|
Hi
I want to list all the folders in any drives and then the files
in each folder in a listbox control
I am able to list all the folders...
I tried with the following code to list the files in the folder
The folder contains 10 files but this code lists only 1 file from
that folder...The last foreach does not work properly...
What changes should i make?
private void ShowPath_Click(object sender, EventArgs e)
{
reader = new XmlTextReader("path.xml");
Form1 f = new Form1();
String sp="";
String[] gi;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text:
String s1;
s1 = reader.Value;
String ss = listBox1.SelectedItem.ToString();
String sss = ss + s1;
object o3 = (object)sss;
DirectoryInfo dire = new DirectoryInfo(sss);
if (dire.Exists)
{
String[] folder;
folder = Directory.GetDirectories(sss);
foreach (string foldername in folder)
{
FileInfo fil = new FileInfo(foldername);
sp = fil.ToString();
}
}
gi = new String[] { sp };
foreach(String pil in gi)
{
DirectoryInfo di = new DirectoryInfo(pil);
if (di.Exists)
{
String[] files;
files = Directory.GetFiles(pil);
foreach (String filename in files)
{
FileInfo fil2 = new FileInfo(filename);
listBox2.Items.Add(fil2.ToString());
}
}
}
break;
}
}
}
|
|
|
|
|
Are you using two list controls
one is for showing the list of folders and
the other for showing the list of files for selected folder from the folder list box??
|
|
|
|
|
Hi
I am using one listbox to list the drives in a system and a another
to list both the folders and the files..
I tried the same in 2 different listbox controls n that works well...
But my requirement is to list the files and folders in one listbox....
|
|
|
|
|
use following code which populates all the files and folder from the directory info path provided to this function
e.g call this function as
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\");
GetAllDir(dirInfo);
.
.
.
private void GetAllDir(DirectoryInfo directory)
{
FileSystemInfo[] dirContents = directory.GetFileSystemInfos();
foreach (FileSystemInfo fileSysInfo in dirContents)
{
if (fileSysInfo.Attributes.Equals(FileAttributes.Directory))
{
this.listBox1.Items.Add(fileSysInfo.Name);
DirectoryInfo dirInfo = new DirectoryInfo(fileSysInfo.FullName);
GetAllDir(dirInfo);
}
else
{
this.listBox1.Items.Add(" - "+fileSysInfo.Name);
}
}
}
Hope this works for you!!!!!
|
|
|
|
|
Hi,
The above code will list the files only in C:\ drive...
If i want it for any other drive i should change it manually
in the code....
In my code i have listed all the drives in a listbox control so
that i can make a choice at the run time...
Can you give some suggestions about this?
|
|
|
|
|
in event handler of the liust where you have listed the drive
write this code
DirectoryInfo dri = new DirectoryInfo(listBox1.SelectedItem.ToString());
GetAllDir(dri);
|
|
|
|
|
I am in need of some help. I have written the following code to save entries in a datagrid to the database, my code looks like this:
UseWaitCursor = true;
this.Validate();
this.accountTypeBindingSource.EndEdit();
ServiceReference1.Service1Client proxySaveBankAccountTypes = new WBGTS.Client.ServiceReference1.Service1Client();
var bankAccountTypeChanges = (WBGTS.BusinessLogicLayer.dsWBGTS)this.dsWBGTS.GetChanges();
if (bankAccountTypeChanges != null)
{
if (proxySaveBankAccountTypes.SaveAccountType(ref bankAccountTypeChanges))
{
var bankAccountTypeRows = from row in this.dsWBGTS.AccountType
where Convert.ToBoolean(DataRowState.Added)
select bankAccountTypeChanges;
this.dsWBGTS.Merge(bankAccountTypeChanges);
this.dsWBGTS.AcceptChanges();
}
}
UseWaitCursor = false;
The problem is that when I add somthing to the datagrid it always shows the -1 number and the new record, after closing the form and opening again the record is fine but if you add a new entry it shows duplicates again!
I am trying to build a foreach loop into this method but I am having difficulty converting dataset to datarow.
Thanks!
Illegal Operation
|
|
|
|
|
Illegal Operation wrote: but I am having difficulty converting dataset to datarow.
A dataset is a collection of datatables. A datatable has a collection of datarows.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
I have a simple batch file script here that runs during log on and log off that writes a rolling CSV file of each user and which computer they are on. Just for fun, here's what I am running...
LOG ON SCRIPT:
for /f "Tokens=2 Delims=[]" %%i in ('ping -n 1 "%computername%"') do set IP=%%i
rem The following line creates a rolling log file of usage by workstation
echo Log In: %TIME% %Date% %USERNAME% >>\\lsdnas3\Logs\Computers\%COMPUTERNAME%.log
rem The following line creates a rolling log file of usage by user
echo Log In: %TIME% %Date% %COMPUTERNAME% IP=%IP% >> \\lsdnas3\Logs\Users\%USERNAME%.log
LOG OFF SCRIPT:
for /f "Tokens=2 Delims=[]" %%i in ('ping -n 1 "%computername%"') do set IP=%%i
@rem The fololowing line creates a rolling log file of usage by workstation
@echo Log Off: %TIME% %Date% %USERNAME% >> \\SERVER\Logs\Computers\%COMPUTERNAME%.log
@rem The following line creates a rolling log file of usage by user
@echo Log Off: %TIME% %Date% %COMPUTERNAME% IP=%IP% >> \\SERVER\Logs\Users\%USERNAME%.log
Well, I want to first choose computer or user logs... Got that figured out (duh) but how do I get a dropdown that will list each file so I can open it as a data source and display it? I know how to open and read the files, and all that boring stuff.
I am sure I will figure it out via a bit of research, but I thought asking might help someone else looking for a similar solution in the future.
Matt
|
|
|
|