extension method for substring C# [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
When I write down the code below. I cannot choose my extension method.It doesn't appear. I can't seem to find my mistake. Thanks in advance.
public static class Extensions
{
public static string MySubstring(
this int index, int length)
{
StringBuilder sb = new StringBuilder();
return sb.ToString(index, length);
}
}
class SubstringExtension
{
static void Main()
{
string text = "I want to fly away.";
string result = text.
}
}

It looks like you want your extension method to be based on a string, so you need to make that string your this parameter in your extension method, like this:
void Main()
{
string text = "I want to fly away.";
string result = text.MySubstring(1, 5);
Console.WriteLine(result);
}
// Define other methods and classes here
public static class Extensions
{
public static string MySubstring(
this string str, int index, int length)
{
StringBuilder sb = new StringBuilder(str);
return sb.ToString(index, length);
}
}
Result:
want

Related

c# copy only integer value of variable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Sorry for this noob question I forgot my lesson in c# so bear with me. I have this code
public class TestClass
{
int indexcounter = 3;
public int returnInteger()
{
int temporarystorage = indexcounter;
indexcounter --;
return temporarystorage;
}
}
I first stored value of indexcounter to temporary storage which is 3. So i can return temporarystorage value which is 3. but it returns the value of 2. What's happening here
Well, your code works just as you describe it should work.
BTW, the returnInteger method code can be simplified to return indexcounter--; - see Decrement operator -- for details.
Here's a quick demonstration:
public class TestClass
{
int indexcounter = 3;
public int returnInteger()
{
int temporarystorage = indexcounter;
indexcounter --;
return temporarystorage;
}
// I've added that property so that we can inspect the value of indexcounter outside this class
public int IndexCounter {get {return indexcounter;} }
}
public class Program
{
public static void Main(string[] args)
{
var a = new TestClass();
Console.WriteLine(a.IndexCounter); // prints 3
Console.WriteLine(a.returnInteger()); // prints 3
Console.WriteLine(a.IndexCounter); // prints 2
Console.WriteLine(a.returnInteger()); // prints 2
Console.WriteLine(a.IndexCounter); // prints 1
}
}
You can see a live demo on rextetser.

How can I create more instances of same object in C#? [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
There is a simple code:
class Test
{
public int number;
public void method()
{
Console.WriteLine("Something");
}
}
class Program
{
public static void Main(string[] args)
{
while(true)
{
Test obj=new Test();
obj.number=3;
}
}
}
This program sets the "number" of obj to 3 in every moment. But I would like to create a totally another, unique object with copy of content of the original object in every loop, automatically. If I make an object with same name, it will be overwritten.
Naturally I don't want to use it in an endless loop, it would be meaningless, but it was the easiest way to explain my problem.
You are creating new objects each iteration, but you only keep reference to the last created object.
public static void Main(string[] args)
{
var myOjects = new List<Test>();
int startIndex = 1;
while(true)
{
Test obj=new Test();
obj.number=startIndex;
myObjects.Add(obj);
startIndex = startIndex + 1;
if (startIndex > 5) break;
}
}
Now you can go through all objects in your list:
foreach (var obj in myObjects) obj.method();
There are a couple of ways that you can do this.
My first thought would be to use a factory method (it just returns an instance of a class). It would look something like this.
public Test TestFactory()
{
return new Test() { number = 3 };
}
Or add a constructor to the Test class that takes an instance of Test:
// inside Test class
public Test(Test that)
{
number = that.number;
}

