Simple Answers for Simple Questions from Experienced Developers

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 7228 Views - Last Post: 18 February 2016 - 08:08 AM

#1 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Simple Answers for Simple Questions from Experienced Developers

Post icon  Posted 16 February 2016 - 03:46 PM

Note: this thread will be subject to heavy moderation*.

This is a thread for experienced programmers to ask questions in. Questions that don't necessarily deserve their own full topic, but ones that can be answered in a relatively simple manner. The people who should get the most out of using this thread are those who have significant experience in some language other than C#. But it can also be for people with C# experience, since the .NET Framework is so broad you can be an expert on one side and ignorant on the other.

The types of questions we'll see here would usually be googleable, with the right contextual information. This thread is for people who are missing that context; we can help you fill in the gaps.

For those doing the answering: remember the topic: "Simple Answers". We don't need paragraphs of discussions, we don't need a debate, we just need to point people in the right direction in this thread.

* The moderators and forum leaders will patrol this thread and remove anything that doesn't meet our requirements. This is not a beginner's thread. This is not a discussion thread. Simple answers, simple questions. If you see anything derailing this topic, please report the post in question. Our moderators will determine whether or not to remove it. We will be strictly enforcing our requirements on this thread. We may even remove clarifying posts (most likely to move the content into the original question post).

Is This A Good Question/Topic? 2
  • +

Replies To: Simple Answers for Simple Questions from Experienced Developers

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6505
  • View blog
  • Posts: 14,362
  • Joined: 02-June 10

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 16 February 2016 - 04:01 PM

In an effort to jump start the answering of many of the common questions I submit my FAQ list:


