|
Xarzu wrote: How do you programatically open a file in c# to append?
As explained in the tutorials you can Google.
Xarzu wrote: I tried searching online for an example and so far I have not found anything.
"C# append text example". Nothing?
Xarzu wrote: System.IO.StreamWriter and there is System.IO.StreamReader
You don't need a stream; File.AppendAllText[^] does as requested. Usage is simple;
File.AppendAllText(@"C:\Temp\Test.txt", "Text to append");
|
|
|
|
|
Hi, New here, and would be very thankful for any help someone can give me.
The code I am working for is a population estimator. It has 3 text boxes, the first is two enter the starting population, the second is to enter the daily increase in that population in a percentage, and the third is the number of days to multiply that by.
My first issue is with the second text box. I don't know how to enter the data as a percent. As of now it is only entering as a whole number, so I when I right in text box one the number 2, in box2 the number 30, and in three the number 10, I get the 302. Not 21.209 like it should be.
Secondly, it all displays in a listbox, and its supposed to loop for every day you enter. I've got the loop to loop by days, but it just runs the same calculation, I want it make every entry build off the last.
Example:
It should say
2
2.6
3.38
4.394
5.7122
etc
I've entered in the code below.
Thanks for any help!
private void calculateButton_Click(object sender, EventArgs e)
{
int organisms;
int days;
int growth;
int population;
int count = 1;
if (int.TryParse(OrganismsTextbox.Text, out organisms))
{
if (int.TryParse(DailyTextbox.Text, out growth ))
{
if (int.TryParse(DaysTextbox.Text, out days))
do
{
population = organisms + growth * days;
resultsListbox.Items.Add("The population For day " + count + " is " + population.ToString("n1"));
count = count + 1;
}
while (count <= days);
|
|
|
|
|
You probably need to use double values rather than integers for your calculations, for greater accuracy. You also need to convert the number taken from the second textbox to a percentage; it's not 30 it is .30. You also need to change your calculation to calculate the growth in compound terms - I'm not sure of the actual formula.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
There are a couple of simple things to do - firstly as Richard said, you need to use floating point values instead of integers:
2 + 30% is 2.6, but as an integer it is 2 again!
The second is that you are looping through the days, so you don't need to include the number of days in each calculation - just use the result from the previous day.
The third is that when you want an increased value, you can't just multiply by the percentage - 2 * 30% is 0.6, so your increase is 0.6, not your population. To increase your population, multiply but 1.0 + the percentage.
Finally, there is a better way to loop through the days: use a for loop instead of a while:
for (int day = 1; day <= days; day++)
{
... calculate here
}
So, my version would be:
resultsListbox.Items.Add(string.Format("The population for day {0} is {1}", day, organisms));
organisms *= 1.0 + percent;
with percent adjusted to 0.3 as Richard suggested.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Thanks guys! Got it to work!
|
|
|
|
|
Glad to hear it!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
I'm having trouble with the interface for the Factory Design Pattern. I've looked at
http://dotnet.dzone.com/articles/design-patterns-c-factory,
which follows my design very closely since it was the first good example I found a while back.
I took a look at
http://stackoverflow.com/questions/27294/abstract-factory-design-pattern,
but my design is very different.
Right now, this is what it looks like. I have included a lot of code because I have a feeling the problem has to do with the visual studio project breakdown. It used to be more simple, with my CR5, interface, factory, CB_spec, El, etc in the same visual studio project. I had to move things around as shown per design discussions and the need for CR6, etc to be separate. Now I'm getting some compilation problems I'm not sure what to do with. See ** below for the first and worst. My question is regarding the compilation issue below.
My iCR Visual Studio project which is the interface:
public interface iCR
{
int CB_IO_Init(int slaveIndex);
int WritePortReady();
int WritePortBusy();
void initCRData(byte[] writeBuffer, byte[] statusBuffer, int SlaveIndex, USB_Comm.CB cb, int cr_Type);
int ProcessTWriting(ref Byte[] writeDat, ref Byte[] statusDat, ref Byte[] dataDumpWriteCheck);
void Failure(String message);
void Success(String message);
}
My CR_Factory Visual Studio project
namespace CR_Factory
{
public class Cr
{
}
public class CRFactory
{
public enum CRType
{
CR0,
CR1,
CR3,
CR4,
CR5,
CR6
}
public CRFactory()
{
}
public iCR GetCR(CRType type)
{
iCR cr = null;
switch (type)
{
case CRType.CR5:
cr = new CR5();
break;
case CRType.CR6:
break;
default:
throw new ArgumentException(string.Format("A CR of type {0} cannot be found", Enum.GetName(typeof(CRType), type)));
}
return cr;
}
public CRType DetermineCR_Type(int type)
{
switch (type)
{
case 0:
return CRType.CR0;
case 1:
return CRType.CR1;
case 3:
return CRType.CR3;
case 4:
return CRType.CR4;
case 5:
return CRType.CR5;
case 6:
return CRType.CR6;
default:
throw new ArgumentException(string.Format("A type of type {0} cannot be found", type));
}
}
}
}
My CR5 Visual Studio Project has a lot of classes in it, but right now I’m just showing you the part referred to in the factory. Later I’ll create a CR6 VS project, etc. As you can see, the compiler doesn't think I implemented initCRData but it's right there, and curly brackets are fine.
public class CR5 : iCR **compile error CR5_new.CR5 does not implement interface member iCR.initCRData(byte[],byte[],int,USB_Comm.CB,int)
{
CB_703 cb_specific = null;
public CR5()
{
cb_specific = new CB_703(SlaveIndex);
}
public void initCRData(byte[] writeBuffer, byte[] statusBuffer, int slaveIndex, USB_Comm.CB cb_specificInstance, int crType)
{...
}
public int CB_IO_Init(int SlaveIndex)
{
int result = -534;
result = cb_specific.IO_Init(SlaveIndex);
return result;
}
.
.
.
}
I have another Visual Studio Project (actually several) that instantiates the factory and gets the appropriate type. We’ll call it El:
namespace CrWr
{
public partial class PControl : UserControl
{
public PControl()
{
}
public Control GetPControl(USB_Comm.CB cbInstance, string dllSelected, THandlerApplication.Temp.TEMP[] temp, string dll, SC.SC.S_C c0)
{
cb = cbInstance;
createControls();
itsDll = dll;
tArr = temp;
cert = c0;
CR_Factory.CRFactory factory = new CR_Factory.CRFactory();
CRFactory.CRType type = factory.DetermineCR_Type(cr_Type);
try
{
cr = factory.GetCR(type); }
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
}
return this;
}
private void OnP()
{
int result = -536;
while (rL)
{
result = cr.CB_IO_Init(SlaveIndex);
if (result == 0)
{
…
}
}
.
.
.
}
|
|
|
|
|
Try this;
public class CR5 : iCR
{
CB_703 cb_specific = null;
public CR5()
{
cb_specific = new CB_703(SlaveIndex);
}
public void iCR.initCRData(byte[] writeBuffer, byte[] statusBuffer, int slaveIndex, USB_Comm.CB cb_specificInstance, int crType)
{...
}
public int CB_IO_Init(int SlaveIndex)
{
int result = -534;
result = cb_specific.IO_Init(SlaveIndex);
return result;
}
|
|
|
|
|
When I do that, the compilation error turns to:
The modifier 'public' is not valid for this item.
|
|
|
|
|
Makes sense. Being an interface, marking it public would be redundant. Delete the keyword.
|
|
|
|
|
Thanks for the comment. I got rid of "public" so now it's
void iCR.initCRData(byte[] wrBuff, byte[] stBuffer, int slaveIndex, USB_Comm.CB cb_spec, int crType)
{}
However, I'm getting compilation errors indicating it's not being seen:
CR5_new.CR5 does not implement interface member iCR.initCRData(byte[], byte[], int, USB_Comm.CB, int)
iCR.initCRData in explicit interface declaration is not a member of interface
Any thoughts? On the one had it doesn't seem to see that it's there, but then it sees it and thinks it's not a part of the interface. This is what the interface VS project looks like,
public interface iCR
{
. . .
void initCRData(byte[] wrBuffer, byte[] stBuffer, int SlaveIndex, USB_Comm.ClearBox cb, int cr_Type);
. . .
}
My iCR dll is referenced in my CR5_new VS project. It could be something really basic, since I've only programmed with VS/C# for a little over 6 months...I have more C++ experience. Thanks for all of your help. I really appreciate it.
|
|
|
|
|
MichCl wrote: My iCR dll is referenced in my CR5_new VS project.
It's easier to "test" it in a separate console-app; create the desired structure there, isolated from the rest of your app.
Does it work if the interface and consuming class are in the same project? If yes, there's a problem with the reference (check namespaces, using-clauses etc) If no, there's a problem with the syntax.
This case, I'm guessing syntax. Compare your code to the example from MSDN[^].
|
|
|
|
|
It does work if the iCR, Factory class, Cr5, and El are in the same VS project, with the USB_Comm.CR class in a separate VS project.
It's weird...when I refer to the method in my CR5 project (the one we've been discussing), VS fills in the method I'm referring to, but when I try to get it to suggest the parameters I'm supposed to supply, it's not coming up with anything.
|
|
|
|
|
MichCl wrote: It does work if the iCR, Factory class, Cr5, and El are in the same VS project, with the USB_Comm.CR class in a separate VS project.
A syntax-error would have generated a compile-error. The fact that it works implies that the code is correct, and that it's a problem with the references.
Did you reference the project (add the project-source to the solution) or the assembly? Is there an old version of this assembly in the GAC?
Alternatively; what happens if you rename the interface? Does the exception pick up the correct new interface-name?
|
|
|
|
|
I figured it out... See below, after my response to your last post.
There are compilation errors with the VS project distributed as I first presented it. It's
CR5_new.CR5 does not implement interface member iCR.initCRData(...) (but it does)
and
iCR.initCRData in explicit interface declaration is not a member of interface (but it is)
I have a reference to the VS project iCR in my CR5_new project.
I'm not sure what you're referring to with GAC above. When I look in my CR5_new project, even though I see it referenced in the solution explorer under references, when I look in MyComputer, I don't see a copy of the dll, so presumably it's not a copy, it's actually looking for it in the original location. When I look at the Solution Explorer properties of iCR, it's giving the path to the correct original location for the dll.
Alternatively, when I change the name of the interface to iC from iCR, I had to change it in
public class CR5:iC in my CR5_new namespace/visual studio project. Plus I had to change it to iC.initCRData(...). Then the above compile errors changed to use the new interface name.
I figured it out when I was looking into the above... My CR5 project had another class in it for USB_Comm.CB. The iCR was also referring to this class for parameters in the interface. So they were cross-referencing. So I moved the USB_Comm.CB class into it's own VS project so they can both refer to it without cross-references.
On the one hand I feel like I'm not using multiple class VS projects, but if I kept these things in them, I'd have repeats of classes in my VS projects like El. Plus when I create my CR6 project, I can re-use code more. Thanks for all of your help!!
|
|
|
|
|
MichCl wrote: I figured it out... See below, after my response to your last post.
Whehe, cool - and good explanation!
|
|
|
|
|
Hi All,
We are facing a critical issue in our application and I need your help in solving this issue:
Currently our application is communicating with Mainframes to get the Statements and we are using AFP2PDF converter to convert the AFP file format from Mainframes to the PDF format. The flow of the application is like :
.Net application --> stiSocket dll --> AFP2PDF dll
Existing application in deployed in Windows Server 2003 (32 – bit System) and the stiSocket dll is built for 32 bit and AFP2PDF dll is also built for 32 bit. So there is no problem in the existing application.
Now we are deploying the code in Windows Server 2008 (64 – bit system) and the first problem we faced is the communication between the >net application and the stiSocket.dll. This issue occurs because the 64-bit application tries to call the 32 bit stiSocket.dll. we solved this issue by re-compiling the stiSocket application in a 64 bit system and we changed the setting of the project to work the dll in 64 bit system.
(we had the source code of the stiSocket application).
Then the problem comes when the stiSocket.dll communicates with the AFP2PDF dll. This again occurs when the 64 bit stiSocket dll communicates with the 32 bit version of AFP2PDF dll. We could not able to change the settings for this dll as we don’t have the source code for this dll.
Is there any way to call the 32 bit C++ dll in the 64 version of dll?
Kindly give your comments on this issue.
Thanks
|
|
|
|
|
Would the application run if it was compiled to 32 bit?
|
|
|
|
|
we converted the .Net application to work for 64 bit machine. The Problem is with communicating the stiSocket dll to the AFP2pDF dll.
|
|
|
|
|
Well, if you don't have the source for the AFP2PFD.dll and there isn't a 64-bit availble, you have two options.
1) Recompile everything back down to 32-bit because you cannot mix 32- and 64-bit code in the same process.
2) Leave your app as AnyCPU and rework the StiSocket.dll code to load the AFP2PDF.dll in a serperate Process, and write a wrapper around the functionality of the .DLL that will marshal remote calls from your StiSocket.DLL to to the exposed functioanlity of the .DLL.
Frankly, I'd go with number 1.
Unless you've got some 64-bit reason that you absolutely need 64-bit support in your app, your 32-bit-only app will work just fine on a 64-bit machine.
|
|
|
|
|
|
Hello to all,
i finally decided to hide or remove the "Dial.." button from ContextMenuContactItem and add the new button that when i click on my own item button show the telephone numbers that contact righ-clicked and selected. How can i make it? Is there some code but NOT ADDIN EXPRESS (we must pay to use it) or solution to take example how to build it?
Is there someone expert and can help me to build it. I have last one week! And also if i click one of number the event click send the number selected in string format to my own function.
Thanks very much for any help.
Thanks!
I will here every hours!! Thanks!
|
|
|
|
|
When creating big applications(even medium apps) there are so many different kinds of exceptions that can come?
How to go about handling them?
And how do prioritize these exceptions?
(We just cannot write a 100 try-catches,can we?)
|
|
|
|
|
You should never be trying to handle exceptions from the whole application in the same place (apart from a last ditch catch-all to put up a message and log a bug report).
In any individual operation, there should be only one or two possible exceptions that can be thrown. You should catch and handle them appropriately as close as possible to the site of the operation.
|
|
|
|
|
We divide our whole application into layers , tiers, components etc. for modularity of a functionality. Cant we have a whole layer just to handle exceptions. In that case if we know that an exception is not being handled properly we will know where to go instead of having to browse through the whole code looking for the pertinent error.
|
|
|
|
|