开发者

Structs in C# (type could not be found)

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 dir开发者_开发知识库ective 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.


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.


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.


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

using MyNamespace.MyLibThatContainsPhotonType;


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;


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.


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


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)
  }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