Using a class library variable in an aspx page - c#

I have the following object in a class library:
namespace ID.CentralisedObjects
{
public class Details
{
public string ContactName { get; set; }
}
}
in the aspx page in my website project (which has a reference to the class library), I am trying to print out the ContactName by using code along the lines of
<div><%= ID.CentralisedObjects.Details.ContactName %></div>
However, it is thinking that the ID at the start of this is Page.ID so it isn't finding my object properly. How do I make it look for the class library ID?

standard naming of namespace has enough influence on your application, changing it according to the naming namespaces guidelines will solve your problem
http://msdn.microsoft.com/en-us/library/893ke618(v=vs.71).aspx

For the namespace conflict: use a qualifier
using DrawCharts IDS = ID;
IDS.CentralisedObjects.Details.ContactName
The other mentioned problems are unresolved by this solution.

Related

Mysterious name resolution differences between legacy and SDK-style C# projects

I encountered this confusing names-resolution problem while trying to convert legacy solution with .NET 4.6.1 projects to the new SDK-style project formats. I was able to create a minimal repro solution LegacyVsSdkStyleProjectNameResolution, which is on GitHub here:
https://github.com/PaloMraz/LegacyVsSdkStyleProjectNameResolution.git
The solution contains:
3 legacy projects targeting .NET Framework 4.6.1 residing in the „Legacy“ solution folder; the project names start with „Legacy.“).
3 SDK-style projects targeting .NET residing in the „Sdk“ solution folder; the project names start with „Sdk.“.
(Please forgive me the non-intuitive assembly / namespace / class names – I could not use the original names and was not able to devise something more feasible).
The dependencies are shown in this image:
The problem is, the Legacy.Common.Configuration project successfuly compiles with this Bundle class declaration:
using Legacy.Common.Data;
namespace Legacy.Common.Configuration
{
public class Bundle
{
public Person Person { get; set; }
}
}
But the Sdk.Common.Configuration produces compilation error CS0118 ('Person' is a namespace but is used like a type):
using Sdk.Common.Data;
namespace Sdk.Common.Configuration
{
public class Bundle
{
public Person Person { get; set; }
}
}
Can anybody please explain, why the C# compiler in the Sdk.Common.Configuration project resolves the Person type symbol incorrectly to namespace Sdk.Common.Person, but in the Legacy.Common.Configuration it resolves it correctly to Legacy.Common.Data.Person class?
In SDK-style projects, a project will automatically inherit all of the things that its dependencies depend on. In legacy projects this had to be done manually, with the tooling inserting explicit references to all of the things that a new dependency depended on.
So in your SDK-style project, Sdk.Common.Configuration is inheriting a reference to Sdk.Common.Person.Management, by virtue of the fact that it references Sdk.Common.Data.
Sdk.Common.Person.Management defines the namespace Sdk.Common.Person:
namespace Sdk.Common.Person.Management
{
...
}
While Sdk.Common.Data defines the class Sdk.Common.Data.Person.:
namespace Sdk.Common.Data
{
public class Person
{
}
}
Sdk.Common.Configuration then does this:
using Sdk.Common.Data;
namespace Sdk.Common.Configuration
{
public class Bundle
{
public Person Person { get; set; }
}
}
There, the compiler searches the namespace Sdk.Common.Configuration (and Sdk.Common, and Sdk) for Person before it considers that using. It finds Sdk.Common.Person which is a namespace, and complains because you're using a namespace as a type.
If you include the using inside the namespace, this order flips, and the compiler will search Sdk.Common.Data first, and find the Sdk.Common.Data.Person class:
namespace Sdk.Common.Configuration
{
using Sdk.Common.Data;
public class Bundle
{
public Person Person { get; set; }
}
}
See this answer for details.
As for how to solve this, first off don't define a class and a namespace with the same name. If you can't avoid this, you can either change the location of the using as shown above, or use the fully-qualified name of the Person class.
If you want to mirror the legacy behaviour of Sdk.Common.Data's references not being inherited by Sdk.Common.Configuration, use PrivateAsserts="All" on the ProjectReference which includes Sdk.Common.Person.Management.
<ProjectReference Include="..\Sdk.Common.Person.Management\Sdk.Common.Person.Management.csproj" PrivateAssets="All" />
This states that Sdk.Common.Person.Management is a private dependency, which isn't inherited by anything which references Sdk.Common.Data.

Nunit Test. Visual Studio. Namespace , class issue

.I m very new to programming, and I think I'm loosing my mind trying to understand what's the problem here. Does Anyone have any suggestions?
The Test Project has Reference set to OrderManagementSystem.Domain and OrderManagementSystem.Controllers.
The Controllers Class is Public .
Im able to access Classes from Domain Namespace , but not Controllers??
What did i do wrong?
This error is because your class name is Controllers and namespace also contains .Controllers.
Either update the name of class or remove .Controllers from the namespace OrderManagementSystem.Controllers
In Controllers.cs file,
using Systems;
....
//Remove .Controllers from namespace
namespace OrderManagementSystems
{
public class Controllers
{
//Your code
}
}

MonoDevelop C# in Unity3D trying to understand "Warning: type should be declared inside namespace AssembleCSharp"

