|
Member 12055749 wrote: Bugging me on a forum.
Member 12055749 wrote: It is bad.
Member 12055749 wrote: how could you improve this implementation? Verify if there's a third argument on the commandline before accessing it, replace the "var" declarations with something descriptive, and add exception handling and logging. I would also check the validity of the arguments at the start, and throw an exception if they are invalid, as opposed to using an else statement. And get rid of variable names like "v1".
Member 12055749 wrote: Using a unit-test?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
It's not all bad.
I was looking at it thinking, surely that should be File.ReadAllLines, but in fact no, File.ReadLines is there too. Didn't know that and now I know the subtle difference.
Something new, every day.
Regards,
Rob Philpott.
|
|
|
|
|
Rob Philpott wrote: It's not all bad. That's true; it is indented correctly
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
1.
V = Extract dictionary.txt file content to a string
V1 = Contains string[] 3rd element
V2 = Contains string[] 4th element
First = Extract from V the first string matching with V1
Last = Extract from V the first string matching with V2
l = Get from V the index of First
l2 = Get from V the index of Last
l3 = Length of Last - First
Results = Skip first element and return next l3 elements
Print Results
The code is reading dictionary.txt and print all results between index of 3rd and 4th array element.
2. This code is hard to read. Variable names aren't intuitive. Doesn't seem to be necessary use var because some types aren't anonymous. ReadLines return a string[], V1 and V2 are strings, l, l2 and l3 are integer.
|
|
|
|
|
Of course, the easy way for you to answer the questions would be to actually run the program and step through it. You'll be amazed at what you can learn.
|
|
|
|
|
This smells like homework. What do you think it does? What do you think of the code quality?
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
|
|
|
|
|
especially if you look at the poster's profile
Best,
John
|
|
|
|
|
i'm a newbie in IT and i'm learning C program.i'm wrote this code and it has error message.
-------
#include <stdio.h>
#include <conio.h>
void main()
{
int n,i;
long s;
scanf ("%d" ,&n);
s=0;
i=1;
while (i<=n)
{
s=s+i;
i=i+1;
}
printf("%ld",s);
}
modified 13-Oct-15 11:37am.
|
|
|
|
|
What is the error message you are getting? Also, in future posts, use a more descriptive subject line, and place in the correct forum since you have a C program problem and this is the C# forum.
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
|
|
|
|
|
I just built and ran your code and there are no error messages. There is one warning about using scanf_s instead of scanf , but that can be safely ignored for now.
I suggest you post the full details in the C forum, making sure you include the exact text of the error message.
|
|
|
|
|
Windows Forms
Let'say you have a List<Control> : some of those Controls' Native Types have the 'FlatStyle Property; others may have the 'BorderStyle Property; other Controls may have neither. Of course we know that the Button Control, for example, inherits from ButtonBase which inherits from Control, etc.
At run-time we can use reflection to determine if a Control is of a Type that contains a specific Property, and, if the Property exists, get its DeclaringType:
PropertyInfo pif = currentControl.GetType().GetProperty("BorderStyle");
if (pif != null)
{
Type typ = pif.DeclaringType;
} If we want to set the 'BorderStyle of the Control at this point, obviously we have to cast it, somehow, back to its Native Type.
But, how ?
thanks, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
One way I have seen this done, albeit in a hacky way, is to provide a Cast method that looks something like this:
public T Cast<T>(object o)
{
return (T)o;
} Then, you call it with this:
MethodInfo methodInfo = this.GetType().GetMethod("Cast").MakeGenericMethod(typ); You can then choose to do with it, what you will.
modified 13-Oct-15 8:20am.
|
|
|
|
|
Thanks, Pete, I am drawing a blank here about how you would use the 'MethodInfo created in your code. I'm trying this:
public T XCast<T>(object o)
{
return (T)o;
}
Type typ = currentControl.GetType();
MethodInfo methodInfo = this.GetType().GetMethod("XCast").MakeGenericMethod(typ);
var xc = methodInfo.Invoke(currentControl, null);
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Your invoke should look something like this:
methodInfo.Invoke(null, new object[] { button }); But, as Eddy has rightly pointed out - you can always use GetValue and SetValue.
|
|
|
|
|
Thanks, Pete, Eddy's suggestion does enable me to do what I want, but I am still curious about your code; if I use this:
public T XCast<T>(object o)
{
return (T)o;
}
PropertyInfo pif = currentControl.GetType().GetProperty("BorderStyle");
if (pif != null)
{
Type typ = currentControl.GetType();
MethodInfo methodInfo = this.GetType().GetMethod("XCast").MakeGenericMethod(typ);
var xc = methodInfo.Invoke(this, new object[]{currentControl});
} Then 'xc does appear to have a valid reference to the Control with the BorderStyle Property, but to set that Property: of course, I can't use 'xc.
Is there a "missing piece" of the puzzle here that would let me set the Property without using 'SetValue ?
thanks, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
BillWoodruff wrote: If we want to set the 'BorderStyle of the Control at this point, obviously we have to cast it, somehow, back to its Native Type. No need to cast, just to get or set the property. If you have the PropertyInfo , then you can call its GetValue method.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Excellent, Eddy, as is said in the American South (where I was raised): "if that had been a snake, it would've bit you."
Everytime I make a mistake of omitting to look for the simpler way, I hear the introduction to "Proud Mary" spoken by Tina Turner:
"But there's just one thing
You see we never ever do nothing
Nice, easy"
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Tx, and you're welcome. Just a lucky open spot in the grass where the snake was easy to see
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have created a .mdf database file. I have created a table and populated It with rows.
Im trying to use Entity framework, to comunicate with the database.
Here, Im using a context to connect to the database:
namespace SportsStore.domain.Concrete
{
public class EFDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
}
And here, Im trying to get the rows from the database:
namespace SportsStore.domain.Concrete
{
public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext();
public IEnumerable<Product> Products
{
get { return context.Products; }
}
}
}
When I run my application, nothing Is printed out. The rows from the database are not printed out.
Anyone who can help me with this? Should I add something to Web.Config? If so, what should I add?
|
|
|
|
|
You haven't shown all the relevant code so it's impossible to be sure why it's not working.
But, I will say that your repository code doesn't return an IEnumerable<Product> . Your code is actually returning a DbSet<Product> , which just so happens to implement IEnumberable<TEntity> so the code doesn't throw an error when you compile it.
The set you're returning is empty because the query hasn't actually run. You have to call some method on the Products query, typically .ToList() , to force the query to run and build the list in memory. It's called "deferred execution[^]". If you're using Entity Framework you better understand what this is.
Your repository should looks more like this:
namespace SportsStore.domain.Concrete
{
public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext();
public IEnumerable<Product> Products
{
get { return context.Products.ToList(); }
}
}
}
|
|
|
|
|
Sorry. That did not help me. Someone else?`
|
|
|
|
|
Lie I said, you have not supplied all the relevant code, such as your View and Controller code.
|
|
|
|
|
When I add ToList(), I get the following error:
Additional information: Failed to set database initializer of type 'SportsStore.domain.Concrete, EFProductRepository' for DbContext type 'SportsStore.domain.Concrete, EFDbContext' specified in the application configuration. See inner exception for details.
Does this mean that I have to add a line to my Web.Config file?
|
|
|
|
|
And the inner exception message is .......?
Also, what IS in your Web.Config file?
|
|
|
|
|
The message of my inner exception says: "Can not load file or assembly EFDbContext or one of its dependencies. The system can not find the file EFDbContext"
Here is my Web.Config:
="1.0"="utf-8"
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<entityFramework>
<contexts>
<context type="SportsStore.domain.Concrete, EFDbContext">
<databaseInitializer type="SportsStore.domain.Concrete, EFProductRepository" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
|
|
|
|