Increase the Scope of Passed Arguments [closed] - c#

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

Related

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/

Two methods, do the same thing, in different ways [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 5 years ago.
Improve this question
Hi I'm new to programming and I got this code:
public void Print(out string dataToPrint)
{
//code....
dataToPrint = "some text here"
}
And:
public string dataToPrint()
{
//code
return "some text here"
}
Which one will be used today, and which example will a professional programmer will use and what is the fastest in terms of performance?
In terms of performance there is nearly no difference, you can try it by running the each method inside a loop and test the time each can take, or use benchmark.net .
I tried it with Benchmark.net using following code:
public class Class1
{
public void Print(out string dataToPrint)
{
dataToPrint = "some text here";
}
public string Print()
{
return "some text here";
}
[Benchmark]
public void one()
{
string data;
Print(out data);
}
[Benchmark]
public void two()
{
Print();
}
}
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Class1>();
}
}
And the result was :
As you can see the difference is too small, so you shouldn't consider it in your case, but I prefer the second form for readability, however, for other cases try using same procedure and find out.

How to check how many instances created for a type in c#? [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 6 years ago.
Improve this question
How can I check that number of instances created for a particular type and how much memory each instance occupied. Please give an example.
As suggested in the comment i will add an example
public class Myclass
{
private static long Count;
public Myclass()
{
Interlocked.Increment(ref Count);
}
}
This will only work for your own class, In this way you can't find out the number of instances of System.String for example.
To find out the size of a class, You should use this using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public class MyClass
{
public int MyField;
public int MyField;
}
And:
int sizeInBytes = Marshal.SizeOf(typeof(MyClass)); //return 8
Also you have here a list with size of int, byte, etc.

Class level variable? [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
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 }

C#, I want to call a method with parameters like var test = new test1.test2.Method("parameter1","parameter2") [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 9 years ago.
Improve this question
I want to call a method in a fixed structure like below in C#:
var Test = new test1.test2.test3("parameter1","parameter2");
Is this possible in C#?
Here test1 and test2 can be classes and test3 is my method name and it will return string text.
I can manage if need to remove new keyword.
I am assuming it should something like this :-
Public class test1
{
Public class test2
{
Public string test3("parameter1","parameter2")
{
//Do something
}
}
}
Yes, you can if test3 is public structure type nested inside public structure type test2 which is nested inside test1
struct test1{
public struct test2{
public struct test3{
public test3(string p1,string p2) {/*do something*/}
//some params
}
//some params
//some params
}
Also test1 and test2 could be namespaces.

Categories

Resources