Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, April 6, 2016

Installing XNA for Visual Studio 2015

I was asked about installing XNA into Visual Studios 2015 because I noted that it was very simple and straightforward.

I knew that there were others like me who are still using XNA so I knew that a Google search would do plenty to help me find how to get my project working in VS2015 (community)

My search quickly brought up a a Stack Overflow answer that points to an updated post by MXA with the title "XNA 4.0 Refresh (Visual Studio 2015)", which was last updated in November of 2015.

This site gives you a single downloadable .zip file to download, and then instructions on what you need to do to install all the components necessary for installing the extension itself

Once you have unpacked the .zip file, just install the pieces one by one in the order given, they don't take long at all.

Before install extension:
  1. Install DirectX
  2. Install Xna Framework 4.0 Redistribution
  3. Install Xna Game Studio 4.0 Platform Tools
  4. Install Xna Game Studio 4.0 Shared
Once the last one finishes, just double click the extension and it will install it, and you are ready to roll.  

I wasn't paying complete attention to my computer while I was doing this, so I can't give you a good estimate about how long it really took.  the download was painfully slow because of the connection that I was on, but the installing the packages didn't seem to take very long at all. 

My Project was already created prior to installing XNA into VS2015, so I don't know how easy it is to get started or set up the different types of projects.  I look forward to doing some development in XNA though.


Windows/XboX/Windows Phone Game

With the recent Microsoft Build event showcasing a lot of mobile development and Gaming development, I really got to thinking about the game where I started with a basic tutorial and expanded the game play dramatically.  

Currently it is a 2D scroller type game, but it scrolls in all directions on a defined World(Space/Universe) map.  My goals up until now had been to further develop the 2D game with the final goal being to interpret it into a 3D game that would support online game play.  The more I thought about the game and the aspect of mobile development along with flying quadcopters I came up with the idea of creating an interactive game play where the user could control the player by turning or tilting the screen in order to turn the player piece.  I think this is a wonderful idea for the small game that I have so far and would make it very playable for the mobile platform.  my thoughts are that once I created the game for Windows phone that I could port it to the Android market immediately once I figured out the best way to do that.  

I haven't yet looked into porting to the Android devices but I don't think that it should be too difficult given that Xamarin is now free with Visual Studios and there is a current build of XNA for Visual Studios 2015 as well. 

Over the weekend I updated Visual Studios 2015 Community edition and installed the Xamarin portion, I also installed XNA into Visual Studios 2015 which was a lot easier than it had been in the past to install into other versions of Visual Studios.

I do have a lot of code to clean up, I keep trying to tell myself not to be so hard on myself, I did write the code many years ago and didn't know as much about coding standards and such as I do now.

I am also going to be looking into Azure for testing and hosting for the game itself.  I am not going to lie to you, but the main reason is because of the demo during build for Age of Ascent,  I was really impressed with what they showed there and the way that you can push out updates to the code to different servers that are all hosting the same game instance.  

I am looking forward to implementing some more code into my game and making it enjoyable for everyone. 

I am not so much of an Apple person, but I have also thrown around the idea of porting it to the iPhone market as well, but that is for another set of posts, I am sure there is going to be a lot going into that as well. 

Tuesday, March 29, 2016

Null-safe Dereference in C# 6.0

Wow, how did I make this far and not know about this?

I love these, although I am still unsure as to how clean they make the code, or if they are going to make me a lazy coder by using them as opposed to what I have been doing.

I came across the null dereference because I was reviewing some code on my favorite code review site, CodeReview.  The code is being used to track downloaded applications in a set of applications that are being downloaded at one time.


if (lastRuntimeDetails != null) //at least one application was already downloaded
    if (lastRuntimeDetails.Ip != runtimeDetails.Ip || 
        lastRuntimeDetails.Port != runtimeDetails.Port)
            _requestExecutor?.Disconnect();

As I was reviewing this code there was an awkward if statement that was nested without brackets (yuck) so that was my first line of business.  The objects that were being compared were being created from a custom class called RuntimeDetails, the first object that is created is called lastRuntimeDetails and is being used to track the last application to make sure that the same application doesn't get downloaded a second time and to keep track of the IP Address and Port for each download. 

Anyway, at first I wanted to get rid of the outer if statement, it looked to me like it was extraneous, at first I was thinking that if the properties were null that they just wouldn't be equal to each other and give a false value to the condition statement.  

Comments were made about Null Reference Exceptions and a lot of thought went into how to do this, I kept thinking about the null properties, and that's when I found Null Dereference Operator, so I set up some tests to make sure that I had a good grasp on things

I created two classes, one to create objects and the other to return comparisons on the objects



public class Class1
{
    public string One { get; set; }
    public string Two { get; set; }
    public Class1()
    {
    }
}

public static class Class2
{
    public static bool returnBoolean(Class1 inputA, Class1 inputB)
    {
        return (inputA?.One == inputB.One);
    }
    

    public static bool checkOnNullProperty(Class1 inputA, Class1 inputB)
    {
        return (inputA.One == inputB.One);
    }

}

and then I eventually came up with the following tests


[TestMethod]
public void TestMethod1()
{
    var input1 = new Class1() { One = "string" };
    var input2 = new Class1() { Two = "string B Two" };
    var test = Class2.returnBoolean(input1, input2);
    Assert.IsFalse(test);
}

[TestMethod]
public void TestMethod2()
{
    var input1 = new Class1() { One = "string" };
    var input2 = new Class1() { One = "string B Two" };
    var test = Class2.returnBoolean(input1, input2);
    Assert.IsFalse(test);
}

