|
I tend towards the static method mostly for consistency sake. If I need to return a list of those objects, which I often do, then I want a static method that queries the datasource once and returns a list of the objects.
I don't want that list retrieval method to make a bunch of calls to the objects constructor only to have the object query the datasource.
And of course I want to be able to get a single object in a similar manner as I get my list, thus static methods all the way.
I'm sure an argument can be made for the 'constructor with parameter' way as well. These things are almost never black and white.
|
|
|
|
|
I prefer not to have the object class query the datasource -- I think that couples the class too tightly to the datasource. By using constructors I can instantiate the class from different sources (IDataRecord, XML, etc.) as the situation requires.
In my opinion the class shouldn't know how, where, or whether the data is persisted. I realize that this may be an unpopular point of view. 
|
|
|
|
|
That makes a lot more sense than have a constructor querying the database.
I've never worked with a code base that did it that way, but I'm going to keep that method in mind for the next greenfield project I work on.
|
|
|
|
|
PIEBALDconsult wrote: I realize that this may be an unpopular point of view.
Unpopular with whom? IMO this is the correct way also.
Failure is not an option; it's the default selection.
|
|
|
|
|
Thank you for your response
|
|
|
|
|
Unfortunately the simplest answer to this is 'it depends'. Both approaches can be correct, depending on how complex the object is and what the data source is (i.e. does constructing the object require a database hit?).
I would typically have a layer of model objects which map directly to database rows, and my data access layer would create those from a pseudo-factory (actually, last time I did this, the data access layer instantiated and populated those objects by reflection, and EF does something similar for you). That means that you can do a single query, pull the query results into memory and create a list of data objects without repeatedly accessing the data source.
Then, if those data objects weren't sufficient for what I actually wanted in my application object model, I'd have the application objects take the relevant data objects as constructor parameters.
In the past, though, I have had application objects directly take the data source (a byte array) as a constructor parameter, because in that case the data source was zero cost so the extra indirection wasn't worth it.
|
|
|
|
|
i have an object
Dictionary<int, String[]> temp = new Dictionary<int, String[]>();
The values in temp are:
1 s1
s2
s3
2 s1
s2
I want to transform it into object
Dictionary<String,int[]> temp1 = new Dictionary< String,int[]>();
So i need some kind of grouping
So i want final o/p as
s1 1
2
s2 1
2
s3 1
Please techies reply... thanks in advance
|
|
|
|
|
Does it have to be fast or just correct?
|
|
|
|
|
|
Repost!
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
modified 7-May-12 8:43am.
|
|
|
|
|
Hi All,
I have the following problem:
I'm creating a windows application what read map data and draw the map from these information.
The problem is the following:
When I read the shape file I get about 5000 shapes what contain the points what I have to use for drawing. I read these shapes in a foreach loop. I get X, Y coordinates from part of the shape, create vertexes from these coordinates and use these vertexes to draw roads.
I use for this draw function in a class but it seems I get OutOfMemory exception. I have found that I should use one vertebuffer and send this to the drawing function. But I do not have idea how could I insert collections to the vertexbuffer and to the indexbuffer and read the data back when I want to draw the lines.
Does anyone have idea how should I solve this problem? Did anyone meet a problem like this?
Thanks for your help!
Regards,
Norbi
|
|
|
|
|
That is so vague I can't offer any effective help, except to say from your description I see little opportunity to run out of memory, barring a huge mistake in your code. Unless each of your shapes resemble a detailed map of the world, rather than some elementary rectangle, circle, etc.
An extremely wild guess could be an infinite loop where you allocate some memory and keep doing that, e.g. inside a property getter/setter or a method that calls itself (which would in the end also run out of stack).
Suggestions:
1. look at all the details of the actual exception, in particular the line number it mentions; then watch the related code.
2. when necessary, ask specific questions, documented with sufficient information and relevant code snippets.
|
|
|
|
|
Hello,
I am working on a math program for my daughter in C# using a Form. Here is the gist of my program:
1) Create random numbers.
2) Add numbers together. Compare to user input.
3) Tell user if question was answered correctly.
I can get the program to cycle through once successfully. I can't figure out how to loop through a series of 50 questions though. When I've tried to loop it, I either end up on question 50 or suffer through an infinite loop.
The idea is to use the 'checkAnswer' button to let the user know if the question was answered correctly and then move on to the next question. However, I have yet to figure out a way to signal to the rest of the program (return value from button click?) that the button has been pressed. I have included the code.
If anyone could point me in the right direction, I would greatly appreciate it. Also, this is the first time I've posted in a programming forum so I've left out any pertinent info please let me know!!!
namespace Math
{
public partial class NewMathForm : Form
{
public NewMathForm()
{
InitializeComponent();
int questionCounter = 1;
for (; questionCounter < 50; )
{
addition(ref questionCounter);
}
}
private void sumTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
sumTextBox.SelectionStart = 0;
}
public void shuffle(ref int[] array)
{
Random rng = new Random();
int count = array.Length;
while (count > 1)
{
int k = rng.Next(count);
count--;
int temp = array[count];
array[count] = array[k];
array[k] = temp;
}
}
private void parseNumbers(ref int[] array)
{
double addend1 = 0;
double addend2 = 0;
string number;
char num1;
char num2;
for (int count = 0; count < 50; count++)
{
number = array[count].ToString();
num1 = number[0];
int length = number.Length;
if (length < 2)
{
char zero = '0';
num2 = zero;
}
else
num2 = number[1];
addend1 = System.Char.GetNumericValue(num1);
addend2 = System.Char.GetNumericValue(num2);
array[count] = Convert.ToInt32(addend1);
count++;
array[count] = Convert.ToInt32(addend2);
}
}
private void addition(ref int tickUp)
{
int[] numbers = new int[100];
int counter = 0;
int questionCounter = 1;
int numberOfQuestions = 50;
for (counter = 0; counter < 100; counter++)
{
numbers[counter] = counter;
}
shuffle(ref numbers);
parseNumbers(ref numbers);
if (sumTextBox.Text == "")
{
addendOneLabel.Text = numbers[questionCounter].ToString();
addendTwoLabel.Text = numbers[questionCounter + 1].ToString();
}
tickUp++;
}
private void checkAnswerButton_Click(object sender, EventArgs e)
{
checkAnswer();
}
public void checkAnswer()
{
int sum = Convert.ToInt32(sumTextBox.Text);
int number1 = Convert.ToInt32(addendOneLabel.Text);
int number2 = Convert.ToInt32(addendTwoLabel.Text);
int answer = number1 + number2;
if (answer == sum)
correctAnswer();
else
incorrectAnswer(answer);
sumTextBox.Text = "";
}
private void correctAnswer ()
{
MessageBox.Show ("That is correct!");
}
private void incorrectAnswer (int sum)
{
MessageBox.Show ("That is incorrect! The correct answer is " + sum + ".");
}
}
}
|
|
|
|
|
for (int count = 0; count < 50; count++)
{
array[count] = Convert.ToInt32(addend1);
count++;
array[count] = Convert.ToInt32(addend2);
You are using the count variable for two different purposes and by incrementing at this point you are corrupting your loop. You should use a separate variable for your array index.
Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness
|
|
|
|
|
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
int x[10],sn=0,sp=22,counter;
int bqueens(int k,int i)
{
int j;
for(j=1;j
|
|
|
|
|
This is not a well framed question (If you are trying to ask one!)
We cannot work out what you are trying to do/ask from the post. Please edit your question and provide better information.
|
|
|
|
|
This looks like C/C++ code not C#. Also a quick Google for your title will lead you to lots of useful information.
Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness
|
|
|
|
|
a) Not a question - please refer to the site guidance about asking a question.
b) Not c# - wrong forum
c) Not complete code.
d) Not formatted code
e) This is a well known problem and easily googled.
|
|
|
|
|
|
hi, I want sorce code of Image Encryption (with chaos signals) in C#, please help me.
|
|
|
|
|
Danial C wrote: I want sorce code of Image Encryption
It does not work like this here.
Here is what is expected of enquirers:
1. TRY first what you want to do! You may not find it too difficult.
2. Formulate what was done by you that looks like an issue/not working.
So, try them and tell if you face issues.
|
|
|
|
|
Hi I am new in C# I try an application like this
"
Title="Window1" Height="300" Width="300">
<grid>
<path name="myPath" stroke="Black"
="" strokethickness="1">
"
and
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DynamicPoint002
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);
LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
myPathFigure.Segments = myPathSegmentCollection;
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
}
}
}
but I do not understand why did not show my the line defined already?
Thanks a lot!!!
H.
|
|
|
|
|
|
Hi friends,
Is possible to choose a instalation directory for my c# aplications ? (I´m using VCS2008)
all my apps istalls on a default user directory
thanks in advance
marcelo campos
|
|
|
|
|
Yes, of course. Most installers default to Program Files | Company | Product Name , but you can override this.
/ravi
|
|
|
|