|
Hello,,i wrote a code for finding the nth term in the series an print the result. So, I used a while loop in that,where I used 2 variables named tem1,tem2. When I print after the while loop,it is snot printing the updated value.Can anyone suggest a solution.
public class ArmstrongExample
{
public static void Main(string[] args)
{
int ip1, ip2, ip3, ip4, res, tem2 = 0;
ip1 = Int32.Parse(Console.ReadLine());
ip2 = Int32.Parse(Console.ReadLine());
ip3 = Int32.Parse(Console.ReadLine());
ip4 = Int32.Parse(Console.ReadLine());
res = summeet(ip1, ip2, ip3, ip4);
Console.WriteLine(res);
int summeet(int p1, int p2, int p3, int p4)
{
int a, b, c, n, tem1, i = 0, d1, d2;
a = p1;
b = p2;
c = p3;
n = p4;
while(i>n-3)
{
d1 = b - a;
tem1 = d1 + c;
d2 = c - b;
tem2 = tem1 + d2;
i++;
tem1 = tem2;
}
return tem2;
}
}
}
|
|
|
|
|
That won't compile. Why not? You don't declare tem2, and if you do, you need to give it a default value or you can't return it.
In addition there is no good reason for a local function here: it just makes the code harder to read.
If it doesn't compile, it doesn;t generate an executable file, so the version you run is the last "compile error free" version of your source code.
Fix the compilation errors, and it may work better:
public static void Main(string[] args)
{
int ip1, ip2, ip3, ip4, res, tem2 = 0;
ip1 = Int32.Parse(Console.ReadLine());
ip2 = Int32.Parse(Console.ReadLine());
ip3 = Int32.Parse(Console.ReadLine());
ip4 = Int32.Parse(Console.ReadLine());
res = summeet(ip1, ip2, ip3, ip4);
Console.WriteLine(res);
}
private static int summeet(int p1, int p2, int p3, int p4)
{
int a, b, c, n, tem1, tem2 = 0, i = 0, d1, d2;
a = p1;
b = p2;
c = p3;
n = p4;
while (i > n - 3)
{
d1 = b - a;
tem1 = d1 + c;
d2 = c - b;
tem2 = tem1 + d2;
i++;
tem1 = tem2;
}
return tem2;
}
But do yourself a favour: stop using as-short-as-possible names for things: it may seem like a waste of time and more work, but using descriptive names makes your code document itself, and that means it's more readable. It also means it's a lot easier to tell when you used the wrong variable, and that makes your code more reliable and easier to debug. the variables a, b, c, and n are unnecessary, you can use the parameters directly instead; and give them sensible names instead of "p1", "p2" and so on. That way, when you want to use the method later, Visual Studio will even prompt you as to what value belongs in what parameter!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You really need to learn how to use a debugger. If you had used one (as well as given us the code where tem2 was declared), you would see that your while condition would never finish. So, why won't it finish? Well, you have declared a variable called n which gets a default value of 0; you haven't actually allocated anything to it. Now, when your program runs, you will see that i (which starts at 0) will always be greater than n-3, so it never terminates. Honestly, you could have found this out in a minute with the debugger.
This space for rent
|
|
|
|
|
Hi,
I fail to see what your loop is trying to achieve; the variable i will be incremented on each iteration, however the five other variables will never change once the first iteration has finished...
Furthermore, when the loop tests for larger than, I expect the control variable to be lowered inside the loop, and not incremented. Otherwise you may end up waiting for an “integer overflow”.
modified 10-May-18 5:09am.
|
|
|
|
|
private void stsearch_Click(object sender, EventArgs e)
{
string stringToSearch = (@"D:\\st.txt" + stname.Text);
string[] lines = File.ReadAllLines(@"D:\\st.txt");
var lineCount = File.ReadLines(@"D:\\st.txt").Count();
foreach (string line in lines)
{
if (line.Contains(stringToSearch))
{
if (str1 != null)
{
lines = str1.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
}
for (int i = 0; i < lineCount-1; i++)
{
subarray = lines[i].Split(',');
stage.Text=subarray[1].ToString();
stgender.Text= subarray[2].ToString();
}
}
}
|
|
|
|
|
I don't think you can, if I read what you are trying right.
You seem to be looping round for each line in the file, splitting it on commas, and trying to display each line in a textbox: but that won't work, because each time you assign a new value to the Text property, it throws away what was in the textbox before:
stage.Text=subarray[1].ToString();
stgender.Text= subarray[2].ToString(); YOu could use a multiline textbox, and append each result to a new line, but that's a bit of a cack-handed approach. Have you considered using a DataGridView instead?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Mohamed Fahad M wrote: string[] lines = File.ReadAllLines(@"D:\\st.txt");
var lineCount = File.ReadLines(@"D:\\st.txt").Count();
That would read the file twice, once just to get the count.
var lineCount = lines.Count(); Learn to walk before trying to run.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Nah. I still prefer old school stuff such as
int lineCount=lines.Length;
|
|
|
|
|
You're right, of course
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
repost of: [^]
Please do not repost: as you make progress, edit your original post.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
So I was trying to convert Binary file to data.
BinaryReader Read_Binary = new BinaryReader(File.Open(@BinaryFile, FileMode.Open));
Uint32_Data = (Read_Binary.ReadUInt32());
But what I want is data in float.
eg. Uint32_Data = 1079320248
in hex = 0x40551EB8
But what I want is floating point. i.e 3.33.
The data bits are proper order. But how can I extract value in floating point.
Online Hex Converter - Bytes, Ints, Floats, Significance, Endians - SCADACore[^]
|
|
|
|
|
Hi,
if you know in advance a float is up, just use BinaryReader.ReadSingle() .
If not, you need to perform a conversion; there are a couple of ways doing this, e.g. using the BitConverter class.
Alternatively you can use a pseudo-union, as in:
[StructLayout(LayoutKind.Explicit)]
public struct Uifl {
[FieldOffset(0)]
public float fl;
[FieldOffset(0)]
public uint ui;
};
|
|
|
|
|
hi all
I am in process of preparation of an app which gives the details of a webservice - Name, ValuePair
Its some thing like this
If we input the url for the webservice. It must give the Name, Value Pair (which is extracted from the Response)
Hope i am clear and help me in this
|
|
|
|
|
Download the info from the WDSL as a string?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Member 2236419 wrote: Hope i am clear and help me in this
How?
We have no idea what you have tried, where you are stuck, or what help you want from us - you haven't asked a question at all!
All you have done is give a very, very brief outline of what you want to end up with - we don't even know where you are starting from, if teh web service exists, or what it provides if it does.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Basically assume that i have 3 webservices
1. service1
2. service2
3. service3
Now my requirement is i need to generalize the consumption of the webservice
So if i add a service reference of the webservice in the current project, and place a request to the webservice, it must give me the properties(Name and ValuePair)
of the response from the webservice.
The main intention of doing this is... in future i am expecting more webservices which will be added into my current application. So i thought of using reflection
to acheive the same but its not getting worked out.
Hope i am clear now
Kindly suggest and help me
|
|
|
|
|
Member 2236419 wrote: So i thought of using reflection to acheive the same
What OG is asking for is what have you tried, where you are getting any errors.
You are only giving him the requirement which sets the context of the problem.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Class1.cs
<pre>namespace ClassLibrary1
{
class Class1 : IGameMenuItemPlugin
{
public bool SupportsMultipleGames => false;
public string Caption => "Save";
public System.Drawing.Image IconImage
{
get
{
return null;
}
}
public bool ShowInLaunchBox => true;
public bool ShowInBigBox => true;
public bool GetIsValidForGame(IGame selectedGame)
{
return true;
}
public bool GetIsValidForGames(IGame[] selectedGames)
{
return false;
}
public void OnSelected(IGame selectedGame)
{
Apath item = new Apath
Num1 = selectedGame.ApplicationPath
var form = new Form1();
form.Show();
}
public class Apath
{
private string num1;
public string Num1
{
get { return num1; }
set { num1 = value; }
}
}
public void OnSelected(IGame[] selectedGames)
{
return;
}
}
}
form1
namespace ClassLibrary1
{
public partial class Form1 : Form
{
Class1.Apath apath1 = new Class1.Apath();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(apath1.Num1);
}
}
}
I am trying to get mesagebox to display
selectedGame.ApplicationPath but I get a blank messagebox.
Appreciate any help please.
|
|
|
|
|
Hi,
I see several issues:
1.
the code shown does not compile; there are several syntax errors in method OnSelected() .
Please show actual code, and if you consider it necessary to shorten the code, keep it correct.
2.
I fail to see the logic in your code structure: both Class1 and Form1 create a new Apath. Are you sure that is what you want? They will be distinct objects.
3.
I fail to see how your OnSelected() method will ever execute. There seems to be nothing that calls it. Therefore num1 remains at its initial value (which is null), and that is what gets shown by your MessageBox, which is kind enough to treat null as an empty string.
Suggestions:
1.
Use descriptive names, not just Form1 and Button1 .
2.
Provide some context information when asking a question here; it helps us understand what it is all about.
3.
If what you want is the path of your .exe file, you should have a look at the Assembly class and try something like
string exePath=Assembly.GetCurrentAssembly().Location;
|
|
|
|
|
In OnSelected you have some syntax errors, but it looks like you intend to create a new Apath, set its Num1, and then use that in Form1. Currently however, Form1 creates a new Apath instead of using the one from Class1.
You could pass the item in the constructor like so.
Class1.cs
public void OnSelected(IGame selectedGame)
{
var item = new Apath
{
Num1 = selectedGame.ApplicationPath
};
var form = new Form1(item);
form.Show();
}
Form1.cs
namespace ClassLibrary1
{
public partial class Form1 : Form
{
Class1.Apath apath1;
public Form1(Class1.Apath item)
{
InitializeComponent();
apath1 = item;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(apath1.Num1);
}
}
}
modified 13-Sep-18 21:01pm.
|
|
|
|
|
guys already i split the value by(,) comma . i have n number of lines with comma(,) in notepad file.
Material Name,Material Type,Length,width,thickness,Area,Initial Gauge Length,Speed,graph type,Breath
Rubber,flat,50mm,20mm,10mm,65mm,22mm,10mm,LOAD VS DISPLACEMENT,30MM
silicon,round,5mm,3mm,2mm,6MM,2MM,1MM,Stress VS Strain,3mm
now i want the separate the data from text file.i have 10 textbox. for ex : if i enter "Rubber" in a textbox then click button.another textbox filled with before comma words in that specified line only (i.e 2nd line)
now how can i get the splitted values in a textbox from seperate line in c# using text file
|
|
|
|
|
Too many questions and no code.
Show some code and ask "one or two" questions at this point; since you are practically asking someone to "answer" with a complete program.
And, only show code "relevant" to the question(s).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
The simplest way is to use a CSV processor - A Fast CSV Reader[^] is a good one - and let it read the whole file into say a DataTable.
You can then use Linq or a foreach loop to find the matching text in the first column, and display the rest in your textbox.
It's pretty simple, but this smells of homework, so I'll give you no code!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
In my Application, I have a WCF service. I am trying to send a Json Object and get the Json object as Response.
But every time, I am getting error "The data contract type 'TestConsole.localhostrREF4.PrimeDataProperty' cannot be deserialized because the required data members 'isPrimeResultField, isPrimeResultFieldSpecified, isPrimeResultSpecified1Field, isPrimeResultSpecified1FieldSpecified, numberField, numberFieldSpecified, numberSpecifiedInField, numberSpecifiedInFieldSpecified' were not found.
My Web Service name is "ISAJsonWebService"
Here is my code of Web Service:
IJsonWebService.cs
[ServiceContract] public interface IJsonWebService {
[OperationContract]
[WebInvoke(UriTemplate = "/GetData",
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
PrimeDataPropertyOut GetData(PrimeDataProperty value);
}
[DataContract]
public class PrimeDataProperty
{
[DataMember]
public int number { get; set; }
[DataMember]
public bool NumberSpecifiedIn { get; set; }
[DataMember]
public bool IsPrimeResult { get; set; }
[DataMember]
public bool IsPrimeResultSpecified { get; set; }
}
[DataContract]
public class PrimeDataPropertyOut
{
[DataMember]
public bool IsPrimeResultOut { get; set; }
[DataMember]
public bool IsPrimeResultSpecifiedOut { get; set; }
}
EndPoint Code behind JsonWebService.svc.cs
public class JsonWebService : IJsonWebService
{
public PrimeDataPropertyOut GetData(PrimeDataProperty value)
{
return new PrimeDataPropertyOut
{
IsPrimeResultOut = true,
IsPrimeResultSpecifiedOut = true
};
}
}
Here is my Web Config:
<configuration>
<appsettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true">
<system.web>
<compilation debug="true" targetframework="4.7.1">
<httpruntime targetframework="4.7.1">
<system.servicemodel>
<diagnostics>
<messagelogging logmalformedmessages="true"
="" logmessagesattransportlevel="true">
<services>
<service behaviorconfiguration="serviceBehavior"
="" name="ISAJsonWebService.JsonWebService">
<endpoint address="" behaviorconfiguration="jsonEndpointBehavior"
="" binding="webHttpBinding" name="json" contract="ISAJsonWebService.IJsonWebService">
<identity>
<dns value="localhost">
<endpoint address="mex" binding="mexHttpBinding" name="mex"
="" contract="IMetadataExchange">
<endpoint address="wsHttpBinding" behaviorconfiguration="wsHttpBinding"
="" binding="wsHttpBinding" bindingconfiguration="" name="wsHttpBinding" contract="ISAJsonWebService.IJsonWebService">
<identity>
<dns value="localhost/wsHttpBinding">
<behaviors>
<endpointbehaviors>
<behavior name="jsonEndpointBehavior">
<webhttp>
<behavior name="wsHttpBinding">
<servicebehaviors>
<behavior name="serviceBehavior">
<servicemetadata httpgetenabled="true">
<servicedebug includeexceptiondetailinfaults="true">
<system.webserver>
<modules runallmanagedmodulesforallrequests="true">
<directorybrowse enabled="true">
I am trying to Consume the web service from a Console App.
Here is the code:
static void Main(string[] args)
{
localhostrREF4.JsonWebService jsonWebServiceRef = new
localhostrREF4.JsonWebService();
PrimeDataProperty pdp = new PrimeDataProperty { number = 1,
NumberSpecifiedIn = false, IsPrimeResult = false, IsPrimeResultSpecified
= false };
WebClient client = new WebClient();
client.Headers["Content-type"] = "application/json";
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(typeof(PrimeDataProperty));
serializer.WriteObject(ms, pdp);
byte[] data =
client.UploadData("http://localhost:65256/JsonWebService.svc/GetData",
"POST", ms.ToArray());
Stream stream = new MemoryStream(data);
var obj = new DataContractJsonSerializer(typeof(PrimeDataProperty));
**var returnPrimeDataProperty = obj.ReadObject(stream) as
PrimeDataProperty;**
}
in the last line (Bold Text) is throwing exception.
Please let me know, what I am missing.
Thanks.
|
|
|
|
|
Start by looking closely at the source app, and find out exactly what class(es) it is including in the JSON data. Then look very closely at those classes, and check that the JSON contains exactly what you expected.
Then look at the destination app, and the matching classes there: if they do not contain exactly the same classes, with exactly the same properties, this is the error you will get.
What the error is saying is: "the source and destination classes are not the same" - the destination has missing or added properties.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|