It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a model witch is holding different values in my website and i am currently trying to retrieve the value token.
I call my model in the following way:
HoldToken t = new HoldToken();
string token = t.Token;
This is how the model looks
namespace MvcResComm.Models
{
public class HoldToken
{
public string Token { get; set; }
}
}
I am always receiving null as my returned token. I think this is because i am using the new keyword.
How can i instantiate the model HoldToken with out newing it?
Most likely, you're using a constructor-less class and an automatic property.
I'd guess that you're not setting the HoldToken automatic property, which is why you're getting the null.
Add a new parameterless constructor and make sure the Token member is initialised in some way.
public HoldToken()
{
// Set value of token here
// Guessing at how you'd instantiate it.
Token = new Token();
}
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
can i declare two properties with different type but with similar name in C#.
Something like this:
public class Types
{
public string Element
{
get { return ""; }
}
public int Element
{
get { return 0; }
}
}
Similar name? Yes. The same name? No.
The only times you can have two members with the same name declared in the same class are with constructors and methods - and those have to have different signatures for each overload.
From the C# 4 spec, section 10.3:
The names of constants, fields, properties, events, or types must differ from the names of all other members declared in the same class.
If this were allowed, it would be extremely confusing and lead to obviously ambiguous code:
Types types = new Types();
object x = types.Element; // What would this do?
you need to run the code which you wrote and get the error but you may want something similar to below
public class Types<T>
{
public T Element
{
get { return default(T); }
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to access the Scoop.it API via C# to retrieve posts in topics. It's pretty much straight forward, in PHP, how are objects managed in C# and how to you access the properties?
Here's the php code which i'd like to get a C# equivalent of:
$topic = $scoop->topic(24001);
foreach($topic->curatedPosts as $post)
{
echo $post->title;
}
I couldn't find an API reference for Scoop.it in C#, but I would imagine that they conform to the naming standards in C#.
So the PHP code converted to C# would looke like this:
var topic = scoop.Topic(24001);
foreach(var post in topic.CuratedPosts)
{
Console.WriteLine(post.Title);
}
However echo in PHP prints something out to the web-page, so I would imagine that you want to replace Console.WriteLine with Response.Write.
If you have a class that looks like this, where Name is a property:
class Topic
{
public string Name { get; set; }
}
Then you would create an instance of Topic like this:
var topic = new Topic();
Now you can access the property Name like this: topic.Name
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
What is the [something] in
[something]
public class c1 {
}
called in C#? What does it do?
That's an Attribute.
This is known as an attribute application / usage. It associates an instance of a given Attribute with a type. These are user definitable items. For example
[AttributeUsage(AttributeTargets.All)]
public class ExampleAttribute : System.Attribute {
public ExampleAttribute() { }
}
This is an attribute which can be applied on ever place an attribute is legal
// Assembly level
[assembly: Example]
// Class
[Example]
public class C1 {
// Field
[Example]
public int m_field;
// Method
[Example]
public void Test() { }
}
More locations are possible but hopefully this gets the general idea across. You may also want to check out this tutorial
http://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx
Its called an Attribute. A class that ends in "Attribute", and inherits from Attribute:
public class SomethingAttribute : Attribute {
}
If you are creating one, be sure to look up the AttributeUsageAttribute class.
C# Attributes. Please see this documentation.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
In my application i want to be able to send an email to a user. In Email i want to have an URL- link to an application page, but url have to be generic (because i need to be able to get some data from DB, depend on generic part of email).
For example :
I send email to user with URL : www.testpage.com/recetpassword/Qb12T
On load of the page i need to get data from DB and for example say Welcome UserName(which i get from DB).
Bad example, but it explains what i need
Looks like you need to add a new static route in your Global.asax.cs file:
routes.MapRoute(
"reset_password", // Route name
"resetpassword/{id}",
new { controller = "SomeController",
action = "ChangePassword",
id = UrlParameter.Optional
}
);
Then in your Controller:
public class SomeController : Controller {
[HttpGet]
public ActionResult ChangePassword(string id){
/* change password logic/domain calls */
return View(/* some model */);
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am learning C# and I would like to know what is the use of a field in a C# class?
example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Class_Constructor_II
{
class Fields
{
const double RandsInADollar = 7.8;
const double RandsInAEuro = 10.83
public double RandDollarConversion
{
get
{
return RandsInADollar / DollarRands;
}
set
{
DollarRands = value / RandsInADollar;
}
}
public double DollarRands
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
Fields f = new Fields();
f.DollarRands = 14500000;
Console.WriteLine(f.DollarRands);
}
}
}
Nothing better than the MSDN Definition:
MSDN - Fields
In case Microsoft decides to change their MSDN URLs again, here it is:
A field is a variable of any type that is declared directly in a class
or struct. Fields are members of their containing type.
Basically you would use them to contain data that is used internally in your class. Something that needs to exist out of method scope for instance, with a longer lifetime, such as the lifetime of the object.
Making fields public is generally considered a bad idea - you would use properties as you have in your example. However, encapsulation dictates that the rest of the world does not need to know how your class does what it does - so it can use fields to store states and values that it needs to do whatever it needs to do.
take look to this field are class wide variable
Fields