|
Yes, but a widening conversion to varchar and comparison between two varchars are probably faster than a narrowing conversion to int with an error check and then comparing two ints.
|
|
|
|
|
Jörgen Andersson wrote: with an error check and then comparing two ints. It's probably more a cast than a conversion.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
|
In fairness to Microsoft, they have introduced TRY_CAST() and TRY_CONVERT() in recent times which are very helpful when dealing with this kind thing.
98.4% of statistics are made up on the spot.
|
|
|
|
|
How about creating a foreign key relationship between the tables?
Would that not prevent this issue from happening again?
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
GuyThiebaut wrote: How about creating a foreign key relationship between the tables?
I believe that would trade one problem for another; you would trade an invalid cast for a foreign key constraint exception, which might well roll back a whole transaction. Moreover, who knows what it would do to the view?
David A. Gray
Delivering Solutions for the Ages, One Problem at a Time
Interpreting the Fundamental Principle of Tabular Reporting
|
|
|
|
|
Exactly, which is why foreign keys can be useful as they will prevent the insertion of data into a database which does not meet certain criteria.
A foreign key constraint should not affect a view - I see it more as a gatekeeper that prevents problematic data from being added to a database, whether that is by insertion, deletion or update.
I am aware that many people do not use foreign keys, however I have seen them to help in making the developer's life easier by keeping problematic data out of a database.
Without foreign keys you can find yourself in the position where the client is complaining about the system behaving strangely when the reason for that behaviour is, for want of a better word, unsanitised data.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
GuyThiebaut wrote: have seen them to help in making the developer's life easier by keeping problematic data out of a database.
I agree with you 100%, and I rely on foreign keys for precisely that reason. Nevertheless, I think it's important to be aware that having one doesn't excuse the developer from handling exceptions. In the situation under discussion, you are trading one kind of exception for another, albeit, more useful, one.
I don't think it would affect the view, either, but I thought it prudent to allow for some technical detail that I might have overlooked. I usually don't construct any views until I have my foreign keys defined, so that I can leverage them to help create the views.
David A. Gray
Delivering Solutions for the Ages, One Problem at a Time
Interpreting the Fundamental Principle of Tabular Reporting
|
|
|
|
|
static object s_obj = new object();
then in some function,
void func()
{
object obj = new object();
s_obj = obj;
}
Honestly, I have never seen such a code before. When someone does this, what actually happens to the memory, statically allocated to s_obj, initially? It's left hanging on the air without any reference ?
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.
|
|
|
|
|
Depending on garbage collection the memory should be released, because the ref counter of the original object is set to zero in the func() assigment.
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
Thanks, But does this apply even to Static Variables?
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.
|
|
|
|
|
Yes; the object ("new object") is not static; the pointer-variable holding that object is.
Once another object is assigned to it, the other object is no longer referenced.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.
|
|
|
|
|
KarstenK wrote: the ref counter of the original object is set to zero
If it's .NET, there's no ref counter. Instead, the GC will detect that the object is no longer reachable. That way, circular references won't leak memory.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
From now on, I will use that as an insult. Isomorphic Git just sounds way better than poopy-head.
|
|
|
|
|
|
Nice. Exactly what I always wanted!
|
|
|
|
|
Yup. Sure beats sliced bread.
... such stuff as dreams are made on
|
|
|
|
|
I was thinking IoT Toaster. But that would work too!
|
|
|
|
|
|
Lol! That's what I am talking about!
|
|
|
|
|
I do a lot with ADO.net, and I use parameters as much as I can. Recently I was working on this method:
private static System.Data.IDbDataParameter
GetParameter
(
this System.Data.IDbCommand CMD
,
string Name
)
{
System.Data.IDbDataParameter result ;
if ( CMD.Parameters.Contains ( Name ) )
{
result = (System.Data.IDbDataParameter) CMD.Parameters [ Name ] ;
}
else
{
result = CMD.CreateParameter() ;
result.ParameterName = Name ;
CMD.Parameters.Add ( result ) ;
}
return ( result ) ;
}
It gets a parameter by name or creates one with the provided name if there isn't one already.
After a while I decided to change it to avoid the call to Contains (the getter and Add have to check for existence anyway) and use try/catch to detect the absence of the parameter:
private static System.Data.IDbDataParameter
GetParameter
(
this System.Data.IDbCommand CMD
,
string Name
)
{
System.Data.IDbDataParameter result ;
try
{
result = (System.Data.IDbDataParameter) CMD.Parameters [ Name ] ;
}
catch ( System.IndexOutOfRangeException err )
{
result = CMD.CreateParameter() ;
result.ParameterName = Name ;
CMD.Parameters.Add ( result ) ;
}
return ( result ) ;
}
And it was good.
Until today.
And Oracle.
Oracle.DataAccess.Client.OracleParameterCollection returns NULL when the parameter doesn't exist!
I had to revert back to the earlier code.
Edit: After a little more experimentation, I find that other ADO.net Providers I have handy also differ from Microsoft's lead -- four vendors, four outcomes. But, then again, MSDN doesn't say what should happen in this situation!
Anyway, I have since decided that the whole concept of the above code was a bad idea -- I know whether or not the parameter exists and I should act accordingly.
In any case, I rarely have to get a parameter by name -- it's a code smell.
Here, then, is an improved GetParameter (by Name), with no trying to Create and Add missing parameters -- I don't intend to call this when I know the parameter doesn't exist.
When a parameter doesn't exist, it is consistent in its behavior, so the caller can react effectively.
private static System.InvalidOperationException
GetParameterException
(
string Name
,
System.Exception Exception
)
{
Exception = new System.InvalidOperationException
(
"A Parameter with ParameterName '" + Name + "' is not contained by this ParameterCollection."
,
Exception
) ;
Exception.Data [ "ParameterName" ] = Name ;
return ( (System.InvalidOperationException) Exception ) ;
}
public static System.Data.IDbDataParameter
GetParameter
(
this System.Data.IDbCommand CMD
,
string Name
)
{
object result ;
try
{
result = CMD.Parameters [ Name ] ;
}
catch ( System.Exception err )
{
throw ( GetParameterException ( Name , err ) ) ;
}
if ( result == null )
{
throw ( GetParameterException ( Name , null ) ) ;
}
return ( (System.Data.IDbDataParameter) result ) ;
}
<rhetorical>
Why do Microsoft's implementations throw System.IndexOutOfRangeException ?
Do they simply iterate the collection until they find the item or hit the end?
</rhetorical>
Edit 2 :
Ca.Ingres.Client.IngresCommand throws System.ArgumentOutOfRangeException
InterSystems.Data.CacheClient.CacheCommand throws System.IndexOutOfRangeException
MySql.Data.MySqlClient.MySqlCommand throws System.ArgumentException
modified 17-May-18 1:56am.
|
|
|
|
|
Oh, you are using Oracle?
Here is your problem!
|
|
|
|
|
I'm interested in the design decision to switch to exceptions when the existing method calls were working. Exceptions can be very expensive in .NET. Was it for some new feature that couldn't be "un-wound" and had to be hard-stopped via exception? Premature pseudo-optimization that went awry?
|
|
|
|