How to change the input array using a TextBox? [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
How to change the array to enter the desired format in TextBox'e?
Here is the code:
private static readonly string[] Extensions = new string[] { textbox1.text };
In the fields of { } must be changed to textbox!
In the text box I want so I can introduce such formats using a comma! txt,png,ico,dll
Or better using List?
List<string> mylist = new List<string>() { "*.txt" };
Wish I could enter my desired formats is not in the code {"*.txt"}! And TextBox'e .
P.S:
private static string[] Extensions = new string[] { "*.txt" };
//public static string[] Extensions;
public static void extractExtensions(string s)
{
Extensions = s.Split(',');
}
And how do I assign values to string [] { "of the textbox"};
It is not easy to understand what you are really having problems with, but try this:
textbox1.Text = String.Join(",", Extensions);
the reverse would be from textbox --> array:
string [] singleExtensions = textbox1.Text.Split(',');
this would give you all the elements which were separated by a , and dump them into the array.
Or if you prefer Lists:
List<string> singleExtensions = textbox1.Text.Split(',').ToList();
EDIT:
Ok since you don't want to post more code, I make a wild guess:
I imagine you have a Windows Forms application and a TextBox in a Form1.
You also should have an instance of the class that you want to use in there.
Let's assume the class is called MyClass and it has an array for the extensions, and a method extractExtensions to extract the extensions:
public class MyClass
{
string [] singleExtensions;
public void extractExtensions(string s)
{
singleExtensions = s.Split(',');
}
}
public partial class Form1 : Form
{
// instance of class:
MyClass _myClass = new MyClass();
}
at a certain point in your Form you want to call the method of your class and pass the content of the textbox like this:
_myClass.extractExtensions(textbox1.Text);
and voilĂ .
EDIT 2:
Say for example you want to extract the extensions with a button click, then you call this method inside the btn1_Click event:
public partial class Form1 : Form
{
// instance of class:
MyClass _myClass = new MyClass();
private void btn1_Click(object sender, EventArgs e)
{
_myClass.extractExtensions(textbox1.Text);
}
}
if you made this method static like in your post you would call it like this:
MyClass.extractExtensions(textbox1.Text);
Assuming you have a list of desired extensions
var extensions = new List<string> { ".txt", ".png", ".dll" };
you can join the items to compose a string and show it in a text box
txtExtensions.Text = string.Join(",", extensions);

global array won't read in to console window c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Schedule.cs
public static class SchArray
{
public static string[] clientName = new string[20];
public static DateTime[] startDate = new DateTime[20];
public static DateTime[] endDate = new DateTime[20];
public static string[] allocatedDriver = new string[20];
public static string[] depot = new string[20];
public static int count = 3;
}
public void schedule()
{
SchArray.clientName[0] = "eric cartman";
SchArray.clientName[1] = "peter griffin";
SchArray.clientName[2] = "homer simpson";
SchArray.startDate[0] = Convert.ToDateTime("2016,3,2");
SchArray.startDate[1] = Convert.ToDateTime("2016,3,4");
SchArray.startDate[2] = Convert.ToDateTime("2016,3,5");
SchArray.endDate[0] = Convert.ToDateTime("2016,3,3");
SchArray.endDate[1] = Convert.ToDateTime("2016,3,5");
SchArray.endDate[2] = Convert.ToDateTime("2016,3,6");
SchArray.allocatedDriver[0] = "owen";
SchArray.allocatedDriver[1] = "daniel";
SchArray.allocatedDriver[2] = "owen";
SchArray.depot[0] = "depot1";
SchArray.depot[1] = "depot2";
SchArray.depot[2] = "depot3";
}
Work_Schedule.cs
public void schedule()
{
Console.Clear();
Console.WriteLine(" Create Work Schedule ");
Console.WriteLine(Schedule.SchArray.clientName[0]);
Console.ReadKey();
}
Console.WriteLine(Schedule.SchArray.clientName[0]);
^^^^^ this line should display the name the eric cartman, i've debugged it and it says there are no objects in the array, they're are all null.
You need to create an object of Schedule since the array elements ware initialized inside the constructor:
Schedule scheduleObject = new Schedule();
Console.WriteLine(SchArray.clientName[0]);
For this particular scenario, i would like to suggest a different approach of creating a static list/array of objects. Let me modify the class as like the following:
public class SchArray
{
public string clientName;
public DateTime startDate;
public DateTime endDate;
public string allocatedDriver;
public string depot;
public int count = 3;
}
And i have a List<SchArray> defined as static;
public static List<SchArray> StaticSchArray = new List<SchArray>();
Then i can populate the list as like the following:
StaticSchArray.Add(new SchArray() {clientName="eric cartman",
startDate=Convert.ToDateTime("2016,3,2"),
endDate= Convert.ToDateTime("2016,3,2"),
depot="depot1",allocatedDriver ="owen" });
Similarly other elements can also be added to the array. this will be a better option for this scenario.

C# array size not defined by integer variable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
namespace OO_Assign_2_eDepot_
{
public partial class Driver
{
int driver_size = 1;
public string[] d_username = new string[driver_size] {"john"};
public string[] d_password = new string[driver_size] { "pass1" };
}
}
driver.size is not recognized as the variable size in the arrays
Choose one:
public partial class Driver
{
int driver_size = 1;
// Set size
public string[] d_username = new string[driver.size];
// Set value
public string[] d_password = new string[] { "pass1" };
}

Categories