Curtis Rutland's Profile User Rating: *****

Reputation: 4433 Guru
Group:
Moderators
Active Posts:
7,830 (5.18 per day)
Joined:
08-June 10
Profile Views:
69,377
Last Active:
User is online A minute ago
Currently:
Viewing Board Index

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Who Cares
Favorite Gaming Platform:
PC
Your Car:
Chevrolet
Dream Kudos:
1000
Expert In:
C#, LINQ, Dynamics CRM 2011

Latest Visitors

Icon   Curtis Rutland has been promoted!

Posts I've Made

  1. In Topic: 2 dimensional array

    Posted 25 Jul 2014

    Well, actually I have a guess as to what's going on. Here's your call to the method:

    List<System.IO.DriveInfo> fetchConnectedDrives = JamSnaps.Functions.newConnectedDevice(currentConnected);
    


    So, what's the value of currentConnected you're passing in?

    From the code available, you don't set currentConnected before the first time you call this method. So, that means you're passing null into your newConnectedDevice method.

    So, of course the if (c != null) check never comes back true, which means you never have a chance to set newFound to true. Which means you'll always return null. So you need to work on that logic.



    I'd also like to share some general advice.

    1. Don't switch a boolean. There's only ever one of two options. That's the ideal case for a simple if/else, not a switch. You could re-write your switch statement to this:

    if(newFound) 
      return DriveList;
    else 
      return null;
    


    Or even this, with an inline-if statement:

    return newFound ? DriveList : null;
    


    2. Don't try to do everything in multiple nested loops/statements. It gets difficult to mentally maintain your place inside them. It's always easier to read "flatter" code; code that is less "nested", so to speak.

    Also, you can reorganize your checks for existing drives in such a way that you don't have to go through every item in each list. If you find a single unique drive name, you know you can return the new drives. So if we reorganize your code, it comes out like this: (notice that I also renamed variables to be correctly cased and descriptive*)

    public static List<DriveInfo> GetNewlyConnectedDevices(List<DriveInfo> existingDrives)
    {
    	List<DriveInfo> drives = new List<DriveInfo>();
            //get all drives and start looping through them
    	foreach(DriveInfo drive in DriveInfo.GetDrives())
    	{
    		if(drive.DriveType == DriveType.Removable && Drive.IsReady)
    			drives.Add(drive);
    	}
    	//if there are no existing drives, then we don't need to bother comparing,
    	//just return all the new drives
    	if(existingDrives == null || existingDrives.Length > 1)
    		return drives;
    	//now we can start comparing
    	foreach (DriveInfo newDrive in drives)
    	{
    		bool match = false;
    		foreach(DriveInfo existingDrive in existingDrives)
    		{
    			//if we find a single match, stop looking
    			if(existingDrive.Name == newDrive.Name)
    			{
    				match = true;
    				break;
    			}
    			//if we didn't find a match, we know there's at least
    			//one new service, so return the list
    			if(!match)
    				return drives;
    		}
    		//if we made it this far, it means all drives are the same
    		return null;
    	}
    }
    


    Now I haven't tested that, but the concept's there. Of course, if you want to go even further, and learn about LINQ, it would make this function even more self-explanatory:

    public static List<DriveInfo> GetNewlyConnectedDevices(List<DriveInfo> existingDrives)
    {
    	//get our new removable ready drives
    	var newDrives = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Removable && Drive.IsReady);
    	//return them if our existing drives are null/empty
    	if(existingDrives == null || !existingDrives.Any())
    		return newDrives.ToList();
    	//get lists of just names from both lists
    	var existingNames = existingDrives.Select(d => d.Name);
    	var newNames = newDrives.Select(d => d.Name);
    	//get all new names except those that already exist in existingNames
    	//and if there are any, return the newDrives list
    	if(newNames.Except(existingNames).Any()) 
    		return newDrives.ToList();
    	//if we got here, there were no new drives
    	return null;
    }
    




    Now, the side note about naming. Typical C# convention states:

    • Function names : PascalCase
    • local variable names : camelCase
    • function parameters : camelCase
    • class names : PascalCase


    Also, use descriptive names. c means nothing. existingDrives means something. And it also is no harder to type, since VS finishes words for you.

    Last note: when naming your objects, keep in mind what they do. Typically, you want to express functions as verbs, and variables as nouns. So, your method named "newConnectedDevice" doesn't really say what it does. You call it to get newly connected devices, so I named my example "GetNewlyConnectedDevices". It says exactly what it does. Because methods do things. They get, they set, they find, they do all kinds of things. That's why you want to name them after the verb they do.

    Hope this all helps.
  2. In Topic: How do I size my listBox height to items?

    Posted 25 Jul 2014

    View Postandrewsw, on 25 July 2014 - 12:20 PM, said:

    Glad I could help.

    Just to complete the picture, the maximum-height could be constrained using:
        if (listBox1.PreferredHeight > 150)
            listBox1.Height = 150;
        else
            listBox1.Height = listBox1.PreferredHeight;
    


    Or with the ternary operator:

    listBox1.Height = listBox1.PreferredHeight < 150 ? listBox1.PreferredHeight : 150;
    


    Though for readability reasons I'd still use a method like Skydiver's. I wonder if there's a Min method already baked into the .NET framework...
  3. In Topic: First WPF/C# Program - Login Page

    Posted 25 Jul 2014

    I know. I answered that question. Your code is correct if your output and behavior is correct. Of course, you don't know if your behavior is correct without testing all possible cases, so that's how you find out if your code is correct: testing all possible ways to interact with your program. If all possible cases produce the desired output, your code is "correct". Otherwise not.

    Of course, "correct" doesn't mean based on standards or best practices.

    If all you want to do is echo one textbox to another, then you're done. But really, why would you? You don't; you should be more interested in the data itself and what you can do with it other than just showing it on the screen.

    You should probably stop thinking about using two more textboxes to show the username/password, since that's not what real applications do (you've never used an app that shows your password, right? Especially not in another textbox).

    Switch your password TextBox to a PasswordBox, and learn how to use that. Then wire up your Login button to actually do some check, to check the password against some value.

    Side note: if you're going to use this as an actual login screen, there's some serious learning you need to do, namely salting and hashing passwords. Don't just store passwords in a database or text file or code; it's extremely poor practice. Read up on it if this is anything more than a learning project.
  4. In Topic: First WPF/C# Program - Login Page

    Posted 25 Jul 2014

    The problem for us is that we don't know what "correct" is. This isn't a yes or no question; it's for you to decide if your output is correct or not based on your input. Because "correct" is different for every application.

    I'd suggest that no, if you don't want the value of one control to be immediately reflected by the other, then you shouldn't have multiple controls bound to the same property. If you desire that behavior, then yes, it's correct.
  5. In Topic: Beginner's Q: Is a void a variable like a bool, int?

    Posted 25 Jul 2014

    There's a difference between variables and data types. Variables are what you store values in. When you say int a = 1;, you're creating a variable with a data type of int. "int" isn't a variable, it's a data type. "a" is the variable.

