dcsimg
Results 1 to 16 of 16

Thread: Trying to declare Same name classes

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,349

    Trying to declare Same name classes

    Hi.
    I'm trying to declare classes with the same name. What I have found so far is to use a namespace and put the classes inside.
    The problems I'm facing are
    1)I do not want the esoteric class to be available but I cannot declare it as Private
    2)I want to be able if possible to also name the namespace with the same class name

    Code:
      Public Class ValidateMember
            Inherits ValidateMemberC.ValidateMember
    
        End Class
        Namespace ValidateMemberC ''If possible the name to be ValidateMember
            Public Class ValidateMember ' I want this to be Private or non visible
                Public Property UserSessionId As String
                Public Property MemberPassword As String          
            End Class
        End Namespace
    Declaration:
    Code:
    Dim lv1 As New ValidateMemberC.ValidateMember ' I would like this to be like Dim lv1 As New ValidateMember.ValidateMember
    
    Dim v As New ValidateMember  ' I do not want this to show
    Last edited by sapator; Feb 6th, 2018 at 07:57 AM.
    Slow as hell.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Trying to declare Same name classes

    This is not possible because if you have 2 classes AND a namespace all with the same name, you can create scenarios where it's mathematically impossible for the compiler to understand which of the 3 things you mean.

    What are you actually trying to do? We've probably solved that problem a different way at least once.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,349

    Re: Trying to declare Same name classes

    Hi.

    So I'm translating Json to vb classes.
    For example a Json class called ValidateMember can have doubles with an array inside another Json Class that is also called ValidateMember with different properties.
    You can see the confusion.
    Thanks.
    Slow as hell.

  4. #4

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,349

    Re: Trying to declare Same name classes

    At worst I can put them in namespaces and let the namespace names differ.
    Maybe so but I was searching for something better, if any.


    Code:
     Namespace ValidateMemberC
            Public Class ValidateMember
                Public Property UserSessionId As String
                Public Property MemberPassword As String
                Public Property MemberLogin As String
                Public Property ReturnMember As Boolean
                Public Property MemberId As String
                Public Property MemberCardNumber As String
                Public Property MemberEmail As String
                Public Property ClubId As String
                Public Property IncludeAdvanceBooking As Boolean
                Public Property IncludeAdvanceSeating As Boolean
                Public Property CustomerLanguageTag As String
                Public Property PinNumber As String
                Public Property OptionalClientClass As String
                Public Property OptionalClientId As String
                Public Property OptionalClientName As String
            End Class
        End Namespace
    
        Namespace ValidateMemberD
            Public Class ValidateMember
                Public Property UserSessionId As String
                Public Property MemberPassword As String
            End Class
        End Namespace
    Slow as hell.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,823

    Re: Trying to declare Same name classes

    eeeh... it's the same class... it's just that everything from MemberLogin on down isn't filled in or is empty. Other wise it's the same class. UserSessionId and MemberPassword would be the only things filled in in both cases.

    Depending on what you're using to deserialize from JSON to VB classes, it should be perfectly fine using the same class in both cases.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,349

    Re: Trying to declare Same name classes

    not exactly.
    The name is the same but if you do Dim lv1 As New ValidateMemberC.ValidateMember you get the full properties but if you do Dim lv1 As New ValidateMemberD.ValidateMember you get 2 properties.
    Slow as hell.

  7. #7

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,349

    Re: Trying to declare Same name classes

    Ahh.
    I see what you mean.
    No again there is an issue.
    If the Login Class has:

    Code:
    Class Login
    Public Property UserSessionId As String
    Public Property MemberPassword As String
    End Class
    And a class of another Json call has:
    Code:
    Class Login
    Public Property UserSessionIdXxxx As String
    Public Property MemberPassword As String
    End Class
    Then they are not the same.
    Slow as hell.

  8. #8

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,349

    Re: Trying to declare Same name classes

    Ok my bad.

    I was using this:
    Code:
    Public Class ValidateMember
            Inherits ValidateMemberC.ValidateMember
    
        End Class
    That was a leftover. If I do not use this then I'm bound to select a namespace with classes that may have the same name in it.
    I think this is acceptable, unless there is something better.

    So I need to to do this:
    Code:
    Namespace Loyalty
    
    Namespace ValidateMember
    Public Class ValidateMember
    Public Property UserSessionIdAs String
    Public Property MemberPassword As String
    End Class
    End NameSpace
    
    Namespace ValidateMember2
    Public Class ValidateMember
    Public Property UserSessionIdXXX As String
    Public Property UserSessionId As String
    Public Property MemberPassword As String
    Public Property ReturnMember As Boolean
    Public Property MemberId As String
    End Class
    End NameSpace
    
    End NameSpace
    So I can even declare the first namespace as exactly the name of the Class i need (of course the second namespace cannot have the same name, but can include the same Class name).

    So calling:
    Code:
     Imports MySolution.Loyalty 
    Dim v As New ValidateMember.ValidateMember 'works but looks a little ugly so I prefer to use the inner namespace as LoyaltyValidateMember and call it like:  
    Dim v As New LoyaltyValidateMember.ValidateMember
    Will work.
    Seems OK?
    Slow as hell.

  9. #9
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Trying to declare Same name classes

    Names don't matter when you are parsing JSON. JSON represents "objects", and in the JavaScript sense of that word there aren't really "classes" with "names".

    Why do you believe the names matter?

    For real, try it out. With the Newtonsoft parser:

    Code:
    Imports Newtonsoft.Json
    
    Module Module1
    
        Sub Main()
            Dim json As String = "{ ""value"": 10 }"
    
            Dim obj1 = JsonConvert.DeserializeObject(Of Example)(json)
            Dim obj2 = JsonConvert.DeserializeObject(Of AnotherExample)(json)
    
            Console.WriteLine(obj1.Value)
            Console.WriteLine(obj2.Value)
        End Sub
    
    End Module
    
    Class Example
        Public Property Value As String
    End Class
    
    Class AnotherExample
        Public Property Value As String
    End Class
    If the problem really is you want two separate API calls to have an object named ValidateMember, you have to distinguish them from each other in some way. Let's say you have a "Login" and "post image" API.

    I think the best choice would be to use the names "LoginValidateMember" and "PostImageValidateMember". If you do this, you can easily use both types in the same code file without trickery.

    Your alternative is to use namespaces to segregate the two, but having two types named "ValidateMember" in two different namespaces means it's harder to use them within the same file: you'll have to fully qualify them.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,823

    Re: Trying to declare Same name classes

    As long as they are in different namespaces, they can have the same name.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Trying to declare Same name classes

    Quote Originally Posted by techgnome View Post
    As long as they are in different namespaces, they can have the same name.

    -tg
    This can be a bad choice still, though.

    For example, try creating a project with a class named "Form", and see how fun that is
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,823

    Re: Trying to declare Same name classes

    Well, sure, if you wanna push the rules... there's lots you can do that is "fun" ... In this case, it's probably fine.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Trying to declare Same name classes

    What I mean is what I talked about earlier, you can create a name ambiguity.

    The reason my "Form" example is immediately aggravating is VB automatically imports System.Windows.Forms into every file. When you create your own Form class, you end up with another type that has that name.

    You can use both in the same file, but you have to resolve that ambiguity. So instead of being able to use your Form, you'll always have to say:
    Code:
    Dim taxForm as New MyProject.Form()
    And when you want to talk about a Windows Form:
    Code:
    Public Class LoginForm
        Inherits System.Windows.Forms.Form
    It's functional, but trouble. My guess is OP wants to use all of their types within the same file, such as within a form, so they might not want to deal with that. If that's the case, a new name is "cleaner" than a new namespace. Considering there's a lot of overlap in property names, I think it'd be easy to grab the wrong one if namespaces are used.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,823

    Re: Trying to declare Same name classes

    In an ideal world, sure... but when you're consuming the data from another location... sometimes you don't have much of a choice. That said, I still don't see why two classes are needed in this case. The parser should be perfectly fine parsing what it has, and leaving the rest of the fields blank.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Trying to declare Same name classes

    Well, that's why I started with "JSON doesn't specify type names". You generally can name your objects whatever you want because JSON parsers only look at properties. You can configure some parsers to emit type names and fail if they don't match, but 1) that's how threads like this get made and 2) once you're doing that you ought to switch to XML and use schemas since you want validation/versioning.

    It's really a nitpicky thing, though, both approaches work.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  16. #16

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,349

    Re: Trying to declare Same name classes

    Hi.
    How about this?
    Code:
    Imports Newtonsoft.Json
    
    Module Module1
    
        Sub Main()
            Dim json As String = "{ ""value"": 10 }"
    
            Dim obj1 = JsonConvert.DeserializeObject(Of Example)(json)
            Dim obj2 = JsonConvert.DeserializeObject(Of AnotherExample)(json)
    
            Console.WriteLine(obj1.Value)
            Console.WriteLine(obj2.Value)
        End Sub
    
    End Module
    
    'this is an autonomous class used in some web service call
    Class Example
        Public Property Value As String
    End Class
    
    Class AnotherExample
        Public Property Value As Example()
    End Class
    
    'This is the class AnotherExample needs to implement
    Class Example
        Public Property Value As String
        Public Property AnotherValue As String
        Public Property AndAnotherValue As String
    End Class
    How would I go and fix this? Is there a better solution?Namespaces looks more easy to implement but of course I would consider an alternative if it can be done better(but not harder ).

    I'm no masochist, I'm doing this for a couple of reasons.
    Firstly the company I would give the project to has already using older calls with similar names, so they would jump right into their part without try to find which one goes where.
    Secondly the Json parser in https://jsonutils.com/ will just gives the classes and that suits me because there are some HUGE Json declarations I have to deal with. If I would have to go in every class that has the same name and give them a different name and then by doing so have to go to the calling classes that include this class and change their declaration also but make sure that I do not change the same class calls that are for another class that has some classes that have the names I just changed and change them also and so on and so on, then that would take a lot of time because I have more than 100 web services here to deal with.

    I may have trouble comprehending what techgnome is telling my about the two classes. Isn't the above example specifying the same classes? That is what https://jsonutils.com/ spitting out when I insert the Json calls, object as someclass() , so there may be a someclass declaration elsewhere that is a root class with different properties. Am I missing something?
    Slow as hell.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width