|
Removing your question after it has been answered is considered bad behavior!
Now other people cannot learn from the discussion thread.
|
|
|
|
|
I don't think he removed his question at all. It looked like this when I first saw it this morning.
|
|
|
|
|
Looking at the user's other messages, they're all the same - a vague question in the title, and a body containing nothing but a <pre> tag.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Only sure way to validate a email address is to send a email to the address with a link in it which the user then clicks on to validate the email. There are variations to this such as sending a key which the user then puts into a web site.
And unless you do that realistically you shouldn't attempt to validate at all. Not worth the time. As noted in a different response the valid form is complex and trivial validation will often be wrong (disallowing real addresses.)
|
|
|
|
|
Recently a question on QA (from a newbie) asked about the best way to selectively invoke a method in either a base class containing the method, or a class derived from the base class containing an identically named and identically functional method ... in another method that took as a parameter an instance of either the base class, or any of its derived types.
Consider the following classes:
public class BaseClass
{
public void ShowMe() {MessageBox.Show("BaseClass");}
}
public class DerivedClass1: BaseClass
{
public void ShowMe(){MessageBox.Show("DerivedClass1");}
}
public class DerivedClass2: BaseClass
{
public void ShowMe(){MessageBox.Show("DerivedClass2");}
} And, consider the following example using instnaces of the classes:
BaseClass baseClass = new BaseClass();
DerivedClass1 derivedClass1 = new DerivedClass1();
DerivedClass2 derivedClass2 = new DerivedClass2();
private void invokeClassMethod1(BaseClass theClass) {theClass.ShowMe();}
private void invokeClassMethod2(dynamic theClass) { theClass.ShowMe(); }
private void invokeClassMethod3(BaseClass theClass)
{
if(theClass is DerivedClass1)
{
(theClass as DerivedClass1).ShowMe();
} else if (theClass is DerivedClass2)
{
(theClass as DerivedClass2).ShowMe();
}
else
{
theClass.ShowMe();
}
} Obviously, 'invokeClassMethod1 will always invoke the method 'ShowMe in the BaseClass instance.
'invokeClassMethod2 uses 'dynamic declaration of the parameter Type, and "does the right thing" to invoke either the base class method, or the derived type method.
'invokeClassMethod3 does an identity check and invokes the "right" method.
Equally obvious is that this scenario calls for a restructuring of the code to declare the method in the BaseClass of type 'virtual, and to then over-ride the method in the derived classes which would then automatically invoke the right target.
The question is: what's the cost (performance, memory use, possible "risks: threading ? extensibility ? security ?) of using 'dynamic in this example compared to declaring the method in the BaseClass 'virtual and over-riding it ?
« I had therefore to remove knowledge, in order to make room for belief » Immanuel Kant
|
|
|
|
|
In a case like you I always going to see IL code - it can tell a lot about your code really looks like...
private static void invokeClassMethod1(BaseClass theClass)
{
theClass.ShowMe();
}
private static void invokeClassMethod2([Dynamic] dynamic theClass)
{
if (Program.<invokeClassMethod2>o__SiteContainer0.<>p__Site1 == null)
{
Program.<invokeClassMethod2>o__SiteContainer0.<>p__Site1 = CallSite<Action<CallSite, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "ShowMe", null, typeof(Program), new CSharpArgumentInfo[]
{
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}));
}
Program.<invokeClassMethod2>o__SiteContainer0.<>p__Site1.Target(Program.<invokeClassMethod2>o__SiteContainer0.<>p__Site1, theClass);
}
This is the IL code (decopiled to C#) you will get for your first two 'invoke' method. It is obvious that the dynamic approach will add more run-time handling (as the type is unknown at compile time)...
If you pass to the first 'invoke' method a series of classes that using virtual/override approach the calling address of the right function will be searched by the basic 'engine' of the object oriented programming (a search in the virtual method table of the actual type of the object), so no need for additional code in your application...
I did not measured the difference between the dynamic search and the object oriented look-up, but I have the feeling that OO will hit dynamic...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Thanks KEP; to the extent I can understand IL (very little), I get what you are saying.
In any case, from an "architectural" point of view I think it would be better to make the method 'virtual and use over-ride.
It might be interesting to look at the IL for the 'virtual+over-ride case ?
« I had therefore to remove knowledge, in order to make room for belief » Immanuel Kant
|
|
|
|
|
The case of the virtual/override has no special 'footprint' in the IL code. It is exactly the same as your first 'invoke' method...The reason is that the selection of the correct method to call done in lower level of the object oriented 'engine'. The selection is done using a virtual-method-table search, and that is part of the lower level OO mechanism. You can't see it in your code...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Kornfeld Eliyahu Peter wrote: The case of the virtual/override has no special 'footprint' in the IL code. It is exactly the same as your first 'invoke' method.
Wouldn't it be using the callvirt opcode instead of call ? It's not visible when you convert back to C#, but there is a difference in the IL.
The difference between call and callvirt can lead to some unexpected behaviour:
http://blogs.msdn.com/b/ericlippert/archive/2010/03/29/putting-a-base-in-the-middle.aspx[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Of course it is a callvirt! However C# always uses callvirt when calling instance methods! So other there is no difference between the two...
Take this code for instance:
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main ( string[ ] args )
{
BaseClass baseClass = new BaseClass( );
DerivedClass1 derivedClass1 = new DerivedClass1( );
DerivedClass2 derivedClass2 = new DerivedClass2( );
invokeClassMethod( baseClass );
invokeClassMethod( derivedClass1 );
invokeClassMethod( derivedClass2 );
BaseClassD baseClassD = new BaseClassD( );
DerivedClass1D derivedClass1D = new DerivedClass1D( );
DerivedClass2D derivedClass2D = new DerivedClass2D( );
invokeClassMethodD( baseClassD );
invokeClassMethodD( derivedClass1D );
invokeClassMethodD( derivedClass2D );
}
static private void invokeClassMethod ( BaseClass theClass )
{
theClass.ShowMe( );
}
static private void invokeClassMethodD ( BaseClassD theClass )
{
theClass.ShowMe( );
}
}
public class BaseClass
{
public virtual void ShowMe ( )
{
Debug.WriteLine( "BaseClass" );
}
}
public class DerivedClass1 : BaseClass
{
public override void ShowMe ( )
{
Debug.WriteLine( "DerivedClass1" );
}
}
public class DerivedClass2 : BaseClass
{
public override void ShowMe ( )
{
Debug.WriteLine( "DerivedClass2" );
}
}
public class BaseClassD
{
public void ShowMe ( )
{
Debug.WriteLine( "BaseClass" );
}
}
public class DerivedClass1D : BaseClassD
{
public void ShowMe ( )
{
Debug.WriteLine( "DerivedClass1" );
}
}
public class DerivedClass2D : BaseClassD
{
public void ShowMe ( )
{
Debug.WriteLine( "DerivedClass2" );
}
}
}
And see the IL code for the same:
1 .class private auto ansi beforefieldinit ConsoleApplication1.Program
2 extends [mscorlib]System.Object
3 {
4
5 .method private hidebysig static
6 void Main (
7 string[] args
8 ) cil managed
9 {
10
11
12 .maxstack 1
13 .entrypoint
14 .locals init (
15 [0] class ConsoleApplication1.BaseClass baseClass,
16 [1] class ConsoleApplication1.DerivedClass1 derivedClass1,
17 [2] class ConsoleApplication1.DerivedClass2 derivedClass2,
18 [3] class ConsoleApplication1.BaseClassD baseClassD,
19 [4] class ConsoleApplication1.DerivedClass1D derivedClass1D,
20 [5] class ConsoleApplication1.DerivedClass2D derivedClass2D
21 )
22
23 IL_0000: nop
24 IL_0001: newobj instance void ConsoleApplication1.BaseClass::.ctor()
25 IL_0006: stloc.0
26 IL_0007: newobj instance void ConsoleApplication1.DerivedClass1::.ctor()
27 IL_000c: stloc.1
28 IL_000d: newobj instance void ConsoleApplication1.DerivedClass2::.ctor()
29 IL_0012: stloc.2
30 IL_0013: ldloc.0
31 IL_0014: call void ConsoleApplication1.Program::invokeClassMethod(class ConsoleApplication1.BaseClass)
32 IL_0019: nop
33 IL_001a: ldloc.1
34 IL_001b: call void ConsoleApplication1.Program::invokeClassMethod(class ConsoleApplication1.BaseClass)
35 IL_0020: nop
36 IL_0021: ldloc.2
37 IL_0022: call void ConsoleApplication1.Program::invokeClassMethod(class ConsoleApplication1.BaseClass)
38 IL_0027: nop
39 IL_0028: newobj instance void ConsoleApplication1.BaseClassD::.ctor()
40 IL_002d: stloc.3
41 IL_002e: newobj instance void ConsoleApplication1.DerivedClass1D::.ctor()
42 IL_0033: stloc.s derivedClass1D
43 IL_0035: newobj instance void ConsoleApplication1.DerivedClass2D::.ctor()
44 IL_003a: stloc.s derivedClass2D
45 IL_003c: ldloc.3
46 IL_003d: call void ConsoleApplication1.Program::invokeClassMethodD(class ConsoleApplication1.BaseClassD)
47 IL_0042: nop
48 IL_0043: ldloc.s derivedClass1D
49 IL_0045: call void ConsoleApplication1.Program::invokeClassMethodD(class ConsoleApplication1.BaseClassD)
50 IL_004a: nop
51 IL_004b: ldloc.s derivedClass2D
52 IL_004d: call void ConsoleApplication1.Program::invokeClassMethodD(class ConsoleApplication1.BaseClassD)
53 IL_0052: nop
54 IL_0053: ret
55 }
56
57 .method private hidebysig static
58 void invokeClassMethod (
59 class ConsoleApplication1.BaseClass theClass
60 ) cil managed
61 {
62
63
64 .maxstack 8
65
66 IL_0000: nop
67 IL_0001: ldarg.0
68 IL_0002: callvirt instance void ConsoleApplication1.BaseClass::ShowMe()
69 IL_0007: nop
70 IL_0008: ret
71 }
72
73 .method private hidebysig static
74 void invokeClassMethodD (
75 class ConsoleApplication1.BaseClassD theClass
76 ) cil managed
77 {
78
79
80 .maxstack 8
81
82 IL_0000: nop
83 IL_0001: ldarg.0
84 IL_0002: callvirt instance void ConsoleApplication1.BaseClassD::ShowMe()
85 IL_0007: nop
86 IL_0008: ret
87 }
88
89 .method public hidebysig specialname rtspecialname
90 instance void .ctor () cil managed
91 {
92
93
94 .maxstack 8
95
96 IL_0000: ldarg.0
97 IL_0001: call instance void [mscorlib]System.Object::.ctor()
98 IL_0006: ret
99 }
100
101 }
See line 68 and 84 - both have callvirt event the second is not a virtual member...
I also found this...http://blogs.msdn.com/b/ericgu/archive/2008/07/02/why-does-c-always-use-callvirt.aspx[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
hi,
I want to execute mutiple sql select queries in ms access using c#. As per my knowledge what we can do with sql server, is we can separate the queries using semi colon (;) like [select * from table1 ; select * from table2] and we can fill mutiple data tables within a dataset with the help of data adapter. is there any way we can do the same in ms access.
we can not create stored procedures too in ms access so what is the way to populate multiple data tables in one go, by sending / executing multiple queries in ms access.
Thanks in advance
|
|
|
|
|
Not sure if this is the right place, but I didn't see any more appropriate place, so...
Is it possible to validate attributes with xsd?
For example (a slightly contrived example, but you'll get the idea):
<host ipAddr="" hostName="" />
Does not make any sense to have both ipAddr and hostname specified.
A google search seems to indicate that you might not be able to do this, but it seems like Visual Studio does it on XSD itself. If I specify xs:element with both ref and name, it'll tell me ref is not allowed.
I want to do exactly that.
|
|
|
|
|
|
As stated in another comment, this question belongs to XML/XSL.
It is not clear exactly what you want to do.
Do you want tot have two attributes that is mutual exclusive, so either ipAddr or hostName is present but not both?
|
|
|
|
|
Yeah. In my original post, I said it doesn't make sense to have both.
|
|
|
|
|
In this particular case you should not need to use two attributes, it should be sufficient with one.
You could use the Dns.GetHostEntry Method[^]
System.Net.IPHostEntry host = null;
host = System.Net.Dns.GetHostEntry("127.0.0.1");
host = System.Net.Dns.GetHostEntry("localhost");
However, your question was interesting so I did some quick research and it looks like XML Schema 1.1[^] might have what you need.
Unfortunately .Net doesn't yet support this version.
|
|
|
|
|
I did say in my original post that this was a contrived example, but it certainly wouldn't make sense to specify:
<host ip="237.152.11.17" name="localhost" />
because that's obviously not correct. In my less contrived example:
<Item minLevel="1" maxLevel="2" />
<Item levels="1,3,5" />
it doesn't make sense to allow:
<Item minLevel="1" maxLevel="2" levels="1,3,5" />
because min and max go together, but levels allows you to specify arbitrary levels.
|
|
|
|
|
Then I think you have to do your validation in code behind.
I was considering using xs:union, but that doesn't work in your case with three attributes.
It could work if you only have two attributes.
See xsd:union[^]
|
|
|
|
|
I looked at the link for union... looks kinda dumb imo... not you, I mean, the union command itself. Its just a more verbose / messier form of regex minus the regex. In the example, it makes your XML take either "54" or small, medium or large. But in the process, it adds ugly namespaces to your XML. Would be cleaner using the regex pattern. Ugh... from what I see, Microsoft has no intention of adding XSD 1.1 support to .NET. Assert would have done it. Oh well... guess I'll have to make the 3 params optional and validate it in the C#. Equally ugly.
Kinda weird that XSD manages to do it on itself. Is that like hardcoded in the validator or something?
If you put:
<xs:element name="blah" ref="blah2" />
Visual Studio will bitch at you that 'ref' cannot be present.
|
|
|
|
|
Maybe it is the example that is stupid, but I have never used union myself so I cannot offer anything better.
I guess it is XSD.exe that bitches about a mix of the attributes name and ref, not the schema itself.
But I get what you mean.
It would be nice if Microsoft supported this in the next .Net version. I have had cases where I wanted to check if a value where between min and max directly in the schema.
MS doesn't support XSLT 2.0 or XPATH 2.0 either and doesn't seem to have an official implementation plan.
|
|
|
|
|
Huh? You can check if a value is between min & max directly in the schema in quite a few ways.
I quit using XPATH once I discovered LinqToXML. Unless you use XPATH regularly, you spend an awful lot of time screwing around with the cryptic syntax to get what you want.
|
|
|
|
|
We have couple of projects (1 win forms and 1 web form) written in VB.Net. We would like to get those converted into C#. We would be using VS 2012 Professional for BUILD. I am already googling for such tools but would also like to take community's opinion/recommendation on any such highly recommended tools with highest precision/accuracy in converting code from VB.Net to C#. I am good with free and paid tools. Your pointers/suggestions will be highly appreciated.
Thanks in advance.
Regards,
Vipul Mehta
modified 28-Sep-14 7:26am.
|
|
|
|
|
Did you read the description at the top of this forum?
|
|
|
|
|
So under which section would this question fall under?
Regards,
Vipul Mehta
|
|
|
|
|
You could try Quick Answers, but there isn't a section that really caters for such questions.
|
|
|
|