|
Thanks Christian,
I have just tried, but no expansion for the following code, why?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[100];
foreach (int p in array)
{
Console.WriteLine (p);
}
}
}
}
regards,
George
|
|
|
|
|
Dunno, but if that doesn't work, then I guess all you can do is look at the MSIL, or trust what the books tell you it expands to.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks Christian,
Could you do me a favor to see whether reflector works for you?
regards,
George
|
|
|
|
|
I would assume you don't see the full expansion because reflector is clever enough to recognise the IL pattern and display the c# that would have formed this. If you switch reflector to IL mode, you will see the raw IL that the C# compiles to. IIRC the C# compiler generates fields and things in IL with < and > signs in their names, which aren't allowed in c#, so there is no risk of a clash.
For example, try using the automatic properties feature of c# 3.0. If you look at it in reflector, you'll see in the IL it's generated a field called something like <myproperty>k_BackingField. Using the '<' signs mean that there's not a risk of the auto generated backing field clashing with any of your fields. But if you switch reflect to C# mode, it'll give you the original automatic property definition. This is reflector recognising the pattern and reversing it back to the original c',
The way to do it is write the C# for a foreach, and also the c# for a for loop, and then compare the IL generated by each.
Simon
|
|
|
|
|
Thanks Simon,
Do you mean C# will expand at IL level, not source code level?
regards,
George
|
|
|
|
|
Yes, you right the source code, will be compiled to IL, which is expanded out, as IL doesn't have a foreach statement.
what reflector does is convert that IL back to C#, but because reflector is quite clever it recognises the IL that is there performs the same functions as a foreach loop would, so when it converts it back to c#, it includes a foreach loop.
Simon
|
|
|
|
|
Thanks Simon,
Is there any online resource about IL language Spec or step by step learning resource? I feel IL is of much difference compared with native assembly language.
regards,
George
|
|
|
|
|
// Save the stream to disk
System.IO.FileStream newFile = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
TextWriter tw = new StreamWriter("d://fyp//images//saved images//info.txt");
// write a line of text to the file
tw.WriteLine("txtFName.txt*txtLName*IDTextBox*" + sSavePath + sFilename);
// close the stream
tw.Close();
on the
TextWriter tw = new StreamWriter("d://fyp//images//saved images//info.txt");
how do i specify the directory so that it will save to /images on my project folder? without specifying the exact location?
also on the
tw.WriteLine("txtFName.txt*txtLName*IDTextBox*" + sSavePath + sFilename);
the result on the notepad is that it writes exactly the same text to the notepad... how do i make it so that i'll save whatever I input onto my textboxes?
|
|
|
|
|
Thomas Toh wrote: how do i specify the directory so that it will save to /images on my project folder? without specifying the exact location?
The same as you do for the stream, server.mappath.
Thomas Toh wrote: how do i make it so that i'll save whatever I input onto my textboxes?
By removing the quotes. txtFName.Text + " " + txtLName.Text + " " + IDTextBox.Text
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
thanks.. the second one works..
however.. for the first one... is this the correct code?
TextWriter tw = new StreamWriter("Server.MapPath/images/info.txt");
also.. if i write 2 or more image files... it will overwrite the exisiting one... how do i make it so that it will write onto next line in my textfile
modified on Thursday, May 1, 2008 9:52 AM
|
|
|
|
|
Wow.
OK, what have we learned ? When a string is in quotes, it becomes a literal string, NOT code. Try copying the code you use above, and it will work.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
ok thanks...
also.. if i write 2 or more image files... it will overwrite the exisiting one... how do i make it so that it will write onto next line in my textfile
|
|
|
|
|
Hi guys. I need help please. I have two functions in my form. The one is an import function and the other is a validation function to match a field from the import. Now, I am writing the output of the import to a multiline text box but I have a problem with getting the return value from the matchinh function into the text box. Below the code.
This is the search Function:
private string SearchPcode(string inputStr)
{
string retval = "";
int pcode = 0;
if (!int.TryParse(inputStr, out pcode))
{
retval = "Invalid Integer!";
}
else if ((pcode >= 4731) && (pcode <= 6499))
{
retval = "Eastern Cape";
}
else
{
retval = "Invalid PostCode";
}
return retval;
}
This is the import Function:
private void btnOpenFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() != DialogResult.OK)
{
this.Close();
}
else
{
txtboxSelectFile.Text = openFileDialog1.FileName;
}
string filePath;
filePath = txtboxSelectFile.Text;
FileHelperEngine<CsvImp> engine = new FileHelperEngine<CsvImp>();
engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;
CsvImp[] res = engine.ReadFile(filePath);
if (engine.ErrorManager.ErrorCount > 0)
engine.ErrorManager.SaveErrors("Errors.txt");
foreach (CsvImp imp in res)
{
SearchPcode(imp.CallComments);
txtboxDisplay.Text += imp.CompanyCode + "\t" + imp.Caller + "\t" +
imp.Reference + "\t" + imp.Agent + "\t" + imp.CallComments + "\t" + Environment.NewLine;
}
}
I want to display the retval just before the Environment.Newline statement.
Any help please?
Thanks in advance
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
You just need to make sure you store the string that is returned. Currently you have:
SearchPcode(imp.CallComments);<br />
txtboxDisplay.Text += ...
In this case the string is returned, but isn't stored anywhere so it's lost. You need another variable to keep it in.
string searchResult = SearchPcode(imp.CallComments);<br />
txtboxDisplay.Text += imp.CompanyCode + "\t" + imp.Caller + "\t" +<br />
imp.Reference + "\t" + imp.Agent + "\t" + imp.CallComments + "\t" + searchResult + Environment.NewLine;
Or, you could use the method more like a variable and do this:
txtboxDisplay.Text += imp.CompanyCode + "\t" + imp.Caller + "\t" +<br />
imp.Reference + "\t" + imp.Agent + "\t" + imp.CallComments + "\t" + SearchPcode(imp.CallComments) + Environment.NewLine;
In this case the string will be returned, and then stored within txtboxDisplay.Text.
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
Brilliant SK Genius. Guess that is why the name?
It works. Thanks a ton matey.
Now I can peacefully again.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Kwagga wrote: Brilliant SK Genius. Guess that is why the name?
Not really, there are people who are much, much better than me.
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
Mr. T and Chuck Norris for example
|
|
|
|
|
Kwagga,
I hate to be "that" guy, but you have some common mistakes in those functions I'll gladly help you with
to start with string retval = ""; is bad in a few ways. In .NET strings can't change, each time you "change" it what really happens is a new string is made and the name (which is a point to the string location in memory) gets referenced to the new string. You can see this when you do the following:
string myString = "Hello World!";
string anotherString = myString What happens is that myString and anotherString are just pointing to the same location in memory.
So when you have
string retval = "";
retval = "my Return value";
return retval; you have useless string objects, which waste resources. Though the waste is very small normally, it can add up.
If you are looping though strings as in this pointless example:
string retval = "";
for(uint i=0; i < 10000; ++i)
{
retval += "a";
} You'd end up with 10000 unreferenced and unaccessible (wasted space) strings, with the 10001 string being your final accessable string pointed to retval
Though this is beside by point, there are better ways to do the above example using a StringBuilder which can change/be changed without making a copy of itself, but this is another topic all together.
You don't want these wasted resources as they can add up and waste memory before they get destroyed by the .NET Garbage Collecter (commonly called the GC)
You don't need to initialize an int's to zero either, even more so since the TryParse() function uses an "out" pass by reference instead of a "ref" means that method doesn't (and can't) use the initial value passed, so the input vale is scrapped and overwritten.
For your first function the following would be about 4 or 5 times better:
private string SearchPcode(string inputStr)
{
int pcode;
if (!int.TryParse(inputStr, out pcode))
{
return "Invalid Integer!";
}
else if ((pcode >= 4731) && (pcode <= 6499))
{
return "Eastern Cape";
}
else
{
return "Invalid PostCode";
}
}
This is using the common practice of "multiple exit points" some people don't like those, but I do
You could use the following if you don't like the muli exit method:
private string SearchPcode(string inputStr)
{
string retVal;
int pcode;
if (!int.TryParse(inputStr, out pcode))
{
retVal = "Invalid Integer!";
}
else if ((pcode >= 4731) && (pcode <= 6499))
{
retVal = "Eastern Cape";
}
else
{
retVal = "Invalid PostCode";
}
return retVal;
}
The reason I don't like this is that you have the extra string object, but one little string isn't that bad if it is easier for you to follow/program.
I've ranted enough at you I guess, but there at lots of these little optimizations you can do to the other function as well. As for your business code in the display layer. I'm not one of the people that think these are to be 100% separated and need to follow those "strict" (and at times HARD even for experienced coders) rules....
|
|
|
|
|
hy everyone!
is it possible to easyly convert date formats into different formats.
what i want to do is to convert a date format from english format to german format or vice versa.
i do have a function which looks like
public static string ConverFormat (string input, string format, string Cultureinfo)
so when entering this function input contains the date, format contains the formatstring, in which format to display (e.g. {"mm/dd/yyyy"}) and cultureinfo contains the cultureinfo (e.g. en-US) to which the input has to be converted to (english or german format).
so let's say input = 01.05.2008, format = {mm/dd/yyyy} and cultureinfo = en-US then the output should be 05/01/2008.
in here how to convert datetime.now into desired format???[^] i found how to format the type of how to display the date. but i want to switch it from dd.mm.yyyy to e.g. mm/dd/yyyy or mm.dd.yyyy (following the the format declared in parameter format).
do i have to do this by hand, meaning parsing the string and change it to the desired format or is there a chance to do this using default functions?
thanks!
Stephan.
modified on Thursday, May 1, 2008 6:25 AM
|
|
|
|
|
|
Please only use ISO 8601 formats -- yyyy-MM-dd
|
|
|
|
|
hi,
I have an application with a few custom controls in it.
My problem is, after loading, if I resize my application it takes a long time for all the controls to rearrange them selvs.
any idea how to make things faster?
tnx
leeoz
|
|
|
|
|
Add to the form constructor after the InitializeComponent() the following:
<br />
this.SetStyle(<br />
ControlStyles.AllPaintingInWmPaint |<br />
ControlStyles.UserPaint |<br />
ControlStyles.DoubleBuffer,true);<br />
|
|
|
|
|
Hi
Im trying to retrieve user properties from active directory.
It successfully gets the first required property, but after that all i get is
'NullReferenceException - Object reference not set to an instance of an object'
Why is this? Here is the code:
DirectoryEntry standardUser = new DirectoryEntry("LDAP://CN=" + txtUser.Text.ToString() + ",OU=Users - Standard Users,DC=mydomain,DC=com,DC=uk", "username", "password");<br />
string sUSN = standardUser.Properties["userPrincipalName"].Value.ToString();<br />
string nastsprofile = (standardUser.Properties["TerminalServicesProfilePath"].Value.ToString());
The bit which gets the userPrincipalName works fine. After that it all goes wrong.
|
|
|
|
|
Well, tha fact that you can get the userPrincipalName means that standardUser is fine, so the problem must be that
standardUser.Properties["TerminalServicesProfilePath"].Value is null. I think you might need a lower case 'T' or something.
I found this similar problem on t'internet HERE[^], where the apparent solution was to use lower case.
My current favourite word is: Bacon!
-SK Genius
|
|
|
|