My Information

Member Title:
(╯□)╯︵ (~ .o.)~
Age:
29 years old
Birthday:
August 29, 1984
Gender:
Location:
San Antonio
Forum Leader:
C#
Full Name:
Curtis Rutland
Years Programming:
7
Programming Languages:
C# -- Forms, WPF, Silverlight, WCF

Contact Information

E-mail:
Click here to e-mail me
Website URL:
Website URL  http://curtisrutland.com
Skype:
Skype  insertAlias_bytes
Twitter:
curtisrutland

Comments

  • (2 Pages)
  • +
  • 1
  • 2
  1. Photo

    NewRandomUsername Icon

    06 Jun 2014 - 20:59
    Did you get my messages(emails) sir?
  2. Photo

    BenignDesign Icon

    03 May 2013 - 13:29
    I see you were staring at my profile. I also stare randomly at profiles. We have much in common. We should probably hook up sometime. Just sayin.
  3. Photo

    eric wallace Icon

    31 Jan 2013 - 02:53
    can you help for visual basic
  4. Photo

    no2pencil Icon

    06 Dec 2012 - 08:34
    It still isn't good enough.
  5. Photo

    ikram002p Icon

    27 Jun 2012 - 10:51
    "It Doesn't Work!" That's not good enough!
    It Still Doesn't Work!.
    I didnt undestand it ??
  6. Photo

    tlhIn`toq Icon

    18 Mar 2012 - 11:22
    Congrats on breaking 3000
  7. Photo

    Curtis Rutland Icon

    08 Sep 2011 - 14:13
    "Leveling up." I like that!
  8. Photo

    MATTtheSEAHAWK Icon

    08 Sep 2011 - 13:55
    Yar ya see, i remember when yee wer purple!
    Anyways congrats! It's weird seeing people who've been one way so long "leveling up" :P.
  9. Photo

    Curtis Rutland Icon

    08 Sep 2011 - 12:59
    Thanks!
  10. Photo

    Sergio Tapia Icon

    08 Sep 2011 - 12:29
    Congrats on becoming a mod. :D
  11. Photo

    Ace26 Icon

    15 Jun 2011 - 13:39
    Hi mate, interesting to see we are age mates. Nice work you do in the forums.
    Just dropped by to say hi, anyways.
    Cheers.
  12. Photo

    RetardedGenius Icon

    20 Apr 2011 - 18:56
    Excellent C# tutorials, they've taught me a lot I didn't know. Please keep more of the good stuff coming! :D
  13. Photo

    Curtis Rutland Icon

    24 Mar 2011 - 14:55
    Yeah, felt like "professionalizing".
  14. Photo

    Dogstopper Icon

    23 Mar 2011 - 17:08
    Woah. Name switch! I took a double take...
  15. Photo

    CodingSup3rnatur@l-360 Icon

    01 Mar 2011 - 14:35
    No problem at all. Glad I could contribute :)
  • (2 Pages)
  • +
  • 1
  • 2