|
I have an array of string which is
string[] myarr = new[] { "a", "b", "c" };
Can you please help me how to get this result?
a
ab
abc
I tried this one but I just don't know how to get the value from the array and add it on my list
string[] myarray = new string[] { "a", "b", "c" };
List<string> resultarray = new List<string>();
int i, j;
for (i = 0; i <= myarray.Length; i++)
{
for (j = i; j <= i; j++)
{
}
}
|
|
|
|
|
A simple loop: print the first character, print the first and the second, ...
|
|
|
|
|
Member 13081065 wrote:
for (j = i; j <= i; j++) What values do you think j is going to take within that loop?
Try debugging your code and stepping through the loops. Do the values you see in the debugger match what you were expecting?
NB: You don't need an inner loop at all. String.Join[^] has an overload which lets you specify the range of elements from the array that you want to join. For example:
string[] input = { "0", "1", "2", "3" };
string output = string.Join(string.Empty, input, 0, 3);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You get a value from an array by using an index - a numeric value that ruins from 0 to (N - 1), where N is the number of elements in the array. So valid indexes for your myarr array woudl be 0, 1, and 2.
If you want "a", then you use myarr[0]
If you want "b", then you use myarr[1]
If you want "c", then you use myarr[2]
That index value can be in a variable:
int index = 2;
Console.WriteLine(myarr[index]); Would print "c" for example.
Think about what your tutor wants you do to, and work out how you would do it manually - that may help you work out what your code needs to do!
Give it a try - this isn't complicated!
Hint: you only need one loop!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
string[] myarr = new[] { "a", "b", "c" };
string s = string.Join( "", myarr);
for ( int i = 1; i <= s.length; i++ ) {
Console.Writeline( s.Substring( 0, i ) );
}
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hello, I am working on a project where I have to make connections with a smartwatch (Samsung gear s3) and a Unity application, then use the heart rate data in the Unity app. Any help is appreciated since I am experiencing problems making it work. Thank you in advance! ^^
|
|
|
|
|
|
Well the problem is that I have never worked before with creating connections of such kind and I don't even know where to begin with. I am currently looking for a client that could establish these connections but still do not know how...
|
|
|
|
|
My wallet is empty ... can you put some money in it? I'm having problems filling it.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I suspect that one will make a loud whooshing noise as it flies quickly above him ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm trying to get into their zone ... the twilight zone.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
What is the difference between Show/Hide or Visible/Hidden"? I have some windows I need to keep running in the background. Searching for something yesterday I came accost a comment that said that Show/Hide are obsolete in a Wpf and I should be using the other. I cannot really find a difference except that Visible/Hidden has an event you can tie to the change.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
If this is a WPF question, then it should be asked in the appropriate forum: Silverlight / WPF Discussion Boards[^] - this is a "Generic C#" forum.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I reposted it under the forum you suggested. I almost put it there in the first place but some references showed it was part of C#.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
I have an idea for a program using the picture viewer control. I would like to have a label at the top of the form that says "Choose a photo of the musicians listed below to see a photo."
Then below that I would make four option buttons. And next to each option button put a musician's name.
Then below that on the bottom have two buttons. One buttons says "Show photo." The other button says "Clear image."
Problem is all the Youtube videos I've seen that demonstrate the picture box have you using open file dialog and it looks like you have to manually browse into the folder on your drive to select the photo.
I don't want to do that. I want my program to be automatic. No browsing for a photo. You select an option and then hit the button "Show photo" and the program shows the photo in the viewer.
But I don't know if this is possible so let me know please.
|
|
|
|
|
Yes, it's possible: you do not need to use a OpenFileDialog to access the image, that just provides you with a path to the file.
If you want to load the file from a specific location, then you can just specify that:
byte[] data = File.ReadAllBytes(@"D:\Temp\MyImage.bmp");
MemoryStream ms = new MemoryStream(data);
Image im = Image.FromStream(ms); Or you can load from the application EXE folder:
byte[] data = File.ReadAllBytes(@".\MyImage.bmp");
MemoryStream ms = new MemoryStream(data);
Image im = Image.FromStream(ms); This has disadvantages in that you can't modify the image reliably as the folder is likely to be read only in production.
Or you can add the image as a resource and embed it in your application:
Image im = Properties.Resources.MyImage; And now you can't change the image from run to run of your app at all because it is part of your application EXE file.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks. But I wish I understood what those lines of code mean. The only thing I understand is where you added the path to the image. That makes sense. But byte[] data and File.ReadAllBytes I don't know the why of that.
And I don't know what MemoryStream ms is either.
If you could perhaps explain what these lines do I would appreciate it.
|
|
|
|
|
Oh come on! Are you trying to run before you can walk? This is pretty basic stuff!
You know what a byte[] is, don't you? It's an array of bytes, is all. So what do you think that File.ReadAllBytes does? And if you don't understand what a function does, look it up in the official documentation: File.ReadAllBytes(String) Method (System.IO) | Microsoft Docs[^]
The next two are there for a specific reason: while you can load an image directly from a file, that locks the file until the image is Disposed - so if you want to write a modified version back to the file in your app, you can't. Converting the data to a Stream gets round that, by locking the Stream instead of the file. And if you don't understand what a Stream is then how on earth are you doing any file input / output?
So those three lines do this:
1) Read the disk-based image file into an array of bytes.
2) Construct a memory-based Stream from the array.
3) Create an Image from the Stream.
Seriously, if you don't understand stuff like this, then you need to re-read your course notes from the beginning, because you have missed a heck of a lot of useful stuff!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Come on now. I only program as a hobby. I've only done console programs so far. There is a lot of stuff I have not done. You programmers need to have some more understanding of what it's like not to be a full time developer.
If you came to me for guitar lessons I too could slice you to ribbons for all that you don't know. But that is a very poor way of teaching guitar.
Why would I need a byte array anyway. Just load the photo into the viewer. My photos will not be changing.
|
|
|
|
|
Dioxazine wrote: If you came to me for guitar lessons I too could slice you to ribbons for all that you don't know.
Yes, because I know nothing.
Dioxazine wrote: But that is a very poor way of teaching guitar.
And so is what you are doing.
Suppose I came to you for help with my guitar playing, and told you that I was learning on my own by guessing what to do and wondering why I can't play as fast as others - but the forks and spoons I jam under the frets to select the notes to strum take me too long to pull out and move?
Clearly, I don't know enough to play the guitar because I've spent so long guessing what to do instead of sitting down and actually learning the right way!
And that's where you are with development. You've found code that does some of what you want, but you don't understand how it works - or even exactly what it is doing - so you can't change it to do something else yourself. And for the same reasons as me with my guitar spoons - they work for me, but not very well - I need to be taken back to the beginning and learn how to play in a structured way that introduces chords and so on in a logical progression.
Seriously, even as a hobby this is a complicated subject and it really benefits from learning the basics thoroughly before you try to rush ahead to "more interesting" projects.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Why do you need stream to load picture into viewer?
Just imagine this:
You(stream) are necessary to play(load) a song(picture) at your guitar(viewer).
|
|
|
|
|
You should just use a "picture viewer". Something like MS Photo. It illustrates concepts such as files and folders.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hi,
I want to save all of my DataGridView content directly into a SQL server. I'm Using SQL Light Compact 4.0
All codes related to save function are in a button:
<pre> private void btnSave_Click(object sender, EventArgs e)
{
SqlCeConnection connection = new SqlCeConnection();
connection.ConnectionString = @"Data Source=C:\Users\Dell\Desktop\DataGridView\PMinfo.sdf";
SqlCeCommand myCommand = new SqlCeCommand();
myCommand.Connection = connection;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
myCommand.CommandText = @"INSERT into [PMinfo] (Number, CostCenter, pType, ServiceType, Receiver, WorkCenter, Start, End, Remaining, Status, ReceiveDate) values(@Number, @CostCenter, @pType, @ServiceType, @Receiver, @WorkCenter, @Start, @End, @Remaining, @Status, @ReceiveDate)";
myCommand.Parameters.AddWithValue("@Number", dataGridView1.Rows[i].Cells[0].Value.ToString());
myCommand.Parameters.AddWithValue("@CostCenter", dataGridView1.Rows[i].Cells[1].Value.ToString());
myCommand.Parameters.AddWithValue("@pType", dataGridView1.Rows[i].Cells[2].Value.ToString());
myCommand.Parameters.AddWithValue("@ServiceType", dataGridView1.Rows[i].Cells[3].Value.ToString());
myCommand.Parameters.AddWithValue("@Receiver", dataGridView1.Rows[i].Cells[4].Value.ToString());
myCommand.Parameters.AddWithValue("@WorkCenter", dataGridView1.Rows[i].Cells[5].Value.ToString());
myCommand.Parameters.AddWithValue("@Start", dataGridView1.Rows[i].Cells[6].Value.ToString());
myCommand.Parameters.AddWithValue("@End", dataGridView1.Rows[i].Cells[7].Value.ToString());
myCommand.Parameters.AddWithValue("@Remaining", dataGridView1.Rows[i].Cells[8].Value.ToString());
myCommand.Parameters.AddWithValue("@Status", dataGridView1.Rows[i].Cells[9].Value.ToString());
myCommand.Parameters.AddWithValue("@ReceiveDate", dataGridView1.Rows[i].Cells[10].Value.ToString());
connection.Open();
myCommand.ExecuteNonQuery();
connection.Close();
}
}
When I press the button, compiler gives error in line
myCommand.ExecuteNonQuery();
'There was an error parsing the query. [ Token line number = 1,Token line offset = 92,Token in error = End ]'
What is wrong with my code?
|
|
|
|
|
Alex Dunlop wrote: 'There was an error parsing the query. [ Token line number = 1,Token line offset = 92,Token in error = End ]'
It seems that the parser doesn't like the field name "End".
Try
myCommand.CommandText = @"INSERT into [PMinfo] (Number, CostCenter, pType, ServiceType, Receiver, WorkCenter, Start, [End], Remaining, Status, ReceiveDate) values(@Number, @CostCenter, @pType, @ServiceType, @Receiver, @WorkCenter, @Start, @End, @Remaining, @Status, @ReceiveDate)";
|
|
|
|
|
Thank you. solved.
When I keep the last column in DataGridView null, compiler give the following error in line
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I have check marked null properties for this column in SQL server.
|
|
|
|
|