tags:

views:

152

answers:

8

I'm new to C#, so forgive this noobish question. I'm playing with a simple XNA game demo. I have a struct that I want to be available to several classes. It is defined as follows:

PhotonType.cs

using System;

namespace ShipDemo
{
 public struct PhotonType {
        public Color tint;
 }
}

In another file in the same folder/namespace, Ship.cs, I reference this struct:

namespace ShipDemo {
    public class Ship {
    //...
       private PhotonType photonType;
       //...
       public Ship(float x, float y, float ang, Boolean correctSound, PhotonType photonType) {
    //...
  }
}

This gives me compilation errors on both references to PhotonType.

Error 1 The type or namespace name 'PhotonType' could not be found (are you missing a using directive or an assembly reference?)

What am I doing wrong here?

////

Also, the C# documentation says

It is an error to initialize an instance field in a struct.

But what if I want to provide default values?

I'm using Visual Studio Ultimate 2010 Beta.

+1  A: 

Have you included the namespace that encloses PhotonType? e.g. via "using"

using MyNamespace.MyLibThatContainsPhotonType;
RandomNoob
Both files are in the same namespace.
Rosarch
A: 

The tip is in the error message you recieved.

... missing a using directive ...

Add using $Namespace$.ShipDemo to the top of your code in which you want to use the PhotonType type.

EDIT:

Both files are in the same namespace.

In your sample code you've said namespace ShipDemo ... in both examples. Assuming you're using different files, this'll be more trouble than it's worth.

Try the following.

PhotonType.cs

 using System;

 namespace ShipUtils
 {
      public struct PhotonType
      {
           public Color tint;
      }
 }

Ship.cs

  using ******.ShipUtils;

  public class Ship
  {
       private PhotonType photonType;
       public Ship(float x, float y, float ang, Boolean correctSound, PhotonType photonType)
  }
Daniel May
A: 

sounds like you need

using ShipDemo;

at the top of your Ship.cs file. Or else ensure that your Ship class is wrapped in the ShipDemo namespace:

namespace ShipDemo
{
    public class Ship
    {
        //...
    }
}

either that or we need you to post more code for us to look at.

Steve Elmer
They are both in the same ShipDemo namespace.
Rosarch
+3  A: 

Are the 2 files in the same Project? If your namespace is split up between 2 projects, you need to reference the project containing PhotonType in the other project.

I fthis is the case, I would question the design as usually we don't split the same namespace in more than one assembly.

Marcel Gosselin
It looks like they aren't, actually, but that is unintentional on my part. How can I add the new file to the project? Did I create it incorrectly if it wasn't in the project initially? I just chose **File** >> **New** >> **File** >> **Visual C# Class**
Rosarch
figured out how to add existing file: right click on project name, **Add** >> **Existing Item**
Rosarch
You can also just drag the files between projects or move them in the file system click the `Show all Files` button and then `Include` the file into the project. (This is handy when you want to move several classes at once)
Matthew Whited
@rosarch If you're sharing types between assemblies in the same solution you shouldn't be copypasting them between projects. You should place them in a single project and then add a Reference to that project in order to use that type elsewhere. Having the same type defined in different projects will cause odd bugs to crop up in your code.
Will
@Will I'm not sure Rosarch is sharing files between 2 assemblies but rather created the file without including it into the project. Remember he said he is a newbie in C#. If he does share the file, your comment is really true though.
Marcel Gosselin
A: 

If this part of a multi-target XNA project (Windows/XBox/Zune)... If so, one of the files may not be included in the project. If this is not the case you might want to double check that the Build ACtion for PhotonType.cs is set to Compile (this is one the Properties pane after you click on the file in the Solution Explorer

Matthew Whited
+3  A: 

Your assumptions are incorrect. Stop making assumptions.

  1. Make absolutely sure you have compiled and are using the latest compiled versions of your assemblies
    • Clean your solution. Recompile your solution. Test again.
  2. If Ship and PhotonType are in two different assemblies (i.e., projects)
    • Clean and recompile.
    • Ensure the assembly containing Ship references the PROJECT containing PhotonType.
      • It is a common bug to reference the DLL of another project and not the project
      • This causes you to reference OLD versions of the project and not the latest
      • Remove any reference to the PhotonType dll and re-add a reference to its PROJECT
  3. If they are in the same solution
    • Clean and recompile.
    • Ensure the PhotonType file is compiled
      • Right-click PhotonType.cs in the project explorer and select Properties
      • Build action should be Compile
  4. If you're still having this issue, your project could be messed up bad
    • It might have noobitis. We have all gone through this when starting out.
    • Create a new solution, add new projects to it, and add new items for Ship and PhotonType
    • See if everything compiles correctly. Compare this new solution to your old one to see what's different

You probably did something odd along the way. Its hard to say exactly what without seeing your project. If none of this works, I'd suggest zipping up your project and uploading it somewhere. It may take a minute to see what's wrong by looking at the project.

Will
+1  A: 

Re: the very last part of your question: If you want default values for your struct, I think a (somewhat) standard approach is to create a static readonly variable called something like Default that will be initialized with the values you want. Static readonly variables can be initialized in a class or struct's static constructor.

public struct PhotonType {
    public static readonly PhotonType Default;

    public Color tint;

    static PhotonType() {
        // from here on out, PhotonType values initialized to PhotonType.Default
        // will have their tint set to Color.White
        Default = new PhotonType();
        Default.tint = Color.White;
    }
}

Then to get the default value you'd simply do this:

PhotonType pt = PhotonType.Default;
Dan Tao
A: 

While Dan has a good solution to how to provide default values, the truth is, if you want to provide default values then you should be using a class and not a struct.

Structs are a group of data that has no "smarts", while classes are groups of data that knows how to work with that data.

Sounds like you want a class to me.

Hogan