|
The hex thing is just a funny rule about "maybe out of range depending on how you look at them" integer literals, here's a simpler remedy:
int red = unchecked((int)0xFFFF0000); Still annoying because it needs the "unchecked" (as if I didn't know I was casting something out-of-range, that's the entire reason for casting!), also annoying (but for different reasons) are:
int red = 0xFFFF << 16;
int red = ~0x0000FFFF;
int red = -0x00010000; .. of course none of those generalize to arbitrary colors
Or perhaps more reasonably, but requiring a time machine: ToArgb and friends should have worked with uint . Problem solved, colours are more unsigned than signed anyway (I mean when do you ever think of white as -1?), inb4 "but CLS compliance".
BTW putting (un)checked around a function call doesn't do anything. It changes the codegen for primitive operations in its "scope", so it's not something that can be imposed on a callee.
|
|
|
|
|
Thanks, Harold,
Right now, the only way I can get back the "full" Color structure ... with fields 'NamedColor, etc., is to do a look-up in the KnownColor enumeration 'Values collection for matching integer values ... and that may result in more than one match : for each match the 'Key will be the full Color structure.
Will post code on request.
Seems like there should be an easier way to do that !
cheers, Bill
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
|
|
|
|
|
Are you trying to serialize the Color ? That's the only time I can think of that you'd need to do this.
The obvious solution would be to combine IsKnownColor[^], ToKnownColor[^] and FromKnownColor[^]; and also IsNamedColor[^], Name[^] and FromName[^].
public struct ColorDetails : IEquatable<ColorDetails>
{
private ColorDetails(KnownColor knownColor, string name, int argb)
{
KnownColor = knownColor;
Name = name;
Argb = argb;
}
public KnownColor KnownColor { get; }
public string Name { get; }
public int Argb { get; }
public static ColorDetails FromColor(Color color)
{
if (color.IsKnownColor)
{
return new ColorDetails(color.ToKnownColor(), null, color.ToArgb());
}
if (color.IsNamedColor)
{
return new ColorDetails(0, color.Name, color.ToArgb());
}
return new ColorDetails(0, null, color.ToArgb());
}
public Color ToColor()
{
if (KnownColor != 0)
{
return Color.FromKnownColor(KnownColor);
}
if (Name != null)
{
return Color.FromName(Name);
}
return Color.FromArgb(Argb);
}
public override int GetHashCode()
{
return Argb;
}
public override bool Equals(object obj)
{
return obj is ColorDetails && Equals((ColorDetails)obj);
}
public bool Equals(ColorDetails other)
{
return Argb == other.Argb;
}
public static bool operator ==(ColorDetails left, ColorDetails right)
{
return left.Equals(right);
}
public static bool operator !=(ColorDetails left, ColorDetails right)
{
return !left.Equals(right);
}
}
ColorDetails cd = ColorDetails.FromColor(Color.Red);
Color clr = cd.ToColor();
bool ismatchEq1 = Color.Red.Equals(clr);
bool ismatchEq2 = Color.Red == clr;
bool isknown = clr.IsKnownColor;
bool isnamed = clr.IsNamedColor;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard,
Yes, I'm messing around with creating a Color extension for Yoshifumi Kawai's MessagePack open-source serializer: [^].
The speed and memory efficiency of MessagePack (which has optional built-in LZ4 compression) is, imho, remarkable.
I came up with something similar to your code, and implemented the required MessagePack 'Formatter and 'Resolver structures, but it had incredibly poor (slow) performance. So, I fell back on just making a simple class that saves a Color as either an integer, or a KnownColor name (string): this (excerpt) will give you an idea of what's going on:
[MessagePackObject]
public class MPColor
{
[Key(0)] public Int32 _colorAsInt32 = -1;
[Key(1)] public String _colorName = String.Empty;
public MPColor()
{
}
public MPColor(Color color)
{
MColor = color;
}
[IgnoreMember]
public Color MColor
{
set
{
if (value.IsKnownColor)
{
_colorAsInt32 = -1;
_colorName = value.Name;
}
else
{
_colorName = String.Empty;
_colorAsInt32 = value.ToArgb();
}
}
get
{
if (String.IsNullOrEmpty(_colorName))
{
if (_colorAsInt32 == -1)
{
return Color.Empty;
}
return Color.FromArgb(_colorAsInt32);
}
return Color.FromName(_colorName);
}
}
} This is quite fast and memory efficient. Since the KnownColor enumeration includes System Colors, that removes the need to store one aspect of state.
Tomorrow, I will study your code in depth, possibly try it out with the required 'Resolver and 'Formatter components in MessagePack.
As ever, really appreciate your quality responses !
cheers, Bill
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
modified 3-Jul-17 9:56am.
|
|
|
|
|
i am printing a string through printer as the string length is more it is not getting printed properly so i have tried to split the string and the printing but it is showing error at run time
error:ex = {"Index and length must refer to a location within the string.\r\nParameter name: length"}
for (int i = 0; printstr != null; i++)
{
if (limit >printstrlength)
{
printstr = actualstring.Substring(startindex, printstrlength);
e.Graphics.DrawString(printstr, f1, Brushes.Black, new Point(x, y));
break;
}
else
{
printstr = actualstring.Substring(startindex, limit);
e.Graphics.DrawString(printstr, f1, Brushes.Black, new Point(x, y));
}
y += 20;
startindex = limit + 1;
limit += 40;
}
|
|
|
|
|
What it is saying is that when you try and do the Substring, the start index (or the start index plus the length of the bit you want to extract) must fit within the input string, and the numbers you have passed do not fit that criteria.
Use the debugger to look at the string and the values you pass to Substring and you should see which is wrong, and that should lead you to why it's wrong.
We can't do that for you - we don't have access to your strings!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
startindex = limit + 1;
limit += 40;
You are increasing limit by 40 each time round the loop. But you are also using this value to specify the length parameter in your call to actualstring.Substring(startindex, limit); , so sooner or later it will exceed the length of the remaining characters in the original string. You need to use a calculated value for the length, which starts at 40 but checks how many characters are still to be printed, each time round.
|
|
|
|
|
I have tried this but i didn't get the error you mentioned.
private void button1_Paint(object sender, PaintEventArgs e)
{
string printstr = "the string length is more it is not getting printed properly so i have tried to split";
string actualstring = "i am printing a string through printer as the string length is more it is not getting printed properly so i have tried to split the string and the printing but it is showing error at run time ";
int printstrlength = printstr.Length;
int limit = 10;
int startindex = 0;
int x = 2;
int y = 5;
for (int i = 0; printstr != null; i++)
{
if (limit > printstrlength)
{
printstr = actualstring.Substring(startindex, printstrlength);
e.Graphics.DrawString(printstr, button1.Font, Brushes.Black, new Point(x, y));
break;
}
else
{
printstr = actualstring.Substring(startindex, limit);
e.Graphics.DrawString(printstr, button1.Font, Brushes.Black, new Point(x, y));
}
y += 20;
startindex = limit + 1;
limit += 40;
}
}
|
|
|
|
|
That is because your code is incorrect. Start with the actualstring length and see what happens.
|
|
|
|
|
i my generating the qrcode using ONBarcode.dll but in this sometimes some default text are getting printed on qr code as it is not free. Can anyone suggest any dll file for generating qr code and there should not be any licence for that.
|
|
|
|
|
Did you know that one of the best tools a developer can have is the ability to work out how to search information sources for themselves and apply critical thinking to solve problems? By doing this, I worked out that you should run a query that looks a bit like this[^]. This gives you many, many potential sources for your answer.
This space for rent
|
|
|
|
|
how we can adjust the size of qr code while printing through printer
|
|
|
|
|
We can't answer that question as it stands - we have no idea how you are printing the QR code: what package(s) you are using, where you generate the QR code from, how you send it to the printer - anything at all really, except that QR codes, printing, and (probably) C# code are involved.
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work from.
So show us the relevant code fragments, explain what happens when you use them, tell us exactly what you want to happen. We can't help you without that!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi ,
I have well formatted .sql queries file, it contains insert/updates queries. I need to validate each query data in database before execution to database and this i need to do for every single query using C#.net collection.
if records are 1 million then is it feasible to read every DML statement and validate against database tables? what is the best possible approach using C#?? please advise.
modified 27-Jun-17 14:31pm.
|
|
|
|
|
What do you mean by "validate".
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Validate definition #1: Validate that the query will or will not return something. Not possible unless you duplicate the database and then run the query on the second data store.
Validate definition #1: Validate the syntax. Presumably you are using TSQL (MS SQL Server) I know for a fact that one could not fully 'validate' PSQL (Oracle) without running it against the database. The same, in some situations would exist with TSQL. There are code libraries that allow for SQL parsing which in turn leads to some form of validation. However, that is a limited form. For example it could validate that column names were of the correct form but not whether they exist or not. You could expand the parser to provide entity name checking however that requires that you have a confirmed up to date source of entity names (must match target database.)
|
|
|
|
|
I want to validate if the data which I am going to Update is already exist in DB or not and i can check it using ifexist().
But The issue with multiple DML statement may be upto 1 million records so do i need to keep checking in foreach loop for such big iterations in C#?
|
|
|
|
|
If you are just updating the data, why do you need to check if it exists or not? Using a WHERE clause in your SQL statement is the way to confine it to data that is already present. Alternatively, if you need to insert missing records and update others, check if your database uses something called an UPSERT query. As the name suggests, this query updates or inserts depending on whether or not the query condition can be satisfied. As you haven't said what database engine you are using, I'm going to link to an example in SQL Server to give you some ideas. SQL Server – How to write an Upsert using MERGE | My Adventures in Coding[^]
This space for rent
|
|
|
|
|
Sure thanks for direction Pete.
|
|
|
|
|
You're most welcome.
This space for rent
|
|
|
|
|
So a restatement of your original question to provide the actual problem.
You have a large number set of data that originates from outside of the database.
You need to process that data either by creating new records or updating existing ones.
So to start with, at least a few years ago (probably 10) universally it was always much more efficient to use database specific command line tools to batch process data. So you don't use C# at all. Although your C# might be used to initially produce the file which is then used for the batch processing.
As the other poster noted you database might support syntax that will do either an update or an insert. However if that is not the case then you can load the data into a different table and then, in the database itself (not C#) process the data to move it into the correct table(s).
You can also do further research by using "batch", "large" and "records" along with your actual database to seek other solutions.
|
|
|
|
|
Hi,
I created my regex like the following:
Regex regexEET = new Regex(".*?EET.*?");
But this one also detects "eet".
I was wondering how can I modify it to only search for uppercase "EET".
Thank you.
|
|
|
|
|
That should only match strings containing EET in upper case. Please show the actual content that you are matching against?
|
|
|
|
|
When I run your regular expression, I only get matches for EET. You haven't specified any regex options have you?
This space for rent
|
|
|
|
|
Oh my! You guys are right!
Regex regexEEST = new Regex(".*?EEST.*?",
RegexOptions.IgnoreCase |
RegexOptions.CultureInvariant |
RegexOptions.IgnorePatternWhitespace |
RegexOptions.Compiled);
I didn't notice
RegexOptions.IgnoreCase
Thank you, your comments were very helpful.
|
|
|
|