Ghost Errors With Generics And Nullable Guids?
Mar 31, 2011Paste the below into a C# Project and all is fine:
public class Test
{
public delegate void RandomEventHandler<T>(T newValue);
[code]......
Paste the below into a C# Project and all is fine:
public class Test
{
public delegate void RandomEventHandler<T>(T newValue);
[code]......
I've got a bunch of web page content in my database with links like this:
<a href="/11ecfdc5-d28d-4121-b1c9-1f898ac0b72e">Link</a>
That Guid unique identifier is the ID of another page in the same database.
I'd like to crawl those pages and check for broken links.
To do that I need a function that can return a list of all the Guids on a page:
Function FindGuids(ByVal Text As String) As Collections.Generic.List(Of Guid)
I've run across some VB.NET code that explicitly creates three GUID constants and uses them in a class's ComClass attribute. I've written COM-aware classes in the past just by checking the "Make COM-Visible" and "Register for COM interop" options in the project options. Is this explicit code simply unnecessary, or is it doing something above-and-beyond what those two options do? Here's a snippet:
<ComClass(MenuHandler.ClassId, MenuHandler.InterfaceId, MenuHandler.EventsId)> _
Public Class MenuHandler
Public Const ClassId As String = "A2204623-A902-44d4-B524-FDFFCD176E53"
[Code].....
I have 5 errors that come up every time I run my project They are all "A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll"
I have try catch blocks around every piece of code I have written, and it does not seem to catch them, so I opted to catch the appdomain.UnHandledException.
That does not seem to catch them either If I step through the code line by line (several hundred lines so I hold down f8) it does not make the error...
Only when I run full speed.
I can only figure that I am accessing something maybe before an object is completely drawn, however how in the heck would anyone suggest I go about finding it, and why will my global event handler not catch them?
I tried to create a ghost text using Labels over the Textboxes. I am using VB.Net2005. I accomplished this with this code:
Public Class frmDataEntry
Private Sub PhantomTextLastName()
If txtLastName.Text = "" Then
[Code].....
Is there any way to reduce this code, so that when I try to add another Textboxes I'll never have to retype a bunch of codes. I have basic knowledge n using Module and Class
I've been trying to make up a Ghost Text Box, so that it shows some 'ghost' text when its contains nothing.
What i did was inherit from the normal textbox, and use OnPaint to show the ghost text, the User paint is switch on/off depending on if there is any text in the textbox.If i start (in the design) and add some text it all works as i expect at runtime, but the text does not get shown at design time (for some reason)
If i clear the text at design time, then it shows the ghosted text at design tim. At runtime the ghosted text is shown, but when you click in the box and hit a key, the key is shown in bold, which is odd as the font is not in bold.
[Code]...
I have a .net application that uses customerrors web.config module to display meaningful messages for errors. It works without any issues for 500 errors/exceptions caused by non-ajax and ajax components (updatepanel). However, in a scenario where updatepanel's asynchronous request times out, there is no error raised at all. I was able to see the timeout in firebug and come up with a solution that would at least display the error message as an alert and then redirect the user to the 500 error page using javascript but it's not quite doing what the rest of the application does in case of an unhandled errors like these. I basically just want everything to go through "LogEvent" mechanism so based on the severity of the error, it does the necessary work.This 500 error page doesn't have anything in the Server.GetLastError() for these timeout scenarios. Is this an expected behaviour? Can it be changed so I do have access to these timeouts in Server.GetLastError() OR maybe just run this error through "LogEvent" mechanism? Is there a better/more graceful way to handle this issue?
Below is my code to give you an idea, not exactly what I have in my application but pretty close.
Web.Config
<customErrors mode="On" defaultRedirect="~/Errors/ErrorUnknown.aspx" redirectMode="ResponseRewrite">
<error statusCode="500" redirect="~/Errors/Error500.aspx" />
</customErrors>
[code]....
I accidently wrote some code today that was like this[code]...
I immediately noticed the issue, but I had already hit the run button. It compiled successfully, I ran it through to the section and it threw an exception.
You can't do this in C#, it gives a compile error "cannot convert from 'int?' to 'int'".
Is there an 'Option Explicit' type switch that I can turn on to ensure that this sort of error does not occur again?
In my program I have an interface iGraphable that contains two properties: Abscissa and Ordinate. Then I have an xxxx class (actually more than one) implementing iGraphable and a ListOfxxxx class implementing BindingListView(Of xxxx).To draw graphs I have a Graph class with a property called Data whose type is BindingListView(of iGraphable).Why have I a cast exception when I pass a BindingListView(Of xxxx) to the Data property.
View 3 RepliesI have the a function that is declared like so: Public Sub Modify(Of SIMType As {New, DAOBase})(ByVal obj As SIMType)
I also have a class called Products which is declared like so:
Public Class Products Inherits DAOBase
So as you can see, if I were to call this function like so:
Modify(Of Products)(new Products())
This would not be an issue. The issue actually arises when I try to cast the object being past in to its real type. For example: both do not work. I get a Value of type SIMTYPE cannot be converted to IMS.Products error. Im assuming this is because I am using generics. Is there a way to adjust my function to allow for a casting operation like I am trying to do? In the end, what I need is a reference of the actual type (Products in this case) to the object.
I have a function called Modify. It is delcared like so:Public Function Modify(Of SIMType As {New, DAOBase})(ByVal obj As DAOBase) As Boolean
You can see that this function is generic. It takes as a paramer a object that is a DAOBase or subclasses of DAOBase.Inside the modify function there is a call like so:
DAOToGP(obj)This is where the polymorphism comes into play. There are four or so subclasses I have created of DAOBase. I have written a DAOToGP() for each of these types. So in the Modify() function, when it calls the DAOToGP(obj), polymorphism should kick in and it should call the correct implementation of DAOToGP() depending on the type that I pass into Modify().
However, I get the following error:Error 20 Overload resolution failed because no accessible 'DAOToGP' can be called without a narrowing conversion:'Public Shared Function DAOToGP(distributor As Distributors) As Microsoft.Dynamics.GP.Vendor': Argument matching parameter 'distributor' narrows from 'SierraLib.DAOBase' to 'IMS.Distributors'.'Public Shared Function DAOToGP(product As Products) As Microsoft.Dynamics.GP.SalesItem': Argument matching parameter 'product' narrows from 'SierraLib.DAOBase' to 'IMS.Products'. C:Usersdvargo.SIERRAWOWIRESDocumentsVisual Studio 2010ProjectsSIMDev_2SIMIMSDVSIMLibGPGPSIMRunnerRunnersRunnerBase.vb 66 39 IMS
I am kind of at a loss here. I am not sure why it cant figure out which function to call.
I'm compiling a VB.Net 2.0 app (created in VS2008) using msbuild, and now I've added a generic return type, it's giving me the following:
Warning: Type library exporter
encountered a generic type instance in
a signature. Generic code may not be
exported to COM.
Having just spent ages removing all of the previous warnings, I don't really want to add a new one. Any idea how to get rid of it (aside from not using generics)?I don't know what details I'd put in the attribute, or what number to put in the project-level ignore list.
I'm trying to reduce code bloat, reduce errors and simplify codebehind by use of generics. In this case I'm applying generics to declaration of persistable properties. Persistance is implemented by My.Settings. Here's the code so far.
[Code]...
What are the situations and their associated benefits of using Generics over Inheritance and vice-versa, and how should they be best combined?I'm going to try to state the motivation for this question as best I can:I have a class as shown below:
[Code]...
Now suppose I have a repository that takes an InformationReturn argument, that has to strore different fields in a DB depending on the type of Info object T is. Is it better to create different repositories each for the type T is; one repository that uses reflection to determine the type; or is there a better way using inheritance capabilities over/with generics?
The codes below are exactly the same, except that one is C# and the other one is VB.Net.C# compiles just fine, but VB.Net throws the warning:
Interface 'System.IObserver(Of Foo)' is ambiguous with another
implemented interface 'System.IObserver(Of Bar)' due to the 'In' and
'Out' parameters in 'Interface IObserver(Of In T)'
Why does VB.Net show the warning and not C#? And most important, how can I resolve this problem?
Obs: I'm using .Net Framework 4 with Visual Studio 2010 Ultimate.
VB.Net Code:
Module Module1
Sub Main()[code]......
Names of entities have been altered to protect their identities...
I've created a class called AnimalSearch(Of AnimalType As Animal(Of Int32))
Public Class AnimalSearch(Of AnimalType As Animal(Of Int32))[code]...
I'm not sure if this is possible or not. I have a number of different classes that implement interface IBar, and have constructors that take a couple of values. Rather than create a bunch of almost identical method, is it possible to have a generic method that will create the appropriate constructor?
private function GetFoo(Of T)(byval p1, byval p2) as List(Of IBar)
dim list as new List(Of IBar)
dim foo as T
' a loop here for different values of x
foo = new T(x,p1)
list.Add(foo)
' end of loop
return list
end function
I get:
'New' cannot be used on a type parameter that does not have a 'New' constraint.
I have a VB6 project with about 100 custom collection classes which I want to convert to VB.Net. A typical example would be something like.
Class CAccounts
Private m_Accounts As New Collection
Public Sub Add(newItem As CAccount)
m_Accounts.Add newItem, newItem.IdKey
[code].....
All of the collection classes in the project use this standard approach. However, not all the properties/methods of the collection classes are actually used. Most of the collection are used in "for each" loops. Keyed access using the string key is quite common. Keyed access by index is much less common.
Ideally I'd like to take a standard approach to converting these classes. I don't really want to have to review each collection and it's usage to consider whether I need a List, Dictionary, etc. Some of these collection contain 100,000 objects, and some will only contain 10. However, on the other hand I don't want to cause performance problems by using a more complex structure where a simpler option would do.
Sticking with the old style Collection. So, it would be relatively easy to convert to VB.Net But, I'd rather move to the more modern structures.Have CAccounts Inherit KeyedCollection(Of String, CAccount). Fortunately most of the classes held in the collections do have the key as part of the class (eg CAccount.IdKey above). This seems to work well. However, relatively few classes will access the colelction by numeric index. So, perhaps this is overkill if I only want keyed access by the string key?Have CAccounts Inherit Dictionary(Of String, CAccount) for the classes where I don't need access by numeric index. The problem I have with this is that all the existing "for each" loops are like "for each account in accounts". I don't want to have to change all these occurences to something like "for each account in accounts.Values". Although perhaps I can get round this by changing the default property?Have CAccounts Inherit MyCollection(Of String, CAccount), where MyCollection is my own bespoke collection. This seems a bit too much hard work.
I'm working with some XML representations of data instances.I'm deserializing the objects using .NET serialization but something in my soul is disturbed by having to write classes to represent the XML.[code]
View 4 RepliesThis is my problem:
<System.Runtime.CompilerServices.Extension()> _
Function CastAs(Of TSource As TTarget, TTarget)(ByVal array As TSource()) As TTarget()
Return Global.System.Array.ConvertAll(array, Function(src As TSource) DirectCast(src, TTarget))
End Function
I have a Class that is defined as
Public MustInherit Class Entity(Of T As Entity(Of T))
And various classes derived from it. I would like to have another class accept all of the derived objects as a property, but I cannot seeem to find a workable syntax. If it were a parameter of a sub, I could say
Public Sub Foo(T As Entity(Of T))(TheEntity As T)
I can't seem to get that to work for a property:
Public Property E(Of Entity(Of T))() As Entity(Of T)
Gives me "Type parameters cannot be specified on this declaration"
Public Property E() As Entity2008(Of T)
Gives me "Type Parameter T does not inherit from or implement the constraint type ..."Is there an acceptable way to do this or am I stuck with using a Set Sub and a Get Function?
Suppose I have an interface called IParseable(Of TParsed, TUnparsed) which requires two functions:[code]Is there a way that I can restrict TParsed and TUnparsed to be numeric types (for which operations like "*" and "+" are already defined)?The problem is that, when I try to implement my interface and define one of the functions, e.g.:[code]VS throws an error saying the "*" is not defined for TUnparsed. I understand that, since TUnparsed could be anything, but is there a way to restrict my generic such that, say, TUnparsed could only be Double, Integer, Long, etc? To require Control to be a TextBox (or maybe I don't understand that very well either). But, anyway, any idea or am I way off track? Just trying to get a hang of these interface thingies and generic types.
View 2 RepliesI am trying to write a generic function that generates an Xelement based on a list of objects, and a property of the object[code]....
View 3 RepliesSo when i build or click debug on my program. No errors come up and its smooth. So i decide to publish my programme. However. I come up with a bunch of errors during the publishing.
Here they are:Error 1 Cannot publish because a project failed to build. 1 1 Simple CALC
Error 2 Unable to copy file "binReleaseSimple CALC.exe.manifest" to "binReleaseapp.publishApplication FilesSimple CALC_1_0_0_0Simple CALC.exe.manifest". The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
I know this is a stupid question maybe, but what is the naming standard for generics?Of t or Of TEntity or Of..it doesn't really matter?I see IQueryable(Of T) but then DBSet(Of TEntity).
View 3 RepliesI have two generic lists of type T. Both lists contain same type, and I'd like to create a third list (or a filtered version of list 2) based on the items in list two that do not exist in List 1, based on the ID of each item.Each list holds a "Package" object, which has an ID property.Right now I mocked up the code using For Each loops, which I know is horrible (the Big O is constant time) so I'd like a more efficent method. this code is in VB per project requirments, but I prefer C# - so either code sample would work for me.
Private Sub RemoveStockPackagesFromSelection()
Dim p As Package
Dim packageList As List(Of Package) = New List(Of Package)
[code]....
I have the following C# code. Here the validations are kept outside the class to satisfy Open - Closed Principle. This is working fine. But the challenge is - the validations are not generic. It is specific to employee class (E.g DateOfBirthRuleForEmployee). How do I make the validations generic for all objects (DateOfBirthRuleForAnyObject).
Note: Make Generic <==> Make Type-Independent
Note: I have NameLengthRuleForEmployee validation also. New validation may come in future.
class Program
{
static void Main(string[] args)
[code]....
I know boxing and unboxing part in arraylist but question is if I define only string in arraylist then there is no question of boxing or unboxing. What will be the difference.
View 1 RepliesWhy only the first parameter in the definition of VB.NET Generics uses the 'Of' keyword ?
For example I have here the the full code that LINQ uses for the Enumerable.Select() method :
<System.Runtime.CompilerServices.Extension()> _
Public Shared Function [Select](Of TSource, TResult)(
ByVal source As IEnumerable(Of TSource),
ByVal selector As Func(Of TSource, TResult)
) As IEnumerable(Of TResult)
[Code]...
Recently I found a way to make CType work with generics (FYI, VB 2010 Express with option strict on). Say I have two generic types, T1 and T2, with variables x as T1 and y as T2. y=CType(x,T2) diagnoses with "Value of type 'T1' cannot be converted to 'T2'". y=CType(CObj(x),T2) compiles cleanly, and it works fine provided that T1 can be converted to T2. Failing that, an exception will be raised at run-time. Obviously, a downside of this is the boxing performance hit. Also, it is a half a step backward along the road to strong typing (a type conversion problem is discovered at run-time vice compile-time).
These objections aside, consider the GSU class below. There are two ways to do a generic CType of a variable, and then there is a way to do a CType of an entire array. GSU is sufficient (see sub Test) to convert an item or an array of items between, for example, all the numeric types (byte, short, double, ...). Exception handling remains to be done.
Type conversion code tends to get lengthy, so I think this is a nice result. What do you think? I am guessing that CType has some pre-generic smarts in it, and all I did was find a way to exploit it in the context of generics and option strict. I am mostly interested in generic tricks for value types.
[Code].....