[TestMethod]
public void TestMethod3()
{
    var input1 = new Class1() { One = "string" };
    var input2 = new Class1() { One = "string" };
    var test = Class2.returnBoolean(input1, input2);
    Assert.IsTrue(test);
}

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void TestMethod4()
{
    var input1 = new Class1() { One = "string" };
    Class1 input2 = null;
    var test = Class2.returnBoolean(input1, input2);
    Assert.IsFalse(test);
}

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void TestMethod5()
{
    var input1 = new Class1() { One = "string" };
    Class1 input2 = null;
    var test = Class2.checkOnNullProperty(input1, input2);
    Assert.IsFalse(test);
}

[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void TestMethod6()
{
    Class1 input1 = null;
    Class1 input2 = new Class1() { One = "string" };
    var test = Class2.checkOnNullProperty(input1, input2);
    Assert.IsFalse(test);
}

Someone pointed out that if the object itself is null it will throw a Null Reference Exception, once they said that I looked at the code again and sure enough the container was created but no object was placed inside of it.


 RuntimeDetails lastRuntimeDetails = null;

So my initial thought of removing that outer if statement was wrong, but now I had new information that could be used to make sure that the Properties didn't throw a Null Reference Exception if the object was created but the property were null.

So here is the code that I suggested to replace the original code.


if (lastRuntimeDetails != null)
{
    if (lastRuntimeDetails?.Ip != runtimeDetails?.Ip ||
        lastRuntimeDetails?.Port != runtimeDetails?.Port)
    {
        _requestExecutor?.Disconnect();
    }
}

The object could be created with null properties now and then we could remove the outer if statement entirely.  I believe that the better solution is to set the properties on creation to some default and then when they are assigned they will be different from the default so the comparison could be made without the need to check for nulls thus removing the chance that future development would have to worry about checking for nulls.  or that the initial creation of the object, instead of being set the object to a null object, we create the object with null properties and then if the object's properties are null then they will give a false when compared to anything other than null (using the Dereference)

The Code Review Question in Question

and

My Answer

Thursday, September 25, 2014

Confusing Errors for a Confusing Feature? I disagree.

I follow Eric Lippert's blog "Fabulous adventures in coding" and his post today (Confusing errors for a confusing feature, part one) talks about a "confusing feature" and it's "confusing errors" of which I disagree.

I don't think that these errors are confusing at all, let me give you a little background of the issue before we begin.

Eric speaks about a feature in C# that requires unique meaning of variables throughout a block of code, something that means that you can't create a variable with the same name twice in the same block of code(scope), and you can't create a variable of the same name in a child scope either because it would make the variable ambiguous.

Eric says that he has a love/hate relationship with this feature, it keeps him from coding bugs into his applications occasionally.

What Eric doesn't like about this is the error that occurs when you try to declare a variable twice in the same scope.


 class C
 {
     static void M1()
     {
         int x2;
         {
             int x2;
         }
     }
 }


Gives an Error on the Inner x2:

error CS0136: A local variable named 'x2' cannot be declared in
this scope because it would give a different meaning to 'x2', which is
already used in a 'parent or current' scope to denote something else
Here is the direct quote from Eric after he says this,
It is no wonder I get mail from confused programmers when they see that crazy error message! 
What is crazy about that error?  It's in plain English, if you declare a second variable named the same as a previous variable it will overwrite the previous variable.  If this was something that the coder did intentionally then they should know that they don't need to declare the variable a second time and that they should just reassign the variable, on the other hand, if it was unintentional then it is probably bad naming on the part of the programmer.

I can see this being an issue when looking at some of the bad naming schemes that come across Code Review.  I know that the example code is just example code, but we come across code where the variables are listed nearly in alphabetical order

 class C  
 {  
     static void M1()  
     {  
         int a;  
         int b;  
         int c;  
         // ...  
         int x2;  
         {  
             int x2;  
         }  
     }  
 }  

and the meaning behind the variable is a lucky guess or only known to the original coder, kind of like Magic Numbers, but this is straying from the point of both posts.

The second thing (and third thing) that Eric says about this error is

And while we’re looking at this, why is 'parent or current' in quotes, and why doesn’t the compiler differentiate between whether it is the parent scope or the current scope?
In which I reply, the variable exists in both scopes, parent and child, meaning that where the error occurs the variable is in the current scope as well as in the parent scope, it already exists in scope.

This all points to naming, why should you need two variables named the exact same thing in the same scope?  You shouldn't it points flaws in the logic of the code you are trying to write.

RubberDuck from Code Review has from time to time pointed to one of Joel Spolsky's post titled "Making Wrong Code Look Wrong" and I think that trying to declare the same variable twice in the same scope falls under things that look wrong, because it is wrong and is a bad programming habit that C# didn't want invading from C++.

After those two statements Eric continues on to the next point and doesn't explain why doing this is a bad thing or how to keep from making this error in the future.

If we flip things around and try to declare the variable after one has been declared inside of a child scope the error message is a bit different.


 static void M3()  
 {  
     {  
         int x4;  
     }  
     int x4;  
 }  

Produces the following error:

error CS0136: A local variable named 'x4' cannot be declared in
this scope because it would give a different meaning to 'x4', which is
already used in a 'child' scope to denote something else
The compiler creates locations for all the variables ahead of time, scope is a means of use, or a map. If we have two cities right next to each other, with the same name in the same state, but in different counties it would be confusing regardless of which one is seen first, so why is this concept so confusing?

--------------------------------------------------------------------------------------------------------------------

I am sure that there is method to his madness in this post seeing that it is "part one" in the series of posts.

This post is more me thinking out loud about the whole thing and I should note also as a disclaimer that I didn't follow the directions and read the post he linked to before writing this post, for everyone's sake here is the link

Simple names are not so simple, part one

I look forward to the life long journey of learning, and the next post (whoever's it may be)

CodeReview User: Malachi