|
|
it was english, do you have a persian document?
|
|
|
|
|
This is an English site: all the documents will be in English.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Im doing some online course, I'm trying to learn C # . It appears to me that i created infinite loop. Any hepl?
class Program
{
public static int Sum(int start, int end)
{
if (start >= end)
{
return 0;
}
else
{
return Sum(start, end);
}
}
static void Main(string[] args)
{
int start, end;
Console.WriteLine("ukucajte prvi broj");
start =Convert.ToInt32(Console.ReadLine());
Console.WriteLine("ukucajte drugi broj");
end = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("suma je: " + Sum(start,end));
Console.Read();
}
}
}
|
|
|
|
|
Well, yes...
public static int Sum(int start, int end)
{
if (start >= end)
{
return 0;
}
else
{
return Sum(start, end);
}
}
If you call Sum with start less than end:
Sum(1, 2); Then it will look at the two values, and see which is the greater.
It's end , so the if test will fail, and it will execute the else clause ... which passes the same values to the Sum method so it does teh same thing again, and again, until it runs out of stack space and fails!
Probably, what you meant to do was
return start + Sum(start + 1, end); But to be honest, a recursive method to do this is a bit overkill!
Why not try it with a simple for loop?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I'm getting unstructured data in a string field. I told the Db guy to put it in a JSON string and I use NewtonSoft to parse it out. I know what I am getting (maybe) and it is different for each Business Unit and even for parts of Business Units. For example, originally I got strings from the Db guy like:
"MCCFL: unique_id=1709", "MCCFL: DetailID=80" and "TCCM: unique_id=1166"
Not too helpful, so for the next business unit, I said I wanted JSON, so I got something like:
{"BU":"MRXE","unique_id":"4208","job_id":"1395","track_number":"9012712162","job_count:1"}
That would make the previous strings look like:
{"BU":"MCCFL","unique_id":"1709"}, {"BU":"MCCFL","DetailID":"80"} and {"BU":"TCCM","unique_id":"1166"}
Notice that all of these represented as a data structure or object, would be quite different, so I parsed it with NewtonSoft thus:
dynamic deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject(strData);
if (deserialized.BU == null)
strError = "BU";
else
{
this.BusinessUnit = deserialized.BU;
strBusinessUnit = this.BusinessUnit;
}
if (deserialized.job_id == null)
strError = "job_id";
else
this.str_job_id = deserialized.job_id;
...
...
Now the point of this is that I had been considering do I want to use NewtonSoft. It's supposed to be light weight, but I could go retro and get my string like this:
BU:MRXE|unique_id:4208|job_id:1395|track_number:9012712162|job_count:1
Then I could use this:
public static string Return_StringValueForString(string strInBarSeperator, string strValueToFind, out string strValueFound, out int iValueFound)
{
strValueFound = String.Empty;
string strError = "Error-";
iValueFound = 0;
if ((strValueToFind == null) || (strInBarSeperator == null))
{
strValueFound = strError;
return strError;
}
string[] strarr = strInBarSeperator.Split('|');
if (strarr.Length < 1)
{
strValueFound = strError;
return strError;
}
int iPositionInString = 0;
for (iPositionInString = 0; iPositionInString < strarr.Length; iPositionInString++)
{
if (strarr[iPositionInString].Contains(strValueToFind) == true)
break;
}
if (iPositionInString == strarr.Length)
{
strValueFound = strError;
return strError;
}
string[] strarr2 = strarr[iPositionInString].Split(':');
if (strarr2.Length < 2)
{
strValueFound = strError;
return strError;
}
strValueFound = strarr2[1];
int ii = 0;
if (Int32.TryParse(strValueFound, out ii) == true)
iValueFound = ii;
else
iValueFound = 0;
return strValueFound;
}
Now there is another version more specialized for getting an integer, but that is a changed line of code:
public static int Return_IntValueForString(string strInBarSeperator, string strValueToFind, out string strValueFound, out int iValueFound)
The point of posting this is what do you think? I think I'm going to remove the NewtonSoft assembly. I have no need for it and this code should be both simpler and more efficient. Partly it is a question about the efficiency of NewtonSoft and partly abous the "dynamic" variable. The method is a bit quick and dirty and cannot be used to return a value of -1, but nothing in the application should be able to generate a negative number anyway... and that would be an easy update if desired. This should be more versatile actually, a bit less coding. Can you see any problems with the plan? I'm going to need this in a lot of places. T'anks. M
|
|
|
|
|
Can you absolutely guarantee that the names and values will never contain either the : or | characters?
If not, then your "retro" approach will need to come up with some way to escape those characters when they appear in a name or value. And suddenly your parsing code becomes significantly more complicated.
With JSON, you can avoid the dynamic overhead by deserializing to a concrete type:
public class YourClass
{
public string BU { get; set; }
public string DetailID { get; set; }
public string unique_id { get; set; }
public string job_id { get; set; }
public string track_number { get; set; }
public int job_count { get; set; }
[JsonExtensionData]
public IDictionary<string, JToken> AdditionalData { get; set; }
}
...
YourClass data = JsonConvert.DeserializeObject<YourClass>(strData);
or to a dictionary:
var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(strData);
The only real way to know which option is more efficient is to profile your code.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
There is no way to make an object up front for a concrete type, since I don't know what is going to be required from Business Unit to Business Unit. I suppose I could add and add as it goes along, but...
I may have to play with that dictionary idea though. I wonder if this would work?
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(strData);
|
|
|
|
|
Michael Breeden wrote: I wonder if this would work?
Yes, that works.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Fazinating... It does work. Didn't know that.
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(strData);
A. Worked - made dictionary[string,string]
{"BU":"MRXE","unique_id":"4208","job_id":"1395","track_number":"9012712162","job_count":"1"}
B. Worked - made dictionary[string,object]
{"Parts":{"unique_id":"4208","job_id":"1395","track_number":"9012712162","job_count":"1"}}
C. Failed... even with brackets... I bet it could work..
{"Parts":{"unique_id":"4208","job_id":"1395","track_number":"9012712162","job_count":"1"}}, {"Parts":{"unique_id":"4208","job_id":"1395","track_number":"9012712162","job_count":"1"}}
|
|
|
|
|
Example C is some pretty nasty JSON. Even if you put square brackets around it, you would need to deserialize as a List<Dictionary<string, Dictionary<string, string>>> .
var data = JsonConvert.DeserializeObject<List<Dictionary<string, Dictionary<string, string>>>>(strData);
That would give you a list of two dictionaries, each containing a single key called Parts , the value of which would be a dictionary containing the unique_id , job_id , track_number and job_count .
If the source data is that variable, you'll probably be better off sticking with LINQ to JSON[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Well I should never hit that situation anyway and your suggestion did give a solution to the problem without using a dynamic or needing a concrete object. I like Dictionaries.
Thanks for the suggestion, Mike
|
|
|
|
|
Hi every one
I have project made in Ms access contains over 40 forms
is there any way to convert Forms form access to c#.net (just Forms, I don't need any thing else)
can any one help me!!!
thanks to all
|
|
|
|
|
|
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...
A very quick search gave over a million hits: convert access forms to c# - Google Search[^]
In future, please try to do at least basic research yourself, and not waste your time or ours.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I know that google is best choice
I spend time to search on google but I didn't find useful answer
so I came to this website
if you think that my question waste your time,simply you could ignore it without answer it
finally thank you for your reply
|
|
|
|
|
I know that google is my best choice
I did search on google but i didn't didn't find useful answer
if you think that my equation waste your time, you could simply ignore it and don't answer it
finally thank you for your answer
|
|
|
|
|
The advice you have been given to post this on a QA forum reflects the fact that traditionally this forum has been more for C# language issues.
Looks to me like you will either have to re-create the Forms/Controls yourself: [^]; perhaps there's a commercial application, but I bet it would be expensive.
Try not to take responses to you here personally; we're here because we wish to assist you and others, and most of us actually enjoy doing that
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
thank very much for your reply
as you said I found commercial tools to do that but again as you said it's expansive
so I was looking if there is another way
thank you again
|
|
|
|
|
The migration cannot be easily automated. An older (outdated) tool that provides some of that functionality can be found here[^].
40 forms is not very much; I'd recommend a manual migration.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
thank you very much for your reply
|
|
|
|
|
You're welcome
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
In MSDN Application.Idle Event (System.Windows.Forms)[^] one can read concerning using Application.Idle in the paragraph Remarks:
Quote: Caution
Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.
When an Application is disposed, does it not also mean the app exits? And therefore all objects will be finalized? So I don't see why/where a Memory leak can result...
I don't get the point on the above MSDN description. Can you help me on this?
Thank you in advance.
modified 19-Jan-21 21:04pm.
|
|
|
|
|
A thread is not an application: if you start a console app, then you don't create an Application object, you only do that for Windows apps.
And processing can continue once the Application object is closed - have a look at programs.cs and you will see:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
It is perfectly possible to add code after the Application.Run hass completed, so unless you manually detach your event handler from the static Idle event the Application object and everything it contains can't be disposed.
If you attach to it, you should detach as well just in case of later changes.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thank you very much for your reply
Yes in case one adds code after Application.Run then it makes sense.
Finally this has completely convinced me to do it:
Quote: If you attach to it, you should detach as well just in case of later changes.
modified 19-Jan-21 21:04pm.
|
|
|
|
|