SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)

    Question Question on Generics

    There's something I don't completely understand about generics. Can somebody elaborate based on the below.

    Consider the following code:

    public class SampleProcessor(Of TModel)

    public function DoSomething(byval m as TModel) as boolean
    ' m.PropertyName doesn't work here as you might expect
    end function

    end class

    How do I access the methods and properties of the input instance? I could use CallByName or reflection I suppose, but one seems antiquainted, and the other I no little about. There has to be a simpler way.

  2. #2
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What your missing is your constraints. Constraints allow you to define an restriction on the type that can be used. I can't find the VB.NET documentation, but here is a link to the C# documentation: http://msdn.microsoft.com/en-us/library/d5x73970.aspx

    Basically, what you are looking to do is set the constraint to an abstract base class or an interface, and all the types you want to use this generic class with must inherit from it.

    Code VBNET:
    public class SampleProcessor(Of TModel as BaseType)
     
    public function DoSomething(byval m as TModel) as boolean
    ' m.PropertyName doesn't work here as you might expect
    end function
     
    end class

    Constraints can also be applied at the method level too.

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Right, I'm aware of that and IComparable etc...the things is, these classes dont' HAVE a base class. They are simple MVC models with nothing but properties. I don't know of a way to give them a base functionality. Am I just out of luck on this one?

  4. #4
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your not willing to use a common base type, then you are outta luck. I would rethink your design if your not willing to use a base type.

  5. #5
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    It's not that I'm not willing. I'd actually love to. I just don't see how. Maybe you can see things from a different angle. Here are two sample of the types of objects that would be passed in. I've omitted the actual get-set code for brevity.

    public class ThisThing

    private _Value as string

    public propetry Value as string ...

    end class

    public class OtherThing

    private _Value as integer

    public propetry Value as integer ...

    end class

    So as you can see, with only simple properties like this, I'm not sure a base class can be derived. What would you suggest in this case to make my validators more flexible?

  6. #6
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you looking for validating view model values, I would highly recommend checking out Castle Windsor's Validation framework: http://www.castleproject.org/activer...alidation.html or the Data Annotations stuff that comes with .NET 3.5 http://www.asp.net/learn/mvc/tutorial-39-cs.aspx

    You can integrate both validation frameworks with Mvc more using xVal too: http://xval.codeplex.com/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •