Class level variable? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I get this error object reference is required on clindID in the submethod
Why can't I access the string clientID in the sub class Methods? I'd like to use it in multiple methods.
class Remote
{
public string clientID
{
set{} get { return this.clientID; }
}
public bool validClientId()
{
clientID="32";
return true;
}
// closing bracket?
Or would it be better to use
string clientID="";
which doesn't work either

You don't have a setter implemented.
public string clientID
{
get { return this.patientID; }
set { this.patientID = value; }
}

Because you didn't reference the variable using an object reference in your validClientId() method (as described by T McKeown's answer), your code is looking within the scope of the validClientId() method itself to find that variable. It can't find it because the variable hasn't been declared within that scope. Try including the object reference as described by T McKeown to force the compiler to look within the this object for that variable.
Also, your class brackets aren't closed. This might just be a problem with your example code, but you need a closing curly brace }

Related

Is there a good practice of setting default values in an constructor? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Consider a simple class
public class MyClass
{
private int myProperty
...
public int MyProperty
{
get
{
return myProperty;
}
set
{
// some evaluation/condition
myProperty= value;
}
}
...
}
Now, if I want to create an empty constructor where I set default values for the class properties I could do this either this way:
public MyClass()
{
myProperty = 1;
...
}
or this way:
public MyClass()
{
MyProperty = 1;
...
}
Both examples seem valid, since I would never set a default value, that doesn't meet the requirements in the setter evaluation.
The question is, is there a best practice or doesn't it matter anyway?
What would be the advantage of one or the other be (as I can't find any)? Is there some reference, where this question is adressed?
So far I have come across code from many different developers that use either or both ways...
You can use both. But i prefer the first one. Why? Because the value that the property uses is directly assigned. For C# 6 above, you can use default value in a property directly without using constructor.
public class Person
{
public string FirstName { get; set; } = "<first_name>";
public string LastName { get; set; } = "<last_name">;
}
I personally like to set it as you done in first block.
For me it serve as additional fact of method is constructing object, not using alredy constructed. Also it makes me sure that properties is not called (they transform to set/get functions which results in couple of excess instruction).
But i believe that both variants are valid and maybe compiler optimizes properties to direct assignment.
For simple data first method is ok. But on more complex data, you could have a condition in the set (depending to another variable for example, set { if (Config.TestEnv) ...} so if you directly set the private value, you could be in trouble.

Correct Description of "class" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
public class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.WriteLine(test.GetType()); // -> it´s Test, of course
int a = 5;
Console.ReadLine();
}
}
public class Test
{
public string Name;
}
In the above case
the Type of "test" is Test, right?
And in this case the Type of "a" is int, named primitive data type,
public is an access modifier
So my Question now is, what is class then? A keyword? A primitive Datatype? Neither, Nor?
Please, don't explain my, what a class is doing or what's the difference between class or object, I know a class is like a construction plan.
Class is a keyword that signifies to the C# compiler that a class is being declared. See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

Other options than setting IEnumerable to optional [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
From what I have read IEnumerable cannot be set to optional. I have a method public IEnumerable<StatusInt> StatusInts { get; set; } that always passes in the default of 0 if the user does not select an option from a combo box. For simple data types I can just add ? and the parameter becomes optional and null is passed in. Is there anything similar that I can do with IEnumerable
Value types can be set to optional with the ? character.
int?' is just short for Nullable<int>
IEnumerable is not a value type, it's a reference type. Reference types are already optional - you can return null if you want.
IEnumerable<StatusInt> StatusInts
{
get
{
if(this.OptionIsSelected)
{
return GenerateOptions(this.Option);
}
else
{
return null;
}
}
}
However, I recommend you don't do this. Instead, return an empty enumerable. This way your calling code does not need to check for a null value. You are less likely to inadvertently trigger a null reference exception.
IEnumerable<StatusInt> StatusInts
{
get
{
if(this.OptionIsSelected)
{
return GenerateOptions(this.Option);
}
else
{
return Enumerable.Empty<StatusInt>();
}
}
}

Increase the Scope of Passed Arguments [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a handful of variables passed from one form to another, but now I'm realizing these variables can't be accessed outside the form method.
public Form3(int str, int dex, int vit, int arc, int hp, int mp, int sp, string name, string charClass)
{
...
}
I'd like to be able to access the arguments from other methods. Is it possible to increase the scope of these arguments within the class itself, or do I need to go to the roots and declare them differently?
Make them class-level members. For example:
public class Form3
{
private int Str { get; set; }
public Form3(int str)
{
Str = str;
}
private void SomeOtherMethod()
{
// here you can access Str
}
// other methods, etc.
}

How do i make that when I make an instance of a new class, it must get a parameter? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
For example:
class WebCrawler
{
List<string> currentCrawlingSite;
List<string> sitesToCrawl;
RetrieveWebContent retwebcontent;
public WebCrawler()
{
}
}
When I make WebCrawler = new WebCrawler(parameter here)...
Add another constructor to your class;
public WebCrawler(parameter here)
{
}
After that, you need to remove parameterless one constructor, so people can create an instance of your class without provide any parameters.
You can create an instance of it like
WebCrawler w = new WebCrawler(parameter here);
You can read for more informations from Instance Constructors
Here is a DEMO.
Create constructor with parameter you want to be provided by user:
public WebCrawler(string param1, int param2)
{
}
When any constructor like that is added the default one (parameterless) is no longer available, unless you write it yourself:
public WebCrawler()
{
}
So just remove it, and user won't be able to create your class instance object without providing these parameters. You can also make the same setting parameterless constructor private or protected.
Instance Constructors (C# Programming Guide)
You can make the parameter-less constructor private...
private WebCrawler()
{
}
Meaning no consumers/callers have the ability to call it.
You then have only one constructor that they can use:
public WebCrawler(string something)
{
}
Add another constructor accepting a parameter:
public WebCrawler(string someParameter)
{
}

Categories

Resources