SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  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 CType questions.

    I have a function that appears in a few classes that I'd like to abstract into a base class. There is a problem involving ctype though. Here it is in a nutshell.

    function FooBar(byval foo as Object, byval bar as System.Type) as string

    return Ctype(foo, bar).SomeKnownFunction

    end function

    As you might expect, this doesn't work. Ctype wants Object as System.Type as the second param, but I don't know how to pass that in, nor how to declare the param for the base function.

    Does anybody have their hands around this?

  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 exactly are you trying to do? Call the same method on a number of classes?

  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)
    I want to abstract this somehow. You'll see where the second and third functions will be the same for every class.

    Code VBNET:
    Option Explicit On
    Option Strict On
     
    Namespace Validators.Data
     
    Public Class ThreadDataValidator
     
    Public Function IsValidForInsert(ByRef state As ModelStateDictionary, ByVal model As Models.Data.ThreadDataModel, Optional ByVal clear As Boolean = True) As Boolean
     
    If clear Then state.Clear()
     
    ' validate everything else
     
    Return state.IsValid
     
    End Function
     
    Public Function IsValidForUpdate(ByRef state As ModelStateDictionary, ByVal model As Models.Data.ThreadDataModel, Optional ByVal clear As Boolean = True) As Boolean
     
    If clear Then state.Clear()
     
    Return IsValidForInsert(state, model, False) And IsValidForDelete(state, model, False)
     
    End Function
     
    Public Function IsValidForDelete(ByRef state As ModelStateDictionary, ByVal model As Models.Data.ThreadDataModel, Optional ByVal clear As Boolean = True) As Boolean
     
    If clear Then state.Clear()
     
    If model.ID <= 0 Then
    state.AddModelError("ID", "Invalid ID!")
    state.SetModelValue("ID", New ValueProviderResult(model.ID, CType(model.ID, String), Nothing))
    End If
     
    Return state.IsValid
     
    End Function
     
    End Class
     
    End Namespace

  4. #4
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Then you want an interface. You can pair that up with an abstract base class, or, if you were using C#, monkeypatch a default implementation in with an extension method.

  5. #5
    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)
    Quote Originally Posted by wwb_99 View Post
    if you were using C#, monkeypatch a default implementation in with an extension method.
    Does VB.NET not have extension methods?

  6. #6
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Aye. Defined like this, declaring them partials. At least I think this is what you're asking, right? Or more like the mvc HtmlHelper extensions?


    PartialPrivateSub MethodName()
    EndSub

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I thought they didn't but I guess I was wrong.

    I'd still vote writing extension methods on an interface to create a facade of a default implementation is kind of odd in some way. You probably want an interface here to define the contract, and perhaps an abstract base class to codify a default implementation.

  8. #8
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    I believe I'm going to have to rethink my approach on this because the only property that I can guarentee commonality on is ID. Even if I did make an interface or abstract for this, I'd still have problems with casting when it comes to the other properties. I have a few ideas now on how I want to handle it. Thanks.


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
  •