|
Hi,
First of all, I'm new on CodeProject, so quick introducing: my names is Matthieu, I'm 26yo and live in France.
I start learning C# and I get stuck at the (easy?) following:
I have 2 string arrays, and I want to copy the first at the end of the second. I used Array.CopyTo method but it fails because the destination array is not large enough.
How can I increase the size of the dest string array ?
Thanks a lot for help,
Matthieu
|
|
|
|
|
Use Concat method of string.
s1= "Hello ";
s2 = "World"
s1 = s1.Concat(string2)
//s1 = Hello World
sorry for my bad English.
|
|
|
|
|
Thanks Itanium, but that's not what I want to do.
s1[0]="hello"
s1[1]="world"
s2[0]="good"
s2[1]="luck"
and I want to create a new string array like this:
s1[0]="hello"
s1[1]="world"
s1[2]="good"
s1[3]="luck"
Thanks a lot for help
Matthieu
|
|
|
|
|
you cannot re-dimension an array in C# like you could in VB with redim. You'll need to do something like:
string[] arrayOne = new string[]{"Hello", "World"};
string[] arrayTwo = new string[]{"Have", "a", "nice", "day"};
int newArrayLength = arrayOne.Length + arrayTwo.Length;
string[] arrayThree = arrayOne;
arrayOne = new String[newArrayLength];
int iterator = 0;
foreach(string s in arrayThree)
{
arrayOne[iterator] = s;
iterator++;
}
foreach(string s in arrayTwo)
{
arrayOne[iterator] = s;
iterator++;
}
I'm sure theres a more diginfied way, but it does work!!!
|
|
|
|
|
You've got two options here:
1. Create a new array of length= array1.length + array2.length, then use two consecutive Array.CopyTo() calls, one using zero as the index to start at, one using array1.length.
2. If you don't mind the minor performance hit from using a ICollection based object, take a look at System.Collections.ArrayList:
using System.Collections;
ArrayList array1 = new ArrayList();
array1.Add("hello");
array1.Add("world");
ArrayList array2 = new ArrayList();
array2.Add("Bonjour");
array2.Add("le monde");
array1.AddRange( array2 );
now array1 should contain (boxed as objects):
"hello",
"world",
"Bonjour",
"le monde"
Hope that helps...
Jeremy Kimball
|
|
|
|
|
I don't think that boxing takes place on strings, does it? Forgive me if I'm being dumb, as I have way more experience in Java than in .NET, but I believe that all strings are objects, aren't they? Class String extends/inherits from Object, not ValueType.
Also, the relative performance hit of using an ArrayList is not so minor in this case. I think that people should always program with performance in mind. Still, it's always nice to show people how to do things in different ways.
Regards,
Jeff Varszegi
|
|
|
|
|
Can any body guide me how to input password using VS.NET setup project, what i have seen is that there are text boxes which can take input but if i want to hide the text enterd in a password field i have no option. Presently what i am doing is that i have made a separate windows installer component which displays a modal dialog to input password, but it clearly seems something plugged into the setup and doesn't look like a part of setup when setup is run.
waiting for ur reply.
|
|
|
|
|
Can any body guide me how to input password using VS.NET setup project, what i have seen is that there are text boxes which can take input but if i want to hide the text enterd in a password field i have no option. Presently what i am doing is that i have made a separate windows installer component which displays a modal dialog to input password, but it clearly seems something plugged into the setup and doesn't look like a part of setup when setup is run.
waiting for ur reply.
|
|
|
|
|
The VS.NET setup project can't really be modified from what it supports. It's very basic. You should look into something like Wise Solutions for a real MSI development environment.
You could, however, add a new form (VS.NET has a fix number of fixed-layout forms) with a single text box then use a binary CustomAction to verify the password (almost like you are now) before proceding.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I have a Form.
Within the form's class, I have a method TestConnection which tries to establish a connection to a database, and returns a bool.
I invoke this method in the Form_Load method.
With regard to what TestConnection returned, I present the user with a messagebox asking whether to try again or not.
Since TestConnection takes a long time, I want to thread it, and once it completes, i want to check the returned bool and proceed.
What is the best way of threading it?
|
|
|
|
|
I'd do something like this:
public class MyThingy {
private bool testConnectionComplete = false;
private bool testConnectionResult = false;
....
public override void Form_Load(...) {
Thread tcStart = new Thread(new ThreadStart(this.TestConnection));
tcStart.Start();
Thread tcMonitor = new Thread(new ThreadStart(this.CheckTestConnection));
tcMonitor.Start();
}
public void TestConnection() {
this.testConnectionResult = WhateverYourResultIs;
this.testConnectionComplete = true;
}
public void CheckTestConnection() {
while( this.testConnectionComplete == false ) {
}
}
}
Of course, there may be better ways...
Jeremy Kimball
|
|
|
|
|
Do anyone know how to get back from the client the values of a data grid when columns have been added dinamically?
I noticed that if the AutoGenerateColumns flag is set to true and a dataset is binded to the DataGrid, postbacks include all the cells, even when the user changed values. This allows to add editing capabilities by traping the Update and Edit events.
However, if the columns are added dinamically (let's say template columns need to be used), no columns appear on the postback. See code below.
Notice that this part of the code
// BoundColumn pDBColumn = new BoundColumn();
// pDBColumn.DataField = "ARTIST";
// pDBColumn.HeaderText = "ARTIST2";
// DataGrid1.Columns.Add (pDBColumn);
adds a column dinamically. The column gets added to the response, but is missing on the client's request.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace TemplateColumns
{
///
/// Summary description for WebForm1.
///
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label LblDebug;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
DataGridBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGridBind()
{
// BoundColumn pDBColumn = new BoundColumn();
// pDBColumn.DataField = "ARTIST";
// pDBColumn.HeaderText = "ARTIST2";
// DataGrid1.Columns.Add (pDBColumn);
// Simple bind to a TextBox control
if (Session["TheDataset"] == null)
{
DataSet pDS0 = new DataSet("EmployeesDataSet");
pDS0.ReadXml(@"c:\cd_catalog.xml");
Session["TheDataset"] = pDS0;
}
DataSet pDS = Session["TheDataset"] as DataSet;
DataGrid1.DataSource = pDS;
DataGrid1.DataBind();
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.SelectedIndex = e.Item.DataSetIndex;
DataGrid1.EditItemIndex = DataGrid1.SelectedIndex;
this.LblDebug.Text = "Cells on DataGrid1_EditCommand: " + e.Item.Cells.Count.ToString();
DataGridBind();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.LblDebug.Text = "Cells on DataGrid1_UpdateCommand: " + e.Item.Cells.Count.ToString();
DataGridBind();
}
}
}
|
|
|
|
|
Can't quite get this right. I have:
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct MIDIHDR <br />
{<br />
[MarshalAs(UnmanagedType.LPStr)]<br />
public String Data;<br />
public uint Length;<br />
public uint RecBytes;<br />
public uint User;<br />
public uint Flags;<br />
public uint Next;<br />
public uint reserve;<br />
public uint Offset;<br />
public uint Reserved;<br />
}<br />
I am having problems getting the right MIDIHDR.data value, I think. I am building the data as follows:
I am building the string with a StringBuilder object.
I StringBuilder.Append() anywhere from 4 to 16 bytes.
I set MIDIHDR.data = StringBuilder.ToString() and
MIDIHDR.Length = StringBuilder.Length
When I test it by sending the MIDIHDR to a midiOutLongMsg call, I have gotten either no sound, or the same, wrong note, no matter what string I send.
example bytes {153, 41, 127, 0}
StringBuilder.ToString() outputs "153411270"
with a length of 9
What i believe this is doing is converting the byte with a value of 153 to a string of length 3 ("153"). Is there a way to build this string as just the 4 bytes? Can I, for example, cast the byte to char before appending to the string? Am I gonna have problems with the unicode/ansi conversion in the string and it's length as its size in the struct??? Or is the marshalling of the struct really taking care of all this and I just have an error elsewhere?
( I may not understand the proper way to use midiOutLongMsg() to send multiple ShortMsg's simultaneously...)
I was making headway with marshalling/interop and have been successful with everything but this. I have used the same byte data to successfully make calls to midiOutShortMsg. The bytes are actually stored in a struct of 4 bytes coerced into a union as an int. So it would be neat if I could just build the string using this int value to represent the 4 bytes, but for now I just wanna get it to work.
i am going to keep playing and researching, but I thought I'd stick the question here to see if I get a nibble.
Any help or insight would be greatly appreciated!
Thanks!
Tym!
Make love, not chocolate chip cookies with nuts.
At least make the cookies without nuts for crying out loud.
|
|
|
|
|
It's too damned early for me to really play with this too much, but my gut is telling me you should be using a byte array (byte[]) rather than a String/StringBuilder. Reason being that the LPSTR in the MIDIHDR is really a char* (read:byte array). Have you taken a stab at using byte arrays yet? You may not even need the MarshalAs attribute in that case...not sure.
Jeremy Kimball
|
|
|
|
|
Thanks for the advice. I was leaning that way, I got as far as using a byte array, but still using the marshalling attribute, which gave me a runtime error.
I think you will prove to be right, it makes sense.
Thanks again, I'll let ya know if it works when I get a chance to try it...
Tym!
|
|
|
|
|
Still can't get it to work...
I have tried combinations of :
byte[]
char[]
String
StringBuilder
no MarshalAs
MarshalAs LPStr, ByValTStr
but I get the following error each time except when building the buffer with a StringBuilder and declaring data as a string, don't need any marshalling:
MIDIHDR can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
I'm thinking I just need build the string differently, so I'm gonna see what I can find on StringBuilder and hope it gets me somewhere.
Thanks!
|
|
|
|
|
Greetings!
How do I rename a file using C#? Does Anybody know how? Please help.
And also, how do I delete files or move them from folder to folder? Please help.
"To teach is to learn twice"
|
|
|
|
|
|
Say I have a string that's "C:\Folder\New\Filename.jpg"
Is there a built in method that will just let me rip the string to "Filename.jpg" or do I need to go through it manually?
|
|
|
|
|
Take a look at the System.IO.Path class and the GetFileName method.
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
Awesome, Was not aware of that class. Thanks for the tip!
|
|
|
|
|
Finding stuff in the framework classes is often the most challenging task working with C# .
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
Reading is harder than typing? I think not. Back in the 1.0 beta days I spent time and at least skimmed through the BCL to see what's available. No one can memorize everything, but at least it gives you a good idea of where to look. Again, this is just laziness bolstered by IntelliSense!
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I guess I'm remembering what it was like being a Java and C++ developer starting to learn C#. The language was easy to pick up, but figuring out where things are and the overall organization of the .Net Framework in general can take a little while to pick up.
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
Heath Stewart wrote:
this is just laziness bolstered by IntelliSense
Thats just what I was thinking )
IntelliSense is a great idea but it does almost remove any need to remember class and method names!
James Simpson
Web Developer
imebgo@hotmail.com
P S - This is what part of the alphabet would look like if Q and R were eliminated Mitch Hedberg
|
|
|
|