tlhIn`toq's FAQ list - Updated Oct 2015


Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.
Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute, check the condition of each of variable's run-time value, and watch the logic unfold right before your eyes.
Visualizing what your code does will let you see why it behaves the way it does.
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

In addition to FAQ 2 in the list below, also check my signature block for a link on how to look at your variables' values at run-time.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

Why are you still building in WinForms?

Quote

"old 1990's WinForms"? i just use visual studio 2015 and started up with a windows form. didn't know there was an option for a newer winform?


WinForms is OLD. Virtually 'legacy' and to many of us nearly considered end-of-life. There are still placing building new projects in it, but I wouldn't work for anyplace that wanted to hold me back by two decades. There are probably tens of thousands of legacy WinForms applications in use in countless companies. "If it ain't broke, don't fix it." in other words: If there isn't a financial gain to be made in re-writing them right now why invest the time and money?

But with decades of WinForms comes developers with decades more experience than you'll have and you have no way to compete with them. Let the legacy developer maintain the legacy applications. Many of them are too stuck in their ways to move forward to WPF. Which is why if you look at job boards, what companies are hiring for and have been looking for, for the last several years its: WPF/MVVM.

So if I were you I would just jump right to WPF. Don't worry about WinForms unless you have a deliberate need for it down the line. Otherwise you spend 10,000 hours getting good at WinForms only to find you need to spend another 7,500 hours bringing your skillset forward several years into WPF - And at the same time break all the bad habits you learned in WinForms.

If some company hires you for WPF development, but would also like it if you could maintain some legacy applications THEN learn WinForms; and take your good habits from WPF with you.


FAQ (Frequently Asked Questions - Updated Nov 2015
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 16 February 2016 - 04:06 PM

I have two questions:

  • In Java there are a bunch of primitive types but common usage is to stick to boolean, int, double and char. There are exceptions but unless you have a good reason, it is best to stick to these ones. C# has even more primitives. Is there a subset that is considered the default?


  • More than one tutorial I have looked at recently formats its comments like this:

            //
            // Query Methods
    


    Is there a reason for the empty comment line? Is it a common convention and is it used by any tools?

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 16 February 2016 - 04:14 PM

@cfoley

To your second question: Those are the "defaults" for C# too, with the inclusion of decimal (a base-10 floating number, compared to a binary floating number like double, best used for money or anything else where precision is required). No good reason to restrict yourself to a overly-specific type unless you're dealing with severe memory limitations.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5830
  • View blog
  • Posts: 19,870
  • Joined: 05-May 12

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 16 February 2016 - 08:42 PM

View Postcfoley, on 16 February 2016 - 06:06 PM, said:

  • More than one tutorial I have looked at recently formats its comments like this:

            //
            // Query Methods
    


    Is there a reason for the empty comment line? Is it a common convention and is it used by any tools?


That comment style looks like it is from people transitioning from C++ to C# and don't use #region. A lot of the Microsoft COM samples and MFC generated code used that style to identify sections.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5830
  • View blog
  • Posts: 19,870
  • Joined: 05-May 12

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 16 February 2016 - 08:48 PM

Is there a rule of thumb for when to use in and out when defining generic interfaces?
interface ICanDoThis<in SomeType>
{
    :
}

interface ICanDoThat<out SomeType>
{
    :
}


Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 17 February 2016 - 08:15 AM

I second that question. I've never found it necessary myself to use them, therefore I am confused by the co- and contravariant operators.
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 17 February 2016 - 08:22 AM

So in my transition from VB.Net to C# I've discovered that I do not grasp Object Oriented Programming :(/>/>

I'm trying to learn WPF and I really want to focus on the MVVM pattern so while working through a tutorial I came across this question.

In the code (A demo repository for tutorial purposes) is the following class:
public class EmployeeRepository
{
	readonly List<Employee> _employees;

	public EmployeeRepository()
	{
		if (_employees == null)
		{
			_employees = new List<Employee>();
		}
		_employees.Add(Employee.CreateEmployee("John", "Smith"));
		_employees.Add(Employee.CreateEmployee("Jill", "Jones"));
		_employees.Add(Employee.CreateEmployee("Kathy", "Morts"));
	}

	public List<Employee> GetEmployees()
	{
		return new List<Employee>(_employees);
	}
}


What is the reason for returning a new List<Employee>(_employees) in the GetEmployees method instead of just returning _employees which is already a List<Employee>. Both seem to work exactly the same so I don't understand the reason for the new List<T> copying an existing List<T>
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6341
  • View blog
  • Posts: 25,580
  • Joined: 12-December 12

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 17 February 2016 - 08:39 AM

Co- and contravariance remains an enigma for me, probably because (as suggested) it isn't obvious when we might find a use for such features.

The most obvious clue I can find is a fragment from the docs:



Covariance permits a method to have a more derived return type than that defined by the generic type parameter of the interface.

IEnumerable<String> strings = new List<String>();
IEnumerable<Object> objects = strings;

In earlier versions of the .NET Framework, this code causes a compilation error in C# [and in Visual Basic with Option Strict On]. But now you can use strings instead of objects, as shown in the previous example, because the IEnumerable<T> interface is covariant.



It probably helps to concentrate on understanding covariance, as contravariance is its mirror:

Quote

In C# and Visual Basic, covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments. Covariance preserves assignment compatibility and contravariance reverses it.

Covariance and Contravariance
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 17 February 2016 - 09:02 AM

View PostCharlieMay, on 17 February 2016 - 09:22 AM, said:

What is the reason for returning a new List<Employee>(_employees) in the GetEmployees method instead of just returning _employees which is already a List<Employee>. Both seem to work exactly the same so I don't understand the reason for the new List<T> copying an existing List<T>


It's likely to prevent you from modifying the underlying collection. You don't want to insert into your list somewhere else, and have that show up in the repository.
Was This Post Helpful? 0
  • +
  • -

#11 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 592
  • View blog
  • Posts: 3,823
  • Joined: 12-January 10

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 17 February 2016 - 09:12 AM

why is it sometimes cStr(variable) works but sometimes you have to use variable.tostring()
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 17 February 2016 - 09:19 AM

That sounds more like a VB.NET question to me.

But quoting from this page: http://stackoverflow...-value-tostring

Quote

CStr is a keyword, whereas ToString is a function (method). CStr is compiled inline and it creates code depending on the type of the passed object. It's mainly there for people being used to it from previous VB versions. I haven't used CStr in .Net anymore (because it's not obvious what it does in which situations and it's also not very well documented).

The difference depends on which ToString function you use. Every type can have it's own implementation.


It's likely because whatever type you're trying to call it on doesn't support that particular conversion. Anyway, avoid the "compatibility" keywords IMO.
Was This Post Helpful? 0
  • +
  • -

#13 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 592
  • View blog
  • Posts: 3,823
  • Joined: 12-January 10

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 17 February 2016 - 11:48 AM

I ran into this before:

i had to take the following:

txtConditionValue.Text = conVal.toString();
txtNewConditionValue.Text = Convert.ToInt32(txtConditionValue.Text).ToString();


and make it as follows:

double convertedNewContionValue = Convert.ToDouble(txtNewConditionValue.Text);

//Remove Decimal Points
txtNewConditionValue.Text = Convert.ToInt32(convertedNewContionValue).ToString();



before i made that change it always error out saying you couldnt explicitly convert a string to int-- illegal input string


this is straight from msn site

string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00",
                    " 0", "137", "1601.9", Int32.MaxValue.ToString() };
int result;

foreach (string value in values)
{
   try {
      result = Convert.ToInt32(value);



other than their example is using an array there shouldnt be any difference--

This post has been edited by DarenR: 17 February 2016 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 17 February 2016 - 12:05 PM

Well, their example is incorrect. You can't convert a string representation of a floating-point number to an Integer using the Convert static class.

Or, we can't really say, since you cut off the catch part. Those incorrect numbers were probably included to show that it can't handle certain formats.

Edit: my second guess seems to be correct. The floating point numbers in that string list were there to show how it can't convert them.
Was This Post Helpful? 0
  • +
  • -

#15 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Simple Answers for Simple Questions from Experienced Developers

Posted 17 February 2016 - 01:32 PM

the original link to the code

@DarenR. Look at the output from that MSDN snippet. It clearly stated that the string that "Looks like it represents a Double datatype" is not in a recognizable format to be converted to an Integer. (// The String value '-26.87' is not in a recognizable format.)

I think you're trying to do is imply that since it looks like a double make it a double and convert it to int32 so that it becomes the integer conversion for the double. (ie, 12.57D converts to 13) I'm really not sure how ToString() or even CStr() comes into play here as they would only be needed to put the value back into something that holds a string.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2