|
Quote: since we are not really good at programming
I like your honesty.
Thanks
-Amit Gajjar
|
|
|
|
|
Implement a game.
Most games are well defined, which will make it easier than an open-ended project like an inventory system. They're also more fun.
Games also range from simple card games to Monopoly or chess. Pick one at an appropriate level.
|
|
|
|
|
Hi all,
Please tell me if this code bellow object oriented or not. If it is not what changes that are needed to become an Object oriented?
Thank you.
Here is the code:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace GrillMaster
{
///
/// This class is main class.
///
public class GrillMaster
{
int GrillCapacity;
string MenuNbr;
int k;
int[] BestFit;
List<int> LstData; // Dynamic list to store the item value
Hashtable hTable; // Hashtable to use the store item name, itemValue pairs
///
/// Constructor method
///
/// <param name="Capacity" />
/// <param name="MenuNbr" />
public GrillMaster(int Capacity, string MenuNbr)
{
this.GrillCapacity = Capacity;
this.MenuNbr = MenuNbr;
this.LstData = new List<int>(); // Dynamic list to store the item value
this.BestFit = new int[100];
this.hTable = new Hashtable(); // Hashtable to use the store item name, itemValue pairs
}
///
///
///
/// <param name="NbrItm" />
/// <param name="ItmVal" />
/// <param name="ItmName" />
public void FillList(int NbrItm, int ItmVal, string ItmName)
{
for (int i = 1; i <= NbrItm; i++)
{
LstData.Add(ItmVal); // fill in the list data with the item value
}
if (!(hTable.ContainsKey(ItmName)))
{
hTable.Add(ItmName,ItmVal); // if HashTable not contains the current item then add it.
}
}
///
///
///
public void ClearList()
{
this.LstData.Clear();
}
public void NumberOfRoundsBestFit()
{
int m = 0;
bool bOK = true;
k = 0;
LstData.Sort(); //Sort the data
LstData.Reverse(); // Sort the data in descending order
BestFit[k] = GrillCapacity;
foreach (var elm in LstData)
{
bOK = true;
for (int m1 = 0; m1 <= m; m1++)
{
if (elm <= BestFit[m1])
{
BestFit[m1] -= elm;
bOK = false; //if find correct empty Grill set variable to false
break;
}
}
if (bOK)
{
k++; //increase the rounds
BestFit[k] = GrillCapacity; // set the initial capacity of the grill
BestFit[k] -= elm; //Decrease the capacity of the grill
}
m = k;
}
}
///
///
///
public void NumberOfRounds()
{
int Capacity = GrillCapacity; // save the capacity of Grill
k = 0;
LstData.Sort(); //Sort the data
LstData.Reverse(); // Sort the data in descending order
foreach (var elm in LstData)
{
if (elm <= Capacity && elm <= GrillCapacity)
Capacity -= elm; //Decrease the capacity of the grill
else
{
k++; //increase the rounds
Capacity = GrillCapacity; // set the initial capacity of the grill
Capacity -= elm; //Decrease the capacity of the grill
}
}
foreach (DictionaryEntry entry in hTable)
{
Console.WriteLine("{0}, {1} ", entry.Key, entry.Value); //display on the console key and value of the HashTable
}
}
///
///
///
public void PrintData()
{
Console.WriteLine("Menu "+ this.MenuNbr + ": "+ (k + 1) + " rounds.");
}
}
///
///
///
class Program
{
static void Main(string[] args)
{
int GrilDim = 600; //Der Grill misst 20cm x 30cm
string MenuName = "";
int[] Items = new int[] { 100, 200, 300, 400, 200, 100 };
string[] ItemsName = new string[] { "Fleisch1", "Fleisch2", "Fleisch3", "Fleisch4", "Fleisch2", "Fleisch1" };
/* Uri serviceUri = new Uri("http://grillassessment.cloudapp.net/GrillMenuService.svc");
var serviceCreds = new NetworkCredential("svelkov@gmail.com", "Ycsu4"); var cache = new CredentialCache(); cache.Add(serviceUri, "Basic", serviceCreds);
var service = new GrillMenu.GrillMenuContext(serviceUri)
{ Credentials = cache };
foreach (var grillMenu in service.GrillMenus.Expand(g => g.GrillMenuItemQuantity))
{
Console.WriteLine("Menu: {0}", grillMenu.Name);
MenuName = grillMenu.Name;
GrillMaster TestGrill = new GrillMaster(GrilDim, MenuName);
foreach (var grillMenuItemQuantity in grillMenu.GrillMenuItemQuantity)
{ service.LoadProperty(grillMenuItemQuantity, "GrillMenuItem");
TestGrill.FillList(1, grillMenuItemQuantity.Quantity, grillMenuItemQuantity.GrillMenuItem.Name);
Console.WriteLine("{0} x {1}", grillMenuItemQuantity.Quantity, grillMenuItemQuantity.GrillMenuItem.Name);
}
*/
GrillMaster TestGrill = new GrillMaster(GrilDim, MenuName);
for(int i =0; i < Items.Length; i++)
{
TestGrill.FillList(1, Items[i],ItemsName[i]);
}
TestGrill.NumberOfRounds();
TestGrill.PrintData();
TestGrill.ClearList(); // Clear the list , and prepare for the next menu!!!
Console.WriteLine();
//}
Console.ReadLine();
}
}
}
modified 29-Jul-13 7:32am.
|
|
|
|
|
No one really agrees just what "object oriented" means, other than that objects are involved.
Anyway, please don't declare things like i or k as fields. The general guideline "variables should be declared in the tightest possible scope" is, of course, just a guideline, however it exists for good reasons. Code such as this, with i 's and k 's all over the place (and even a definition of i that hides the outer one) is harder to reason about. I had to read all of the code to find out that you really weren't sneakily using them somewhere before resetting them. And there was really no reason not to follow the guideline here, as far as I can tell.
|
|
|
|
|
+5 just for reading through that.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
stmk69 wrote: Please tell me if this code bellow object oriented or not. If it is not what changes that are needed to become an Object oriented? That would give you an example, but it's not said that the example would help in determining whether the next class you encounter is OO or not. It's more efficient to research the difference between non-OO and OO-code.
Non OO-code in C# (procedural code translated to C#)
static class X
{
public static void Method1(string arg1, int arg2, bool arg3, bool arg4)
{
}
public static void Method2(int arg2, bool arg4)
{
}
public static bool AllBoolArgsTrue(bool arg3, bool arg4)
{
return ((true == arg3) && (true == arg4));
}
} Take note that it's a list of procedures that you can call, along with some parameters, without instantiating the object. The proper OO-version of this code would look similar to the code below;
class X
{
string arg1;
int arg2;
bool arg3;
bool arg4;
public X(string arg1, int arg2, bool arg3, bool arg4)
{
}
public void Method1()
{
}
public void Method2()
{
}
public bool AllBoolArgsTrue()
{
return ((true == this.arg3) && (true == this.arg4));
}
} The example might suck, but it shows clearly why we prefer OO-code; it's more readable, and hence, easier to maintain. Instead of passing a variable to each method of the class, we pass it to the object. Stuff that belongs together is grouped together - and each object becomes an easy testable black-box. No more spaghetti-code!
This would be the essence; good OO-code has more features, like inheritance - but one does not "always" require a base-class, it's not a mandatory construction. On the contrary, if it doesn't add anything (beyond complexity) then it should be omitted.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I am loading an assembly as follows:
Assembly assembly = assembly.LoadFile("file.dll");
I am then looping through the types within in and when a particular type is found I want to retrieve a property from it as follows:
Type[] types = null;
types = assembly.GetTypes();
foreach (Type type in types)
{
if(type.Name.ToString().Equals("TYPE"))
{
PropertyInfo proprty = type.GetProperty("PROPERTY", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
}
}
The property is a HashTable but no matter what method I try to obtain the property value i.e GetValue etc I am unable to retrieve it.
I am probably doing something stupid. Can someone give me guidance as to the syntax required to obtain the property and cast it into an actual HashTable value that I can operate on?
|
|
|
|
|
First of all, you can't use the type until you instantiate it. Merely retrieving an assembly doesn't have any real effect. Secondly, why not use GetType instead of looping through the types? The following code should demonstrate some of what you need to do.
Assembly assembly = Assembly.LoadFile(@"pathtofile\TestFile.dll");
var type = assembly.GetType("TestFile.MyType");
if (type == null) return;
object instance = Activator.CreateInstance(type);
PropertyInfo propertyInfo = type.GetProperty("MyHashTable",
BindingFlags.GetProperty | BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance);
if (propertyInfo != null)
{
object myHashTable = propertyInfo.GetValue(instance, null);
Hashtable ht = (Hashtable) myHashTable;
ht.Add("Hello", "Hello");
} Obviously, you'll want to put a lot more checking in place, but this should give you the basic idea.
|
|
|
|
|
Thanks for the info Pete.
That unfortunately is the trouble in Paradise because I am trying to use the properties of this assembly without referencing anything explicitly and the assembly in question has an entry point that is quite complex as it uses other referenced assemblies that connect to a database. plus there are lots of associated namespaces and associated functionality. a Framework - if you like...
Anyway - although it is a structured environment I want to do this dynamically. As I said without referencing. this is because this is a standalone application and I want to scan, not load the whole external app and its associated framework... Also if I do explicitly reference the required assemblies it will become a pain to manage further versions of my application as the external application it is supposed to reflect is a strongly typed application that is not backwards compatible i.e version 2 is not compatible with version 1 etc so each release will need to be recompiled...
I know it is possibly but it will just take me too much time to make it so at this point!
I am going to investigate the hashtable fields for key names... hopefully that will do it!! 
|
|
|
|
|
As Pete said it is impossible to read instance fields / properties without instantiating an object of that type. If you are trying to read static fields / properties though then you can do it like the following code sample I created:
using System;
using System.Collections.Generic;
namespace TestClassLibrary
{
public class TYPE
{
private static Dictionary<String, String> _PROPERTY = new Dictionary<String, String> { { "first", "First String" },
{ "second", "Second String" } };
public static Dictionary<String,String> PROPERTY { get { return _PROPERTY; } set { _PROPERTY = value; } }
}
}
This first code block is for the class library that will be loaded. The next code block will read the static property PROPERTY and will out put its values to the console:
using System;
using System.Collections.Generic;
using System.Reflection;
namespace TestReflection
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.LoadFile(@"D:\Development\Console\Playgroung\TestReflection\TestClassLibrary\bin\Debug\TestClassLibrary.dll");
Type type = assembly.GetType("TestClassLibrary.TYPE");
PropertyInfo propertyInfo = type.GetProperty("PROPERTY", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
Dictionary<String, String> dictionary = (Dictionary<String, String>)propertyInfo.GetValue(null, null);
foreach (KeyValuePair<String, String> kv in dictionary)
{
Console.WriteLine("{0} => {1}", kv.Key, kv.Value);
}
}
}
}
Is this what you had in mind? Notice the BindingFlags.Static in the GetProperty call.
Regards,
— Manfred
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Hi,
I used the below connection string to read excel file.
connectionString = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=D:\test2.xlsx;Extended Properties=\'Excel 12.0;HDR=No;\";
While opening, an exception is thrown.. "External table is not in the expected format"
I explored in serveral sites and changed the connection string as mentioned there and followed other solutions. But none of them didn't work.
Please guide me to solve this issue. Thanks in advance.
|
|
|
|
|
|
I used the below connection string as mentioned in the given article. Still error exists
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test.xlsx;Extended Properties=\"Excel 12.0;HDR=NO\"";
|
|
|
|
|
You need to escape the backslash in your file name thus:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\test.xlsx;Extended Properties=\"Excel 12.0;HDR=NO\"";
Use the best guess
|
|
|
|
|
I'm reading that data source value in runtime. so blackslash is not an issue.
|
|
|
|
|
I can only assume that there is something else causing the problem. Are you able to read the file with Excel?
Use the best guess
|
|
|
|
|
I followed below steps to make it work. Now, I'm able to read file
1. open your excel file
2. try to read that
3. Able to read the file
4. to read the file next time, no need to open the file.
I don't know the reason how and why its working 
|
|
|
|
|
I've an ItemsControl that is virtualized using following options (.NET 4.5):
VirtualizingStackPanel.IsVirtualizing="True" ScrollViewer.CanContentScroll="True" VirtualizingPanel.ScrollUnit="Pixel"
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
Scrolling using Pixels works but I noticed that the offset that i set is beeing modified afterwards, and that's a big issue for me hence i sync the offset with other controls. Also BringIntoView() does also behave wrong (randomly not work).
This is how it looks like when I log the offset I set and the offset reported in OnScrollChanged.
Set = 614,745
ScrollChanged = 614,745
Set = 702,565714285714
ScrollChanged = 702,565714285714
ScrollChanged = 658,838326118333
ScrollChanged = 681,27584415585
Btw, this don't happend if i use an ordinary StackPanel. Any idea why the ScrollViewer behaves this way?
|
|
|
|
|
Hi All,
I want to use the below powershell script in c#. The script is with two foreach loop and the 2nd foreach loop uses add-member to add the value that is part of $mailbox variable. I managed to write c# code to work upto first foreach. I need help to complete the c# code with 2nd foreach loop and using the value of $mailbox.
Powershell Script :
ForEach ($Mailbox in Get-Mailbox) {Get-ActiveSyncDeviceStatistics -Mailbox $Mailbox.Identity –ErrorAction SilentlyContinue | Select DeviceFriendlyName, Devicetype, DeviceUserAgent | ForEach-Object { $_ | Add-Member –MemberType NoteProperty -Name "MailboxIdentity" -value $Mailbox}}
I managed to go upto,
c# code:
PowerShellpowershell = PowerShell.Create();
PSCommandcommand = newPSCommand();
command.AddCommand("Get-Mailbox");
command.AddCommand("where-object");
command.AddParameter("Filterscript", scriptBlock.Create("!$_.name.startswith(\"DiscoverySearchMailbox\")"));
powershell.Commands = command;
powershell.Runspace = CreateRunSpace.GetRunSpace();
varresult = powershell.Invoke();
PSCommandcommand1 = newPSCommand();
command1.AddCommand("write-output");
command1.AddParameter("InputObject", result);
command1.AddCommand("Foreach-Object");
command1.AddParameter("Process", ScriptBlock.Create("Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity"));
powershell.Commands = command1;
powershell.Runspace = CreateRunSpace.GetRunSpace();
var result1 = powershell.Invoke();
I'm stuck at 2nd foreach loop to add-member while adding the output of get-mailbox. Appreciate your help and suggestions. Thank you.
|
|
|
|
|
You already asked these two questions in Quick Answers, please use one forum only.
Use the best guess
|
|
|
|
|
Hi All,
I want to use the below powershell command in c# and get the desired output.
Powershell script:
foreach($a in get-mailbox){get-activesyncdevicestatistics -mailbox $a.identity | Select DeviceFriendlyName, Devicetype, DeviceUserAgent,@{Name="User Name";expression={$a.displayname}}}
------------------------------------------------------------------------
I have made the code as below, but need your help to work on Select part with hash table. Please let me know, how I can include the "Select DeviceFriendlyName, Devicetype, DeviceUserAgent,@{Name="User Name";expression={$a.displayname}}}" in the below code.
----------------------------------------------------------------------------
C# code:
Collapse | Copy Code
PowerShellpowershell = PowerShell.Create();
PSCommandcommand = newPSCommand();
command.AddCommand("Get-Mailbox");
command.AddCommand("where-object");
command.AddParameter("Filterscript", scriptBlock.Create("!$_.name.startswith(\"DiscoverySearchMailbox\")"));
powershell.Commands = command;
powershell.Runspace = CreateRunSpace.GetRunSpace();
varresult = powershell.Invoke();
PSCommandcommand1 = newPSCommand();
command1.AddCommand("write-output");
command1.AddParameter("InputObject", result);
command1.AddCommand("Foreach-Object");
command1.AddParameter("Process", ScriptBlock.Create("Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity"));
powershell.Commands = command1;
powershell.Runspace = CreateRunSpace.GetRunSpace();
var result1 = powershell.Invoke();
|
|
|
|
|
I have rdlc string in which some words are bold,have text color,italic,etc and this string i get from database and i need to display it in rdlc report as is formated.
|
|
|
|
|
Assuming the field is formatted with HTML, you should just need to set the "Markup type" property on the field to "HTML":
http://msdn.microsoft.com/en-us/library/dd207057.aspx[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you for your help bu unfortunately I have designed report in VS.NET 2008 and currently i need solution for version VS.NET 2008 before i plan to migrate my report in newer version.
|
|
|
|
|
If your report is running against SQL Server 2005, you can't use inline formatting.
If it's running against SQL Server 2008 or higher, the "Markup type" property should work. Depending on your version of BIDS, you might need to open the report in Report Builder to access the property.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|