|
GravityPhazer wrote: There are situations, though, when you have to use string.Format() or custom implementations. Can you name one (or more) from the top of your head that hasn't been mentioned yet?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Ah sorry, withdraw that. I thought I had a problem using conditional with strings:
Write($"Test is {(isTest ? "enabled" : "disabled")}");
But I obviously just mixed up something with the brackets.
That, though, is something that's clearly some kind of disadvantage: Using something like this makes it kinda difficult to read ore more like difficult to find the error if you place any bracket wrong (or have any other kind of syntax issue). At least if you use a lot and/or complex expressions.
But I still prefer interpolation
I personally had to get used to it first, though. I think it's nicer to use than string.Format() and although I built a string extension method which let's me quick format a string, I think this is better to use. I think it's definetly an advantage that you actually see which variable or value you have at that spot. With format you just see the index there and have to check which value is actually being set (especially when you got >4 elements with an object array).
But like I said, in the beginning I forgot most of the time to first write the indicator sign $ , and thus had to go back - which is indeed more time consuming then. But that's just temporary.
|
|
|
|
|
Yep, I like it too, in general. Things like your example or if you have a deeply nested value then make it less readable again. That's why I still like my custom solution (not the first of its kind) better in those cases:
string text = "Test is {enabled}. Also {foo}.".XFormat(new
{
enabled = isTest ? "enabled" : "disabled",
foo = some.Really.Deep.Nested.Value[index]
});
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Well, I haven't ever thought about using anonymous classes for that.
I've written something that makes it easier (since I not only forget $ but also string.Format() at the beginning) by formatting at the end, like with your example. But using the same approach as the default format:
var myString = "Test value 1 is {0} and value two is {1}".FormatWith(value1, value2);
The cool thing is that thanks to ReSharper you can tag that method as formatting method, and thus it highlights the placeholder braces if you use that method on a string. Pretty neat!
But using anonymous types for that is a nice idea, as well. I should implement that, too
PS: I should mention I implemented that method far before anonymous types were supported.
|
|
|
|
|
GravityPhazer wrote: But using anonymous types for that is a nice idea, as well. I should implement that, too Unless you want to do it for kicks, you might want to wait for my article on that
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I don't have much time at the moment so you're article will likely be out before I can even start on that. Exams, deadlines and stuff are keeping me away from programming fun :-/
But writing my article was fun enough for this week and a good break from learning
|
|
|
|
|
GravityPhazer wrote: I love all C#6 features and use them quite a lot.
I love writing code that I think others in the future will have a hope of figuring out without jumping through loops to do so.
And I appreciate those rare occasions when I must maintain someone else's code were they were more focused on implementing the needed business functionality in a straightforward manner and where they actually took the time to think about real error conditions and how to maintain the code when it runs in production.
Of course none of that has anything to do with the language that it is written in. Applies equally to C#, Java, C++, Flavors of SQL, perl and even OS script languages.
|
|
|
|
|
Sascha Lefèvre wrote:
Do you have a project where fast string formatting is critical?
Can't ever recall where that ever was an issue ever.
Worked on a project where reading and parsing text fast was a concern. But turn out via profiling and production metrics that language specific IO was sufficient with some minor tweaks.
Sascha Lefèvre wrote: Now that we have string interpolation
For me I can't see that as anything but syntactic sugar that provides no real benefit. To be fair the only thing I work on is back end servers so consequently the vast majority of strings I usually work with are logging strings.
|
|
|
|
|
jschell wrote: For me I can't see that as anything but syntactic sugar Sure that, but
jschell wrote: that provides no real benefit to me improved readability is a great benefit. Especially when you have a format string with a couple more than just 2-3 placeholders where some of them even repeat the index-notation becomes really cryptic.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Sascha Lefèvre wrote: to me improved readability is a great benefit.
Only study I have ever seen that was even close to objectively measuring "readability" was one related to font choices in marketing material.
|
|
|
|
|
I can't find tutorials to understand
(Permissions ,Principal,AccessControl,Authentication)
|
|
|
|
|
Are looking for a tutorial for something specific (like cryptography or role based security)? System.Security contains plenty of classes which cater for different kinds of needs. Also, did you checked MSDN documentation if that helps you?
"You'd have to be a floating database guru clad in a white toga and ghandi level of sereneness to fix this goddamn clusterfuck.", BruceN[ ^]
|
|
|
|
|
There is an article on Codeproject that might help...
Understanding .NET Code Access Security[^].
You might have more luck finding more articles and/or tutorials if you pick a more specific part of the namespace - use the MSDN documentation to drill down into what is available. For example if you pick on Cryptography then this pops up - Walkthrough: Creating a Cryptographic Application[^]
O'Reilly publish a book called "Programming .NET Security" by Allen Jones and Adam Freeman - a free trial at Safari could get you started Programming .NET Security [Book][^] (but the trial only lasts 10 days and it's a bit pricey after that) - Disclaimer - I am nothing to do with Safari and I am not recommending nor condemning it's use.
modified 20-Jan-16 8:25am.
|
|
|
|
|
Im using the following code to fix the orientation of image taking into account the EXIF Orientation Tag
static void FixImageOrientation(Image srce)
{
const int ExifOrientationId = 0x112;
if (!srce.PropertyIdList.Contains(ExifOrientationId)) return;
var prop = srce.GetPropertyItem(ExifOrientationId);
var orient = BitConverter.ToInt16(prop.Value, 0);
prop.Value = BitConverter.GetBytes((short)1);
srce.SetPropertyItem(prop);
switch (orient)
{
case 1:
srce.RotateFlip(RotateFlipType.RotateNoneFlipNone);
break;
case 2:
srce.RotateFlip(RotateFlipType.RotateNoneFlipX);
break;
case 3:
srce.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case 4:
srce.RotateFlip( RotateFlipType.Rotate180FlipX);
break;
case 5:
srce.RotateFlip(RotateFlipType.Rotate90FlipX);
break;
case 6:
srce.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case 7:
srce.RotateFlip(RotateFlipType.Rotate270FlipX);
break;
case 8:
srce.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
default:
srce.RotateFlip(RotateFlipType.RotateNoneFlipNone);
break;
}
}
This code removes the EXIF Orientation Tag properly.
And saving the image works is i simply use **`img.save`**
But the app provides user the ability to select the format of the image.For that i use the following code
private void saveJpeg(string path, Bitmap img, long quality)
{
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
ImageCodecInfo Codec = this.getEncoderInfo(imgformat);
if (Codec == null)
return;
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(path + ext, Codec, encoderParams);
}
public string getimgext(string ccodec)
{
if (ccodec.Equals("image/png"))
{
return ".png";
}
else if (ccodec.Equals("image/jpeg"))
{
return ".jpg";
}
else if (ccodec.Equals("image/tiff"))
{
return ".tif";
}
else if (ccodec.Equals("image/bmp"))
{
return ".bmp";
}
else if (ccodec.Equals("image/gif"))
{
return ".gif";
}
else
{
return null;
}
}
private ImageCodecInfo getEncoderInfo(string mimeType)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType == mimeType)
return codecs[i];
return null;
}
When i save the image with `SaveJpeg` the image gets saved with wrong orientation.What im i doing wrong? Please help.
|
|
|
|
|
1. context: Windows Forms. last year I posted a question here GZip, and the code shown here is based on content in that thread: [^].
2. currently: I am successfully using GZip to serialize and de-serialize a very complex object.
3. the issue is: if I do two reads/de-serializations of the code I get the "being used by another process" file access error. so far research on the web has not solved the problem.
4. comparison with using WCF to serialize to 'xml: using this very standard code:
using (FileStream fs = new FileStream(filepath, FileMode.Create))
{
dcs.WriteObject(DataObject, this);
} The serialized object can be de-serialized any number of times without the error seen using GZip.
5. the GZip code:
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
public static DataObject DeSerialize(string filepath)
{
DataContractSerializer dcs = new DataContractSerializer(typeof(DataObject));
DataObject dto;
using (MemoryStream xmlStream = new MemoryStream())
{
using(GZipStream deCompStream = new GZipStream(File.Open(filepath, FileMode.Open),CompressionMode.Decompress, true))
{
deCompStream.CopyTo(xmlStream);
deCompStream.Close();
xmlStream.Position = 0;
}
dto = (DataObject)dcs.ReadObject(xmlStream);
xmlStream.Close();
}
return dto;
} thanks, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
modified 17-Jan-16 10:12am.
|
|
|
|
|
Bill, shouldn't you close the underlying stream as well?
using (MemoryStream xmlStream = new MemoryStream())
{
using (FileStream fs = File.Open(filepath, FileMode.Open))
{
using(GZipStream deCompStream = new GZipStream(fs, CompressionMode.Decompress, true))
{
deCompStream.CopyTo(xmlStream);
deCompStream.Close();
xmlStream.Position = 0;
}
dto = (DataObject)dcs.ReadObject(xmlStream);
xmlStream.Close();
}
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Alternatively just specifying false for the last ("leaveOpen") parameter of the GZipStream-c'tor.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Thanks, Sacha, turns out I just didn't take a close look at that flag, setting it to 'false makes everything work.
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
To avoid those non-descriptive true's and false's (non-descriptive as arguments) I usually use the named argument syntax for them or, alternatively, if it's my own method declaration, enums.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
thanks, Griff, looking at your code and mine I can't seem to see a difference, and I am, I believe, closing the XMLStream.
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
The difference is that your code opens the FileStream inside the GZipStream constructor call:
using(GZipStream deCompStream = new GZipStream(File.Open(filepath, FileMode.Open),CompressionMode.Decompress, true))
Where mine extracts that to a separate using block which closes and disposes it at the end.
using (FileStream fs = File.Open(filepath, FileMode.Open))
{
using(GZipStream deCompStream = new GZipStream(fs, CompressionMode.Decompress, true))
{
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi Griff,
Your code works.
I must be blind-as-a-bat tonight. fyi: I tried reversing the order of creating memorystream and gzipstream ... still using only two levels of 'using ... and got the same error.
Sascha also had a valid solution here: I was leaving the file open by my setting the Gzip calls 'leaveOpen flag to 'true.
But, your code adds an additional level of 'using that takes care of the "in-use" error independently of the value of the 'leaveOpen flag in the calls to GZip.
thanks, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
modified 18-Jan-16 2:43am.
|
|
|
|
|
Hi,
Can you help me ? I am try diable that MainForum run again form volumepanel. I want this:
When i am goto to top of screen the mainform show only ove volumepanel.
Here is code:
X = Cursor.Position.X;
Y = Cursor.Position.Y;
if ( Y == 0 ) {
if (( Application.OpenForms["volumepanel"] as MainForm ) != null ) {
MessageBox.Show("fdf");
} else {
volumepanel vpanel = new volumepanel();
vpanel.Show();
}
|
|
|
|
|
I'm sorry, but I think you need to be more specific.
Personally, I have no idea what you're trying to achieve.
Best,
John
-- Log Wizard - a Log Viewer that is easy and fun to use!
|
|
|
|
|
i am developing Dekstop Manager for disabled peoples, contolled with eyes.
If user goes to top of the screen:
X = Cursor.Position.X;
Y = Cursor.Position.Y;
if ( Y == 0 ) {
Exec this code:
volumepanel vpnelc = new volumepanel();
vpnelc.Show();
And show volumecontrol frame. The problem is that open in loop volumecontrol frame when user stay with mouse on top. I am need protect that show only one volumecontrol.
Here is captured the problem:
20160114 231834 - YouTube[^]
|
|
|
|