Page 7 of 8 FirstFirst ... 45678 LastLast
Results 241 to 280 of 282

Thread: Need Help with 'simple' class design

  1. #241

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    I had to laugh when I saw the first code you posted. Looks like a sort function for classes, and I'm not talking about 'member.firstname'. It looks like they are swapping the whole class. Code like that always hurts my brain and leaves me thinking... why would I ever want to do something like "that"?

    You don't see this quite as clearly because you haven't been using Console.WriteLine() for 10 years.
    As much as I really hate to say this, It's been a tad longer than 10 years, but I refuse to say just how long ago it was

    And now: This code I'm working on now is probably the smallest I've done. So that makes me wonder what type (or section) we should be looking at as the target where Lambdas are concerned. I think you said that would get rid of the LessThan part, so that makes me think that's probably the best place in the code to use it.

    Is that a safe assumption?

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

    Re: Need Help with 'simple' class design

    Read post #152 again. At some point, I was in exactly the same position with LessThan() for Integers, and explained how to convert it to use a comparer for Integers.

    If you follow and understand those instructions, it is literally a fill-in-the-blanks to do it for Contacts. I don't think you even need to write an entire new line of code.

    (That's why I was so so so so insistent on 'stay on task'. That was almost 100 posts ago, and it's only now that we've wandered back to where we were when it was made.)
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #243

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Thanks for reminding me about #152. And yes it is a 'fill in the blank' for Contacts

    So here's the code. If this is not what you wanted, let me know and I will try again

    Please Note:
    • This is all the code. Nothing has been hidden. Sub Main() doesn't contain any more code than what's posted
    • Also, the entire content of the Compare class is also posted. There is no other code.
    • The code has been tested to verify the correct output
      code.



    Code in Sub Main()

    Code:
     Private memberContacts As Contact
    
    
        Sub Main()
    
            Dim newCompare As New Compare
    
    
            Dim memberContacts(3) As Contact
    
            memberContacts(0) = New Contact() With {.FirstName = "Dale", .LastName = "Cooper"}
            memberContacts(1) = New Contact() With {.FirstName = "Harry", .LastName = "Truman"}
            memberContacts(2) = New Contact() With {.FirstName = "Andy", .LastName = "Brennan"}
            memberContacts(3) = New Contact() With {.FirstName = "Hawk", .LastName = "Hill"}
    
            Console.WriteLine("First names, before swap:")
            For Each contact In memberContacts
                Console.Write("{0}, ", contact.FirstName)
            Next
            Console.WriteLine(Environment.NewLine)
    
            Console.WriteLine("First names, after swap:")
            For Each Contact In memberContacts
                Console.Write("{0}, ", Contact.FirstName)
            Next Contact
            Console.WriteLine(Environment.NewLine)
    
            newCompare.SortContactsByFirstNameAscending(memberContacts)
    
            Console.WriteLine("----")
            Console.WriteLine("Names, after sort:")
            For Each member In memberContacts
                Console.WriteLine("{0} {1}", member.FirstName, member.LastName)
            Next
            Console.Read()
    
        End Sub
    Class Compare:

    Code:
    Public Class Compare
        Sub Swap(input() As Contact, leftIndex As Integer, rightIndex As Integer)
            Dim temp As Contact = input(leftIndex)
            input(leftIndex) = input(rightIndex)
            input(rightIndex) = temp
        End Sub
    End Class
    Last edited by jumper77; May 20th, 2016 at 03:28 PM.

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

    Re: Need Help with 'simple' class design

    That swaps, but doesn't sort!
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #245

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    My apologizes, give me a minute

  6. #246

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    It's taking me a bit. Wouldn't you know it, the time I cleaned up all the code before I gave it to you would be the time I would actually need it. So I'm trying to find a backup file somewhere, but if I can't, I will rewrite it.

  7. #247

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Sorry for the mix up. I just now found my code. I updated the code in #243. No need to post it twice.

  8. #248
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Need Help with 'simple' class design

    You didn't update the 'Compare' class with the sorting code.

    (Or note the advice that creating a new class wasn't needed.)
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  9. #249

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Everything is included in sub Main now

    Code:
        Sub Main()
    
            Dim memberContacts(3) As Contact
    
            memberContacts(0) = New Contact() With {.FirstName = "Dale", .LastName = "Cooper"}
            memberContacts(1) = New Contact() With {.FirstName = "Harry", .LastName = "Truman"}
            memberContacts(2) = New Contact() With {.FirstName = "Andy", .LastName = "Brennan"}
            memberContacts(3) = New Contact() With {.FirstName = "Hawk", .LastName = "Hill"}
    
            Console.WriteLine("First names, before swap:")
            For Each contact In memberContacts
                Console.Write("{0}, ", contact.FirstName)
            Next
            Console.WriteLine(Environment.NewLine)
    
            Console.WriteLine("First names, after swap:")
            For Each Contact In memberContacts
                Console.Write("{0}, ", Contact.FirstName)
            Next Contact
            Console.WriteLine(Environment.NewLine)
    
            SortContactsByFirstNameAscending(memberContacts)
    
            Console.WriteLine("----")
            Console.WriteLine("Names, after sort:")
            For Each member In memberContacts
                Console.WriteLine("{0} {1}", member.FirstName, member.LastName)
            Next
            Console.Read()
    
        End Sub
        Sub Swap(input() As Contact, leftIndex As Integer, rightIndex As Integer)
    
            Dim temp As Contact = input(leftIndex)
            input(leftIndex) = input(rightIndex)
            input(rightIndex) = temp
        End Sub
        Public Sub SortContactsByFirstNameAscending(input() As Contact)
            For i As Integer = 0 To input.Length - 2
                Dim smallestIndex As Integer = i
                Dim smallestValue As Contact = input(i)
    
                For j As Integer = i + 1 To input.Length - 1
                    If LessThan(input(j), smallestValue) Then
                        smallestValue = input(j)
                        smallestIndex = j
                    End If
                Next
                Swap(input, i, smallestIndex)
            Next
        End Sub
    
    
        Private Function LessThan(left, right) As Boolean
            Return left.FirstName < right.FirstName
        End Function

  10. #250

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Ok guys... I need some help with this one. I'm trying to implement ICompare but don't really know how to use it. The code I have is 'close', but not working. I got this code of the net. It uses Object so I left it that way thinking the compiler would be happier while I was trying to get it to work.

    I get an error on the 'Implements IComparable(Of Contact) Here's the error:

    Error BC30149 Class 'Compare' must implement 'Function CompareTo(other As Contact) As Integer' for interface 'IComparable(Of Contact)'.

    'Other' was part of the code that I got. I'll include the original code at the bottom.

    Any help appreciated

    Here's the code:

    Code:
    Public Class Compare : Implements IComparable(Of Contact)
        Public Property zIndex As Integer
    
        Public Overloads Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo
            Return obj
        End Function
    Here's the original code:
    Code:
    Class Box
        Implements IComparable(Of Box)
    
        Public Sub New(ByVal size As Integer)
    	sizeValue = size
        End Sub
    
        Private sizeValue As Integer
        Public Property Size() As Integer
    	Get
    	    Return sizeValue
    	End Get
    	Set(ByVal value As Integer)
    	    sizeValue = value
    	End Set
        End Property
    
        Public Function CompareTo(other As Box) As Integer _
    	Implements IComparable(Of Box).CompareTo
    	' Compare sizes.
    	Return Me.Size().CompareTo(other.Size())
        End Function
    End Class
    Thanks for looking

  11. #251

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Think I would one of my problems. I haven't declared an Interface.

  12. #252
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Need Help with 'simple' class design

    Two hundred fifty+ posts in 'simple' is somewhere in the rear-view window. At this point it is hard to know what is going on.

    What would have been nice, IMHO, was for the 'simple' class to have been one thread. A collection of that class another, delegates for it another, etc.

    Assuming you are trying to sort a list of contacts it would have to look something like this.

    A simple contact class.
    Code:
    Public Class Contact : Implements IComparable
    
        Public FirstName As String
        Public LastName As String
    
        Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
            If obj Is Nothing Then Return 1
            Dim c As Contact = DirectCast(obj, Contact)
            If Me.LastName <> c.LastName Then
                Return Me.LastName.CompareTo(c.LastName)
            Else
                Return Me.FirstName.CompareTo(c.FirstName)
            End If
        End Function
    End Class
    Some test code to see if sort works.
    Code:
            Dim foo As New List(Of Contact)
            foo.Add(New Contact With {.FirstName = "one", .LastName = "last1"})
            foo.Add(New Contact With {.FirstName = "two", .LastName = "lasts"})
            foo.Add(New Contact With {.FirstName = "abc", .LastName = "last1"})
            foo.Add(New Contact With {.FirstName = "three", .LastName = "lasts"})
    
            foo.Sort()
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #253

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Hi db, It's good to hear from you. I do agree about the different threads. This one started out that way but quickly became something else. It's my fault because I started the thread. Next time I won't let the thread wander out in the weeds.

    Thank you for the code. Wish I could just whip some out like that. Someday

  14. #254
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Need Help with 'simple' class design

    This

    Quote Originally Posted by jumper77 View Post
    Hi db, It's good to hear from you. I do agree about the different threads. This one started out that way but quickly became something else. It's my fault because I started the thread. Next time I won't let the thread wander out in the weeds...
    will help with this

    Quote Originally Posted by jumper77 View Post
    ...Wish I could just whip some out like that. Someday
    Do you have the basic class? Could you post just that? Why not resolve this thread and start others as needed.
    Last edited by dbasnett; May 22nd, 2016 at 10:33 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #255

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Ok... I understand. Makes sense as well. I think that being more specific would probably get more response in the beginning, but the length of the thread wouldn't be that big

    thanks

  16. #256

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Just realized.... I should make a new thread for this question.

  17. #257

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    New thread started

  18. #258
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Need Help with 'simple' class design

    Mark this resolved please.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  19. #259

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    I don't know if said this or not, but thank goodness I posted some of my code in this thread. I killed my two top level commits in my source control. So everything I have at the moment is three commits back. I'm hoping that copying the code from here will get me back closer than where I am now.

    db. I have my reasons

  20. #260
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Need Help with 'simple' class design

    I'm... a little disappointed how it all turned out in the end.

    What I implemented was the bit with LessThan(). I then challenged you to go the last mile, to copy what I did in a previous post to convert the LessThan() delegate to a comparer delegate. When you pulled the tablecloth back... it didn't sort at all. So you apologized, and corrected it, and pulled back the tablecloth from... a version without any sorting code. So you updated it to... a version of my LessThan() code that might even be a copy/paste.

    And then you started asking about implementing an interface. I hinted in #119 you didn't need a new class, and I swear at least two or three times in this mess I said "Implements" or "Inherits" weren't needed for this task, which was "use delegates to sort". It's not that they aren't commonly used for it, it's that this thread was never about IComparer. That's why dbasnett said to start another thread. It is, has been, and will continue to be off-topic for this unfinished challenge.

    I don't now if I have time next week to keep watching this one or the other one. I direct you back to my suggestion to read #152, all the way to the end. Ask yourself if the code you posted looks like the beginning, middle, or end.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  21. #261
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Need Help with 'simple' class design

    Quote Originally Posted by jumper77 View Post
    I don't know if said this or not, but thank goodness I posted some of my code in this thread. I killed my two top level commits in my source control. So everything I have at the moment is three commits back. I'm hoping that copying the code from here will get me back closer than where I am now.

    db. I have my reasons
    When I first started coding for PC's back in those Y2K days the first thing I noticed was a need to backup the entire project at certain points. It wasn't just the code that needed to be saved - it was project and solution files with references and all the rest.

    Going back to prior versions always hurts.

    But then again it gives you the freedom to attempt other methods and then roll back to a point-in-time prior to those attempts if they prove less then worthy.

    Regardless coding is incremental steps that are proven to be clean - 100% - before moving forward.

    If that is the discipline you follow then archiving those proven moments is paramount to success.

    You can never add code to unworthy or untested or questionable code - that's a house of cards destined to fall and imo, just a waste of time. I want my time at my desk - when I'm really outputting moments of good code - to be productive and meaningful climbing of the ladder to completion.

    And now that I'm doing JavaScript it's even more important to get point-in-time versions archived. JavaScript makes you want to do things like define the function name WITH NO CODE within and then place the function call in the spot you want. Run and test with breakpoints to make sure you arrive at the new and empty function. Then you have a WATCH window that tells you what you know about the scope of your variables and what not - and you actually find yourself COPY/PASTING the "path to the variable/value" in the watch window and going back to the IDE-code window and pasting it in and working your logic around it.

    Coding has become 2 lines of code and a re-run of the app with breakpoints to see the affect of said change and then again and again and again and rinse and repeat and rinse.

    At this time if I waste even 30 minutes creating code that will be discarded I make a mental note - take a break - pet my dogs - re-group.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  22. #262

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Thanks for the input sz, I try my best when it comes to backups, but what happened this time got me. I use online source control using BitBucket. Plus I have two backup directory's. One the source control, I did something that made me lose my last two commits and I didn't do backups soon enough. So here I sit.

    Thankfully I'm back to where I was. The code is different, but at least it's working

  23. #263

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    never mind... I was about to speak to soon....
    Last edited by jumper77; May 24th, 2016 at 10:16 AM.

  24. #264

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    The last time I worked on the a sort method, it looked like this. The name of the method will be changed.

    Code:
       Sub SortContactsByName(input() As Contact)
    
            For i As Integer = 0 To input.Length - 2
                Dim smallestIndex As Integer = i
                Dim smallestValue As Contact = input(i)
    
                For j As Integer = i + 1 To input.Length - 1
                    If input(j).FirstName.ToLower < smallestValue.FirstName.ToLower Then
                        smallestValue = input(j)
                        smallestIndex = j
                    End If
                Next
                Swap(input, i, smallestIndex)
            Next
        End Sub
    In post #152, it talks about sorting integers. But I'm sure that pages after that post it was changed to sorting names. So I'm just doing a reality check.

    Is this how I should be doing it now?
    I already have the code....

  25. #265
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Need Help with 'simple' class design

    Yes.

    Look at the pattern in the Integer sorting post. It walked through a very specific process to go from "not delegates" to "delegates":
    1. Where is the comparison? I must replace that comparison with a method.
    2. I must replace THAT method with a method that matches a the comparison delegate I wish to use.
    3. Instead of hard-coding the method call, I am going to call a delegate variable that I receive as a parameter.


    I demonstrated (1) and (2) in a recent post, and left (3) as the challenge. It might do you well to find that post and walk through it again, comparing it to the Integer case to see just how similar they are. Armed with that knowledge, and seeing how Integer (2) went to Integer (3), should make Contact (3) very obvious.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  26. #266

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Ok.. will do, and thanks for the reply. Just wanted to know if I was good to head on the the delegate part

  27. #267

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    I should have said, thank you very much for your post. I read it again and I "do" understand what you're talking about.

    Hope your day is going smooth...

  28. #268

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Hi all, I haven't been on here much (well, my computer is always on even when I'm gone).

    I have a friend who's not doing well and he wanted me to come over, so I did.
    But I've been sick all day. Can only stay so long on the 'puter befor having to lay down.

    thanks

  29. #269

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    OMG! I know I'm feeling better this morning. I'm almost through with this part of the project. Need to fix a couple of things and I'm done. I may have one last question, but that's it.

  30. #270

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Well, I finally have it. It's finished. Think I'll get a beer now. Actually I just popped a top. But it was on a coke

    I've cleaned everything up and there is no unused code in the project. I also checked the naming and that seems good.

    So I'm attaching the whole projects.

    Note: I had to change the descending delegate. When I used the -1* one, I got stuck in an infinite loop. I tried everything to get it to work but It would always get stuck in the loop. So I made the descending one look just like the ascending. The loop went away. Have no idea why.
    Last edited by jumper77; May 25th, 2016 at 12:18 PM.

  31. #271

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    Just had another thought. I would like the next step to be using 'compareTo(
    But my thoughts are this. We should us the same files we are using now. That way all you have to focus on is changing how you compare things.

  32. #272
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Need Help with 'simple' class design

    A mod's going to get you for including the bin and obj folders again. Always delete them before making the .zip.

    You're very close.

    I'd wager part of why this is so hard is you insist on maintaining several class files despite me insisting that you stop. I wrote this example such that it should be very easy to open the integer sorting solution, make some small changes, then have the Contacts solution. But, if you reorganize the code, and move all the pieces around, all the care I took to structure everything the same way in each example is messed up. And because you can't put your files next to my file and see how all the bits line up, you lose a lot of the hints I've tried to encode. I don't tell you "please stop doing this" because I hate fun. I say it because when we're dealing with short examples, there is no way to understate the benefits of everyone having the same file with the same structure.

    After looking over this project:

    Why are there two methods in the Compare class? I had one SortIntegers() method in the integer solution. That means you should have one SortContacts() method. The point of having the delegate parameter is so that you can write one Sort() method, then control how it sorts by providing different comparison delegates. If you copy/paste your sorting algorithm, and give methods different names, you're losing the benefit of having the Delegate parameter.

    Think about it, and try to write it with only one SortContacts() method.

    If you get stuck, I've attached the solution, because you're close enough. This is another advantage of having everything in one file for these examples: no .zip, no bin or obj folders, no getting your attachment deleted by a moderator.
    Attached Files Attached Files
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  33. #273
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Need Help with 'simple' class design

    Why not work and share all on DotNetFiddle - ddays has been putting sample code up for running at that site.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  34. #274
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Need Help with 'simple' class design

    Quote Originally Posted by szlamany View Post
    Why not work and share all on DotNetFiddle - ddays has been putting sample code up for running at that site.
    Because it's terrible with VB syntax in every environment I try. In particular, it doesn't understand For loops. I can't live if it's constantly reformatting my code to look like:
    Code:
    	Public Sub Main()
    		For i As Integer = 1 To 5
    		For j As Integer = 1 To 5
    		Console.WriteLine(i * j)
    	Next
    	Next
    			
    
    	End Sub
    I reported it as a bug. Last year.

    It sort of works if you do all your work in VS then carefully paste it into their site. It also works to upload text files or use [code] blocks.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  35. #275

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    A mod's going to get you for including the bin and obj folders again. Always delete them before making the .zip.
    I guess that was an accident because I was testing the project to make sure if everything would work if I deleted the bin folder. Must have had one test to many. But I was trying.

    I'd wager part of why this is so hard is you insist on maintaining several class files despite me insisting that you stop. I wrote this example such that it should be very easy to open the integer sorting solution, make some small changes, then have the Contacts solution. But, if you reorganize the code, and move all the pieces around, all the care I took to structure everything the same way in each example is messed up. And because you can't put your files next to my file and see how all the bits line up, you lose a lot of the hints I've tried to encode. I don't tell you "please stop doing this" because I hate fun. I say it because when we're dealing with short examples, there is no way to understate the benefits of everyone having the same file with the same structure.
    About the classes. I honestly thought the compare class was yours. If not, then yes I am making it harder on myself. And for the other things. Next time I won't change your code. I guess that will make it easier on everyone. I guess that way you don't have to figure out what the heck I'm doing because you will be looking at your 'own' code.

    After looking over this project:

    Why are there two methods in the Compare class? I had one SortIntegers() method in the integer solution. That means you should have one SortContacts() method. The point of having the delegate parameter is so that you can write one Sort() method, then control how it sorts by providing different comparison delegates. If you copy/paste your sorting algorithm, and give methods different names, you're losing the benefit of having the Delegate parameter.

    Think about it, and try to write it with only one SortContacts() method.
    I encountered a small problem when using your descending code. The one that used -1. When I used it, I got stuck in an infinite loop. Of course it was probably my fault. I tried to find out why it was happening and gave up. I guess I should have asked for help. When I changed it to be exactly like the ascending code, the loop went away. So I did what I thought best. But I guess it wasn't a good idea after all.

    If you get stuck, I've attached the solution, because you're close enough. This is another advantage of having everything in one file for these examples: no .zip, no bin or obj folders, no getting your attachment deleted by a moderator.
    Thanks for sending the solution, but I will only use it as a last resort. I would much rather solve it myself. If I run into any issues I'll post.

    And one last thing, Is the reason I got into an infinite loop was because of the names of my methods? Thanks

  36. #276

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    I fixed the zip file so it doesn't have the bin folder anymore

  37. #277

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    I just got the sort working using the -1 and I'm only using 1 function

    edit: no loop
    Last edited by jumper77; May 25th, 2016 at 03:18 PM.

  38. #278

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    don't have everything working... stay on hold for a bit.

  39. #279

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    I not sure how you actually meant this to work, but this is what's happening. I do have two separate function that are fired off. Sort Ascend and Sort Descend. But they both go through the sort Ascending section. The one at the top that's Func(Of Contact....

    here's the first two method calls that are in sub main
    Code:
            thisCompare.SortContactsAscending(memberContacts, SortContactsAscending)
            For Each value In memberContacts
                Console.WriteLine(value.FirstName)
            Next
            Console.WriteLine("----")
    
    
            thisCompare.sortContactDescending(memberContacts, SortContactsAscending)
            For Each value In memberContacts
                Console.WriteLine(value.FirstName)
            Next
    Now the next two subs/functions are in the "compare" class
    Code:
       Sub sortContactDescending(input() As Contact, comparer As Func(Of Contact, Contact, Integer))
    
            For i As Integer = 0 To input.Length - 2
                Dim smallestIndex As Integer = i
                Dim smallestValue As Contact = input(i)
    
                For j As Integer = i + 1 To input.Length - 1
                    If comparer(input(j), smallestValue) > 0 Then
                        smallestValue = input(j)
                        smallestIndex = j
                    End If
                Next
                Swap(input, i, smallestIndex)
            Next
    
        End Sub
    
        Sub SortContactsAscending(input() As Contact, comparer As Func(Of Contact, Contact, Integer))
    
            For i As Integer = 0 To input.Length - 2
                Dim smallestIndex As Integer = i
                Dim smallestValue As Contact = input(i)
    
                For j As Integer = i + 1 To input.Length - 1
                    If comparer(input(j), smallestValue) < 0 Then
                        smallestValue = input(j)
                        smallestIndex = j
                    End If
                Next
                Swap(input, i, smallestIndex)
            Next
        End Sub
    Notice that < got turned into > in the descend function. I don't know if that's the way it's supposed to work or not. When I look at both Func(Of sections in sub main, I don't think I can see another way it would work?

  40. #280

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Need Help with 'simple' class design

    I moved all the compare code into Module1. But when I did, SortAscending has errors, but sortDescending doesn't

    Here's the two code snippets.

    Code:
            SortContactsAscending(memberContacts, SortContactsAscending)
            For Each value In memberContacts
                Console.WriteLine(value.FirstName)
            Next
            Console.WriteLine("----")
    
    
            sortContactDescending(memberContacts, SortContactsAscending)
            For Each value In memberContacts
                Console.WriteLine(value.FirstName)
            Next
    On the first line posted, memberContacts AND SortContactsAscending gets the following error:
    Value of type Contact() cannot be converted to Contact

    However, sortContactDescending has no errors at all?? Here are both methods. Maybe someone can see something I don't.
    Code:
       Sub sortContactDescending(input() As Contact, comparer As Func(Of Contact, Contact, Integer))
    
            For i As Integer = 0 To input.Length - 2
                Dim smallestIndex As Integer = i
                Dim smallestValue As Contact = input(i)
    
                For j As Integer = i + 1 To input.Length - 1
                    If comparer(input(j), smallestValue) > 0 Then
                        smallestValue = input(j)
                        smallestIndex = j
                    End If
                Next
                Swap(input, i, smallestIndex)
            Next
    
        End Sub
    
        Sub SortContactsAscending(input() As Contact, comparer As Func(Of Contact, Contact, Integer))
    
            For i As Integer = 0 To input.Length - 2
                Dim smallestIndex As Integer = i
                Dim smallestValue As Contact = input(i)
    
                For j As Integer = i + 1 To input.Length - 1
                    If comparer(input(j), smallestValue) < 0 Then
                        smallestValue = input(j)
                        smallestIndex = j
                    End If
                Next
                Swap(input, i, smallestIndex)
            Next
        End Sub
    One other odd thing. When you right click on Ascending ahd click 'go to definition' it goes to the sortAscending Func(Of

    But it you do the same thing with SortDescending, it goes to the method call and not func(Of

Page 7 of 8 FirstFirst ... 45678 LastLast

Posting Permissions

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



Click Here to Expand Forum to Full Width