In the scripts folder, under assets I am trying to define a simple data class:
File: TirggerObject.cs
using UnityEngine;
public class TriggerObject {
public int TriggerID { get; set;}
public Vector2 Location { get; set;}
public float TriggerRadius { get; set;}
}
The class name has a warning associated with it: "Warning: Type should be declared inside namespace 'AssemblyCSharp'"
As far as I can tell the AssembleCSharp is where the class is defined. The only oddity I see is that the solution file tree in the MonoDvelop main UI shows the class file: TriggerObject.cs as being in the assembly "Assembly-CSharp". While the warning message calls it "AssemblyCSharp", without the dash.
I am assuming I can live with this warning since the code seems to run. But, I am struck by how little I find about Unity's assemblies and I can find no reference to this exact warning. I am also not finding any clear explanation of why this is not already in the right assemble or how I can place it in the right assembly.
I hate not understanding warnings.
This is because your file is physically inside the Unity assembly, but that fact is not reflected in your code. I'm afraid this is Unity's "bad automatic practice" when you create your scripts via Unity's Editor. When you create new files via MonoDevelop, the namespace block is automatically generated.
The solution to the problem is to add namespace block to your file:
namespace AssemblyCSharp {
using UnityEngine;
public class TriggerObject {
public int TriggerID { get; set;}
public Vector2 Location { get; set;}
public float TriggerRadius { get; set;}
}
}
You can actually place "using" statement inside or outside the namespace block. Read more about it here.
Please, be aware that adding the namespace section to one file will force you to modify ALL your code files which are dependent (simply due to class visibility).

Can't case webusercontrol, namespace not found

I have this control:
public partial class controls_UploadedImageView : System.Web.UI.UserControl
{
And in a static function I have this code:
using (var ctl = (controls_UploadedImageView)tmp0.LoadControl("~/controls/UploadedImageView.ascx"))
{
ctl.RenderControl(h);
}
However the cast to `` fails:
The type or namespace name 'controls_UploadedImageView' could not be
found (are you missing a using directive or an assembly reference?)
I can't work out how to cast the control properly so I can set it's properties before rendering.
Update
Turns out that as my project is a website project and not a Web Application, it is causing this issue. A solution is to convert the entire project to a web application but this looks time consuming and fiddly. Does anyone have any solution that doesn't require me to convert the entire project?
I think I have fixed this before by using an interface.
Create an interface in the app_code folder
public interface ICustomControl
{
... add any extra methods here
}
when you declare the class for your user control, include that interface
public partial class controls_UploadedImageView : System.Web.UI.UserControl, ICustomControl
then use that interface.
using (var ctl = (ICustomControl)tmp0.LoadControl("~/controls/UploadedImageView.ascx"))
This is all from memory, but hopefully it gets you close to the solution. I'll check my code later if its not helping.
Try simply adding a namespace to your .ascx.cs (may need to update the .ascx also). Then add a using <namespace>; statement to the class that needs to reference that control.
Ex:
namespace MyFancyNamespace
{
public partial class controls_UploadedImageView : System.Web.UI.UserControl
{
}
}
and...
using MyFancyNamespace; //this goes at the top of your class.
using (var ctl = (controls_UploadedImageView)tmp0.LoadControl("~/controls/UploadedImageView.ascx"))
{
ctl.RenderControl(h);
}

C#.NET Namespace name does not exist in namespace error - only when using is outside local namespace directive - why?

Using .NET 2.0, C#, Windows Forms development, Enterprise Library 3.1.
We have a project namespace (call it Project). We also have several sub-namespaces inside of that project, for example Project.Namespace1, Project.Namespace2, etc.
In one class, we define enums and such to be used with the Enterprise Library Logging block, like this:
namespace Project.Logging
{
public static class Logging
{
public enum LogPriority
{
// enum values here
}
}
}
In another class, I use the enum values so I need to declare a using statement. Same project, so there is no assembly to reference, right?
If I declare the using inside of the local namespace, like this, it works fine:
namespace Project.SomeName
{
using Project.Logging;
// code referencing the Logging enum
}
However, if I put the using statement outside of the local namespace declaration, I get the "type or namespace name 'LogPriority' does not exist in the namespace 'Project.Logging'... Like this:
using Project.Logging;
namespace Project.SomeName
{
// code referencing the Logging.LogPriority.whatever
}
Why is this? Has anyone run across this before?
I have run into similar (though not exactly the same) problems before when using a class that has the same name as its namespace.
Oddly enough it seemed to compile ok on some developers pc's but not on others. In the end we made sure that no namespace contained a class of the same name.
namespace Project.Logging
{
public static class Logging // this is what caused the probems for me
{
}
}
I also had a wired error. I cannot find any namespace which is coming from different assemblies, but begins with executing assembly name.
Finally, I found out that I have set the target framework to .NET framework client profile.
Yes, most likely you have an unusual value set for the "Default Namespace" in your project properties. I would validate the project configuration.
We ran into this issue before and it all went down to ambiguous naming of the namespace and the class name.
When we tried to have our namespace as Services.Web.xxx and also add in a service reference as Services.Web.xxxx and ALSO add a references to an assembly that was named Services.Web.xxx you can only imagine the problems we ran into.
In the end to fix it we simply did a rename to make sure that there was only one instance of the Services prefix
Also you could do the following and create an alias to LogPriority to LogEnum:
using LogEnum= Project.Logging.Logging.LogPriority;
namespace Project.SomeName
{
internal class MyClass
{
public MyClass()
{
LogEnum enum1 = LogEnum.None;
}
}
}
namespace Project.Logging
{
public static class Logging
{
public enum LogPriority
{
None,
Default
}
}
}
It definitely can make a difference if you have usings inside or outside the namespace. There is a good discussion here, and it is likely to be related to your default namespace settings.

Categories

Resources