|
Yes, it was intentional sealed, i don't see any chances that it will be become a base class someday, but if it will then we can just remove the sealed keyword. I just don't know if it is a good practice.
|
|
|
|
|
Well, yes, if you have the source, you could "un-seal" the class ... but ...
I suggest you consider the idea that you may be pursuing "micro-optimization" here, and that may have very little pay-off.
You might enjoy reading Eric Lippert's thoughts on micro-optimization in .NET: [^]
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Bookmark
I will gonna read it later.
Thanks
|
|
|
|
|
There are a few things you can do. BillWoodRuff gave a good example.
- the control variable is never used inside the function.
- Is there a reason to pass value as an object? In addition I would never use a keyword (value) as a variable name. (unless the autofill gave it that name of course)
- I never program multiple exit points in a function, often it is harder to debug. Better to set a boolean and return that at the end. (although that's more a matter of taste)
- Keep the { }. It will be easier to add a statement if a modification is needed.
- int.TryParse is a very good function. I also often use Convert.ToInt32, but make sure you know what it returns in case of null values or invalid integer strings/objects. (read MSDN doc)
- In some countries the decimal sign is not a ".", but a komma ",".
- You don't need _minValue or _maxValue you have int.MinValue and int.MaxValue.
- An object variable has an IsNumber() and ToInt() function? (not in version 4.5)
Hope this helps.
|
|
|
|
|
hmm about the decimal sign, its new to me that it is a comma to other country.
BTW, basically the initial value of _minValue and _maxValue is from the int.MinValue and int.MaxValue, but in some point it could be possible that i will limit the value the user can entered, and it was defined in the constructor.
private int _maxValue = int.MaxValue;
private int _minValue = int.MinValue;
private bool _IsAllowNull;
public ValidateInteger(bool isAlloweNull = true)
{
_IsAllowNull = isAlloweNull;
}
public ValidateInteger(int min, int max, bool isAlloweNull = false)
{
_maxValue = max;
_minValue = min;
_IsAllowNull = isAlloweNull;
}
while the IsNumber() and ToInt() is a custom extension.
Thank you
|
|
|
|
|
Personally I would put a mask on the control to only allow the entering of digits 0-9, then all you have to check is that the entry is not outside the max and min values.
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
Yes I can do this. but i can easily miss it.
thanks
|
|
|
|
|
I'd say it's not too bad. Drop all the elses, they are not needed as each condition returns out. That will remove some of the indentation.
If value is null and nulls are allowed, then it'll throw a null exception on the ToString(). The error message for the null check is wrong.
The IsNumber() and ToInt() functions are interesting, as in they don't exist on object. Extension methods? The assumption seems to be that the number is actually a string representation of a number, in which case first confirm it is a string, then do a int.TryParse to extract it...
Regards,
Rob Philpott.
|
|
|
|
|
yup the IsNumber() and ToInt() is a custom extension, basically the IsNumber() will identify the string if it can be a number(either a decimal,int and so on), while the ToInt() as you expected will convert a value to int.
BTW thank you, for your suggestion
|
|
|
|
|
A question on QA [^] from someone who knows the internal structure of a Type, but does not have a reference to the Type or an Interface the Type implements ... and who needs to cast the Type as Object to its original Type to access its internal field.
In other words: I have an instance of this Class cast to Object:
public class ExampleClass
{
public int ExId { set; get; }
public string ExString { set; get; }
public ExampleClass(int exId, string exString)
{
ExId = exId;
ExString = exString;
}
} And, my code does not contain the definition of this Class.
So I want to cast the instance of ExampleClass as Object to its Native Type. The thought occurred to me that it might be possible, since I know the internal structure of ExampleClass to "spoof" an Interface like this:
public interface IExample
{
int ExId { set; get; }
string ExString { set; get; }
} And, use it like this on the instance to get it back to ExampleClass Type:
IExample iexample = example as IExample;
This results in the value of 'iexample being set to 'null.
I also tried "spoofing" the Class, and using that:
public class xExample
{
public int ExId { set; get; }
public string ExString { set; get; }
public xExample(int exId, string exString)
{
ExId = exId;
ExString = exString;
}
}
xExample xexample = ExampleClassAsObject as (xExample); My conclusion is that this kind of "spoofing" is not possible in .NET, and, being foolish enough to ask why, I ask: why ? Security reasons ? A "spoof" is never "the real thing" ?
Just curious, thanks.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Try it with the dynamic keyword;
using System;
class ConsoleApplication
{
public class ExampleClass
{
public int ExId { set; get; }
public string ExString { set; get; }
public ExampleClass(int exId, string exString)
{
ExId = exId;
ExString = exString;
}
}
static void Main(string[] args)
{
object ec = new ExampleClass(1, "hello world");
dynamic ec2 = ec;
Console.WriteLine(ec2.ExString);
Console.ReadLine();
}
}
BillWoodruff wrote: Security reasons ? I think it is a technical "feature", a side-effect. Otherwise, dynamic would be a nice hole in that security.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks, Eddy, that's a use of 'dynamic I never would have thought of.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Your welcome ofc
I'd see it more as a bit of abuse; it is not a very nice 'solution', but it does the trick. Will explode as soon as someone changes the name of the property. Not during compile time, but during runtime. A compile-time check would be preferred.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The answer, as always, lies in the il. Specifically, you're looking at these two lines:
L_001a: ldfld class SpoofTest.ExampleClass SpoofTest.Class1::example
L_001f: isinst SpoofTest.IExample So, the code loads the instance and then tests it to see if it's an instance of your interface. Obviously, it's not an instance so the code would leave iexample as nulL, so this code
public Class1()
{
IExample iexample = example as IExample;
if (iexample == null)
{
System.Diagnostics.Debug.WriteLine("This does not compute");
}
} produces this IL
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
.maxstack 3
.locals init (
[0] class SpoofTest.IExample iexample,
[1] bool CS$4$0000)
L_0000: ldarg.0
L_0001: ldc.i4.1
L_0002: ldstr "Test"
L_0007: newobj instance void SpoofTest.ExampleClass::.ctor(int32, string)
L_000c: stfld class SpoofTest.ExampleClass SpoofTest.Class1::example
L_0011: ldarg.0
L_0012: call instance void [mscorlib]System.Object::.ctor()
L_0017: nop
L_0018: nop
L_0019: ldarg.0
L_001a: ldfld class SpoofTest.ExampleClass SpoofTest.Class1::example
L_001f: isinst SpoofTest.IExample
L_0024: stloc.0
L_0025: ldloc.0
L_0026: ldnull
L_0027: ceq
L_0029: ldc.i4.0
L_002a: ceq
L_002c: stloc.1
L_002d: ldloc.1
L_002e: brtrue.s L_003d
L_0030: nop
L_0031: ldstr "This does not compute"
L_0036: call void [System]System.Diagnostics.Debug::WriteLine(string)
L_003b: nop
L_003c: nop
L_003d: nop
L_003e: ret
}
Now, compare this to what happens if I change ExampleClass to implement IExample. The IL changes to this
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
.maxstack 3
.locals init (
[0] class SpoofTest.IExample iexample,
[1] bool CS$4$0000)
L_0000: ldarg.0
L_0001: ldc.i4.1
L_0002: ldstr "Test"
L_0007: newobj instance void SpoofTest.ExampleClass::.ctor(int32, string)
L_000c: stfld class SpoofTest.ExampleClass SpoofTest.Class1::example
L_0011: ldarg.0
L_0012: call instance void [mscorlib]System.Object::.ctor()
L_0017: nop
L_0018: nop
L_0019: ldarg.0
L_001a: ldfld class SpoofTest.ExampleClass SpoofTest.Class1::example
L_001f: stloc.0
L_0020: ldloc.0
L_0021: ldnull
L_0022: ceq
L_0024: ldc.i4.0
L_0025: ceq
L_0027: stloc.1
L_0028: ldloc.1
L_0029: brtrue.s L_0038
L_002b: nop
L_002c: ldstr "This does not compute"
L_0031: call void [System]System.Diagnostics.Debug::WriteLine(string)
L_0036: nop
L_0037: nop
L_0038: nop
L_0039: ret
}
You'll obviously notice, at this stage, that the isinst check is no longer there.
|
|
|
|
|
Thanks, Pete, that's a satisfyingly "deep" answer !
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
You're welcome my friend. I like going into the innards of the code, so a question like this was a golden opportunity for me.
|
|
|
|
|
Is there a way to get OAuth2 access token without prompting user for UserID an Password. I mean, from back end accept the authorization and authentication request silently without prompting Website?
|
|
|
|
|
Considering the website will probably NOT trust your users local account authority on their own workstations, no.
The entire reasons for entering a username and password is so that the user can prove to the system that they are who they way they are. The system has to have a "trusted authority" to validate those credentials against.
|
|
|
|
|
TcpClient tClient = new TcpClient("gmail-smtp-in.l.google.com", 25);
string CRLF = "\r\n";
byte[] dataBuffer;
string ResponseString;
NetworkStream netStream = tClient.GetStream();
StreamReader reader = new StreamReader(netStream);
ResponseString = reader.ReadLine();
/* Perform HELO to SMTP Server and get Response */
dataBuffer = Encoding.ASCII.GetBytes("HELO KirtanHere" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
ResponseString = reader.ReadLine();
dataBuffer = Encoding.ASCII.GetBytes("MAIL FROM:<youremail@gmail.com>" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
ResponseString = reader.ReadLine();
/* Read Response of the RCPT TO Message to know from google if it exist or not */
dataBuffer = Encoding.ASCII.GetBytes("RCPT TO:<senderemail@domainname.com>" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
ResponseString = reader.ReadLine();
if (GetResponseCode(ResponseString) == 550)
{
MessageBox.show("Mai Address Does not Exist !
Original Error from Smtp Server :" + ResponseString);
}
/* QUITE CONNECTION */
dataBuffer = BytesFromString("QUITE" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
tClient.Close();
// write a sub procedure for return status of email which is valid or not
private int GetResponseCode(string ResponseString)
{
return int.Parse(ResponseString.Substring(0, 3));
}
|
|
|
|
|
Not entirely sure, but I think their rationale is to prevents bots from grabbing valid emails.
Best,
John
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
If you mean "I can send mail to any address, even if it doesn't actually exist" then yes, you can. Most email systems do that and discard invalid addresses to prevent spammers and phishers from knowing which ones are valid and concentrating on those.
Nothing you can do about that!
But...if you send too many, you will almost certainly end up on a blocked domain list.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hello,
I have a winform application which has a treeview and listview with n number of columns. I want to reorder the columns but the treeview data is not getting interchanged. How do i trigger the event wherein on column reorder the row data is changed.
Currently on ColumnReorder event im swapping the childnode data based on olddisplay index and newdisplay index and storing it in childnode.tag. But im unable to trigger the drawnode event to display the reordered information.
For example if i have 3 columns say Folder, DateCreated and Size and when i drag and drop "DateCreated" column after "Size" i want the data in "DateCreated" column to be moved.
Thanks
|
|
|
|
|
If your intent is to re-build the TreeView based on re-ordering the columns in the ListView, I think that's not a good design choice because of the amount of work you'd have to do to re-build the TreeView node-structure.
If you absolutely have to have different views, I would use multiple TreeViews, each one built to match a given ListView column order; then I'd just display the TreeView that matches the current ListView column order. Of course, depending on how many items you display, how many columns, etc. that could be expensive in terms of memory consumption and time-to-build the TreeViews.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Hi,
Im supposed to have the design like that hence im using treeview and listview. How do i rebuild Treeview on reordering of columns. I have the new treeview data but im unable to trigger the event to write the new treeview data.
Thanks
|
|
|
|
|
To help you further with this, I'd need to know:
1. did you write the code that populates the ListView, and builds the TreeView Nodes based on the ListView ?
2. if you did write the code: please describe, in a general way, how you build the TreeView Nodes.
3. is the ListView being populated by being 'bound to a DataSource ?
4. Do you know how to write code that recursively parses a TreeView, accessing every Node ?
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|