|
From what I am aware of a System.String is a reference type, yes, but it's also immutable - i.e., the character sequence in it cannot be changed once it is created. Methods that appear to change a string only return a new string with the modifications made.
Note: everything below here is my interpretation of what is going on.
When you do something like this:
string a = "one";
string b;
b = a;
a and b will point to the same location for the string in memory - just like you expect with reference types. The catch is that because a string is immutable, as soon as you do this:
b = "two";
The CLR will now remove b's reference to a, create a new String object (with the character sequence "two") and assign that to b. a and b now point to two entirely different System.String's in memory. This process of sharing a reference until someone makes a change helps keep memory usage down whilst keeping a System.String inherantly immutable - two strings will point to each other until one of them changes. When one of them changes, two entirely seperate strings are created.
Even when you pass a string by reference (using ref ) you still aren't actually modifying the original string. All you are doing is creating a new System.String in memory somewhere and changing the ref'd variable's reference to point to this new string. The old string still exists in memory somewhere, but providing certain conditions are met it's up for garbage collection now.
<br />
public void Foo()<br />
{<br />
string one="two";<br />
Bar(ref one);
}<br />
public void Bar(ref string inStr)<br />
{<br />
inStr = "three"; <br />
}<br />
This space for rent!
My Blog
|
|
|
|
|
why I use TextWriter() to write xml file it will replace old file everytime? how to write without replace old data? I want to add some element to old file without replace one.
|
|
|
|
|
Hi,
Instead of using a TextWriter()-derived class to write the XML to the file (I assume you're using a System.IO.StringWriter or an equivalent, since System.IO.TextWriter is abstract) try using something like System.Xml.XmlDocument . That will load an entire XML document into memory for you and allow you to manipulate it at will. You can add your new element and save the document to disk again.
If you want to stick with a Stream, however, have a look at the System.IO.File.AppendText method. That will create a new StreamWriter that appends text to the file.
This space for rent!
My Blog
|
|
|
|
|
What is the syntax of overloading "[]" for a arraylist extended class?
Thanks
|
|
|
|
|
In C#, the indexer for the ArrayList class is the Item property, so you'll have to overload this.
www.troschuetz.de
|
|
|
|
|
you can use the new keyword if a method/accessor is not marked virtual.
eg
public class MyArrayList : ArrayList
{
public new object this[int i]
{
get{ return base[i]; }
set{ base[i] = value;
}
}
|
|
|
|
|
What you're looking for is not an operator ("[]"). It's called an indexer (as previously mentioned), and while ArrayList does expose an Item property, the property you'll need to overload (or override) is the this property :
e.g.
public object this[Guid objectKey] {<br />
get{...}<br />
set{...}<br />
}<br />
<br />
public object this[string objectName] {<br />
get{...}<br />
set{...}<br />
}
I'm not sure what your plan for this is, but you may also want to consider implementing the ICollection interface (probably the CollectionBase class) if you're working on making a strongly typed collection. It uses an ArrayList internally.
Hope this helps.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
hello friends,
i just want to know wheather their is any api in .Net,com etc for geting the open with dialog,as like we get in windows when we try to open any of the files with no extentions.
if there is any other implementation other than reading from the registry ,plz let me know.
regards
Deepak
deepak
|
|
|
|
|
|
Is it possible for me to somehow change the color of the Main Menu?
|
|
|
|
|
hello,
I am working on .Net framework in c# lang. and this
problem is related to Font Dialog class.The problem
i am facing is as below:
steps:
1)start an application containing a font dialog .
2)Install a new font while the application is running.
3)On applying the new font installed to any component
say textbox,the application throws an exception
with message:"Only true type fonts are supported and
this is not a true type font"
On debugging it is just after dialog.
if(ShowDialog()==DialogResult.Ok)
{
}
4)On restarting the application and appying the same
new font everything works fine.
How ever this is not the case with some of the
products in market eg Adobe Photoshop,Xml spy,Or
microsoftword..
Please suggest me regarding this.
Thanks and Regards
|
|
|
|
|
Hi To All
I have seen in almost all groups to my surprice nobody shown interest in SmartClient, So please do let me know if any of you are interested we can request a seperate group in code project.
Please do join me for this.
|
|
|
|
|
I'm not sure we need a seperate forum on CodeProject for Smart Client development. The existing C#, VB.NET and .Net Framework groups should be fine.
If the list of smart client questions grows then I'm sure the CP team will add a section, as SmartClients are as important as ASP.NET.
Michael
CP Blog [^] Development Blog [^]
|
|
|
|
|
Hi,
So I was given this data file, which i think is in binary format and i have to extract data from it starting at an offset of 1000. I tried opening the file in Wordpad and really have no clue how its encoded. I was able to mimic the way it was displayed in wordpad using a streamreader with a text encoding of utf7 then writing it to the console. Each value I'm trying to extract consists of 2 bytes and I have to convert this into a positive/negative integer. I tried many ways of doing this but was unable to get the right result. I noticed first of all, that the values i'm getting largely consist of zeros and all positives which is way wrong. The whole idea of binary files are so foreign to me and i have no clue where to begin on this. I really wish they had started us on C in school rather than java...
thanks in advance
|
|
|
|
|
Hi
I have a program which reads a xml schema file and also reads a input xm l file and if there is a element present in the schema which is also present in imput xml fle. it stores the vaues of input xml file for theat element intoa new xml file , while the other elements present in the schema are automatically getting values acccording to the datatype.
private void button2_Click(object sender, System.EventArgs e)
{
dataSet1.ReadXmlSchema(txtSchema.Text);//txtschema is the schema file
XmlDocument xdc = new XmlDocument();
foreach (DataTable dTbl in dataSet1.Tables)
{
object[] oValues = new object[dTbl.Columns.Count];
xdc.Load(@"C:\CDEV\generator\testdatagenerator\FIELDNAMES.xml");
int i=0, iIndex=0;
foreach (DataColumn dColmn in dTbl.Columns)
{
if(dColmn.ColumnName.ToString() == "billto_name")
{
iIndex = i;
break;
}
i++;
}
//INPUT XML FILE
xdc.Load(@"C:\CDEV\generator\testdatagenerator\billtoname.xml");
XmlNodeList nodeRows;
XmlElement Root = xdc.DocumentElement;
nodeRows = Root.SelectNodes("//billto_name");
for(int j = 0; j <= 200; j++)
{
int k = 0;
foreach (DataColumn dColmn in dTbl.Columns)
{
if(j == iIndex && j <nodeRows.Count)
oValues[j] = nodeRows.Item(i).InnerText;
else
{
switch(dColmn.DataType.ToString())
{
case "System.String":
oValues[k] = (string) "castle Hampers" + j;
break;
case "System.Int32":
oValues[k] = (int) 66 + j;
break;
case "System.DateTime":
oValues[k] = new DateTime(2004,01,30).AddDays(j + 1);
break;
case "System.Decimal":
oValues[k] = new Decimal(1900.99) + j;
break;
case "System.Int16":
oValues[k] = (short) 3256 + j;
break;
case "System.Int64":
oValues[k] = (long) 400 + j;
break;
case "System.Double":
oValues[k] = (double) 8888 - j;
break;
case "System.Single":
oValues[k] = (float) 4.5 + j;
break;
}
}
}
dTbl.Rows.Add(oValues);
}
}
}
I view the data's through a datagrid. But I get values only for the first field and the others are showing only null
santhosh
|
|
|
|
|
|
Hi
Now I had incremented k,But I find that in the first row, first value of the element(both element name same in schema and input xml) first value is from input value and other values for that columns are those specified for automatic insertion like castlehampers1, 2...etc.. and first row of other elements are null and from necxt row they are correct.
pls help me to do.
ie, my output was like this
--------------------------------------------------------------------------- -ie under the elemnet name "billto_name"(datatype-> string) the values as(james frank, castle hamper1, castle hamper2..etc.. up to 200)
under the element name "dividend" (datatype -> float) the values as (null, 5.5,6.5....)
under the element name "section"(datatype-> double ) the values as (null,8887,8886....)
-------------------------------------------------------------------------
u can see that it takes only the first value from input xml(ie, james frank is the value which is taken from input xml file) others like castle hamper1,2 are given by me in the program for a string datatype . And also the other values are correct from the second row and why it didnt assign a value for the first row (why its showing null).so pls help me to do this.
private void button2_Click(object sender, System.EventArgs e)
{
dataSet1.ReadXmlSchema(txtSchema.Text);//txtschema is the schema file
XmlDocument xdc = new XmlDocument();
foreach (DataTable dTbl in dataSet1.Tables)
{
object[] oValues = new object[dTbl.Columns.Count];
xdc.Load(@"C:\CDEV\generator\testdatagenerator\FIELDNAMES.xml");
int i=0, iIndex=0;
foreach (DataColumn dColmn in dTbl.Columns)
{
if(dColmn.ColumnName.ToString() == "billto_name")
{
iIndex = i;
break;
}
i++;
}
//INPUT XML FILE
xdc.Load(@"C:\CDEV\generator\testdatagenerator\billtoname.xml");
XmlNodeList nodeRows;
XmlElement Root = xdc.DocumentElement;
nodeRows = Root.SelectNodes("//billto_name");
for(int j = 0; j <= 200; j++)
{
int k = 0;
foreach (DataColumn dColmn in dTbl.Columns)
{
if(j == iIndex && j
|
|
|
|
|
|
I couldnt get the result.
Pls change my coding and sent . you can get my coding part in the previous thread.Pls help .
santhosh
|
|
|
|
|
How can I set the image property of a picturebox object at run time?
I have added to images (jpg) to my solution both with build action Embedded Resource, and I want to switch at run time which image is displayed. I can not use the ImageList because the image size exceeds 256x256 and is not the same for both pictures. (I might want to include more pictures in the future).
I also know that I can use the following code:
this.myPictureBox.Image = new Bitmap("C:\\myfolder\\myImage.jpg");
but if I use that code there better be an image at that location and that is not what I want. I want to embedd the image in the exe and then populate the image property of the picture box with the embedded images at runtime.
I thought a little more about this problem and I think I want to create an array of bitmaps and then load all the images into the bitmap array then I can use the following code to change the picturebox image property at runtime:
private void ChangeImage(int ImgIndex)
{
this.myPictureBox.Image = this.myBitmapArray[ImgIndex];
}
Well the problem still remains to find a way to load an image into a bitmap object. It is very easy to do if I specify the path and the filename but again that is not something I want to do.
I did find some information using the System.Reflection.Assembly class, but I could not quite figure out how to load the resource that is embedded.
Any assistance would be greatly appreciated.
Sincerely,
Einar
|
|
|
|
|
Hi Einar,
Check out this link on the Microsoft Newsgroups: howto load an embedded resource[^]
Basically, it involves this:
Assemly myAssembly = GetType().Module.Assembly;
Bitmap bmp = myAssembly.GetResourceStream("Namespace.Folder.File.Ext");
Replace the Namespace.Folder.File.Ext path accordingly. Of course, make sure your bitmap is actually embedded first!
Good luck.
This space for rent!
My Blog
|
|
|
|
|
I am looking for suggestions on a good way to visually layout database designs and class designs. I have seen in some articles where the database looks to be a screen shot from the SQL diagram and some articles show class designs.
|
|
|
|
|
I personally use a good ol' whiteboard. I invested in a 1200x900mm magnetic one a few weeks ago - I can't live without it now! However, a lot of people swear by Visio, as well.
If you're looking for a free alternative, you could try Dia[^]. Dia is an open-source diagram modelling tool which appears to be fairly good.
This space for rent!
My Blog
|
|
|
|
|
If you have a control on a windows form that has a ContextMenu enabled, and you would like to know WHICH control it is that the user has right-clicked, is it possible to pass the name of the control through to the menu click event so that it could process the control using that information? The sender object only retrieves the MenuItem object and not the name of the control that activated the menu click. How would I acquire the name of the control that activated the ContextMenu click event?
|
|
|
|
|
Like this:
MenuItem miSender = (MenuItem)sender;
ContextMenu cmSender = (ContextMenu)miSender.Parent;
TextBox tbSender = (TextBox)cmSender.SourceControl;
|
|
|
|