|
Good points. I,j,k are quite confusing.
|
|
|
|
|
Hi, thank you all for responding. K is initialized outside of the function and I have modified my function as follows:
class Class1
{
private int k = 0;
private List<string> list1 = new List<string>();
//Passing in myTemp as a string
public string AddText(string myText)
{
int i = 0;
do
{
//check to see if element is empty
if (list1.Count == 0)
{
//Add the content of myText into list1
list1.Add(myText);
i++;
}
} while (i < list1.Count);
//Returns the content of the elements that were affected
return list1[i];
}
}
I have stepped through the function and Visual Studio says the line, return list1[i];is the cause of the error message "Index was out of range". I don't know what else to do. If you can explain why I'm getting the error by looking at my code, please point it out. Thanks for your help.
-- modified 7-Jul-12 9:16am.
|
|
|
|
|
That's a good clue! Now, add list1.Count and i to the Watch list and step through again, observing how each line of code affects them. I think you'll find that, when you hit the return statement, i = list1.Count . That will put it out of range, since the last element of list1 has an index of Count - 1. Just a guess...
Will Rogers never met me.
|
|
|
|
|
The reason this code would throw an out of range exception is if the code executes within this section
if (list1.Count == 0)
{
list1.Add(myText);
i++;
}
now you have added an item to the list and the count is now 1. You then increment i from zero to 1.
Now you are trying to return the first item in the collection (a zero based index collection) with the value of 1 when the value you should be requesting is 0
return list1[i-1] or list1[0] or list1[list1.Count - 1] etc. of course if your i value is 0 and you try list1[i-1] you will also receive an error because that would be the value -1 and that is out of range.
Hope that helps if you haven't already figured this out.
|
|
|
|
|
i'm working on an application that marks a computer that ever accessed it.
i mark it using computer name, but since computer name can be changed, i'd love to know whether static IP can be changed or not, thanks before.
|
|
|
|
|
|
PIEBALDconsult wrote: Of course they can.
guess i've to find something else that's really static.
btw thank you.
|
|
|
|
|
If you can get the MAC address of the network card of the computer connecting to your server, that should work for what you are describing.
There are ways to change the MAC address too, but it not something you will normally see.
Soren Madsen
|
|
|
|
|
Excellent answer, Soren. Yes, the MAC address can be spoofed, but 99.999% of users won't have any idea how to do so, nor any reason to. IIRC, on a local network the ARP command will return the MAC address. On the Internet, I'm not sure that there is a way to return this information, but it's worth looking into.
Will Rogers never met me.
|
|
|
|
|
Thanks Roger,
I agree, the OP (and just about all of us) is highly unlikely to run into anyone spoofing the MAC address, but I thought I would mention that part too.
Soren Madsen
|
|
|
|
|
You haven't said what kind of application you're working on, nor if you are writing the server-side code or client side.
On the server-side, it doesn't matter if you're writing a ASP.NET or Windows Forms app, you cannot get the client MAC address. You'll end up getting the MAC address of the near-side of the router closest to your server, not the client MAC.
If you're writing client-side code in a Windows Forms app, the client get easily get it's own MAC address and send that information in a request to the server. You'll have to implement the interface on the server and client for this to work.
|
|
|
|
|
I know the OP has not given a whole lot of detail, but since he posted his question in this forum, I figured it would be valid to mention the MAC address. He is looking for something that is both unique to a computer and does not change.
I totally agree he might not be able to acquire the MAC address and that totally depends on what he is developing.
Soren Madsen
|
|
|
|
|
i've tried it and i got something like 50E549A6XXXX.
thank you very much.
|
|
|
|
|
In a C# 2010 application, I created a new linq to sql (*.dbml) file. When this occurred, the .net framework created a default connection string. However I want the *.dml file to use the connection string I specify.
to solve this problem, I have the following questions:
1. When I do create a new .dbml file, how to I make the .net framework use the connection string I want to use and not come up with a default connection string?
2. For an existing .dbml file that I added to the application, how can I change the default connection string to the value I really want to be used?
|
|
|
|
|
Whenever you create instance of DataContext pass the connectionstring in the constructor. This way you will be able to bypass the default connectionstring.
http://www.exploresilverlight.com
Cheers!
Vinod
|
|
|
|
|
can you show me a code example of what you are referring to?
|
|
|
|
|
TestDataContext dataContext = new TestDataContext("connectionstring");
http://www.exploresilverlight.com
Cheers!
Vinod
|
|
|
|
|
For a C# 2010 application, I dragged a new table onto the *.dbml surface. When this occurred, the .net framework changed the exisiting connection string to a default one that it came up with instead of using the existing connection string.
Thus to resolve this question, I have the follwoing questions:
1. When I drag a new table onto the .dbml designer surface, how can I keep the .net framework from changing the existinng connection string and use the existing connection string?
2. For an existing .dbml file that I already dragged the table onto the .dbml designer surface, can can I fix the exisiting code so I do not need to recreate the code from scratch again?
|
|
|
|
|
Some time back I watched a demo from Anders Hejlsberg where he showed how to get the filename and linenumber at compile time by declaring a function using syntax similar to the following:
void log(string message, string filename = _file_, int lineno = _line_)
I don't recall if that was something still being worked on or a proposal or if it is in an actual shipping version of C#.
1. Is this in some version of C#,
2. and if so what version of C# is it in?
3. And what is the actual syntax?
Yes, I know I can get this info in builds where a .pdb is provided through the StackFrame class. That's not what I'm asking.
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun
|
|
|
|
|
Haha, after a little more searching, I found it here[^]
The syntax is:
public void TraceMessage(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun
|
|
|
|
|
Tip! Tip!
|
|
|
|
|
Good idea.
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun
|
|
|
|
|
Interesting.
It is rather new, just available since .NET 4.5
I use Environment.StackTrace (available since 1.0), however that is a very expensive way as it returns the whole stack trace, where one only needs the info about one or two nesting levels.
|
|
|
|
|
Hi This is Chandra,
presently working on GoogleMaps. Right now i can able to bind the langitude and latitude dynamically based on selection of country. That is multiple locations i can able to display in the map.Here my problem is i want to display information window when i click any one of the location. Please find my code below in C~ as well as Asp.net
C# Code:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindInfo();
}
}
protected void bindInfo()
{
try
{
SqlConnection con = new SqlConnection("Data Source=xxx;Initial Catalog=Test;Persist Security Info=True;User ID=xxx;Password=xxx$;Connect Timeout=38000;pooling='true'; Max Pool Size=200");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetCountryNames";
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlPostalCode.DataTextField = "Country";
ddlPostalCode.DataValueField = "Country";
ddlPostalCode.DataSource = ds.Tables[0];
ddlPostalCode.DataBind();
con.Close();
}
catch (Exception x)
{
Response.Write(x.ToString());
}
}
private void GetLocation(string CName)
{
try
{
SqlConnection con = new SqlConnection("Data Source=xxxx;Initial Catalog=Test;Persist Security Info=True;User ID=xxxx;Password=xxxx$;Connect Timeout=38000;pooling='true'; Max Pool Size=200");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetLocationWiseInfo";
SqlParameter param = new SqlParameter();
param.ParameterName = "@Name";
param.Value = ddlPostalCode.SelectedItem.Text.ToString();
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
BuildScript(ds);
}
catch (Exception x)
{
Response.Write(x.ToString());
}
}
public void BuildScript(DataSet tbl)
{
string Locations = "";
foreach (DataRow dr in tbl.Tables[0].Rows)
{
if (dr["Latitude"].ToString().Trim().Length == 0)
continue;
string Latitude = dr["Latitude"].ToString();
string Longitude = dr["Longitude"].ToString();
string Sales = dr["Sales"].ToString();
Locations += Environment.NewLine + " map.addOverlay(new GMarker(new GLatLng(" + Latitude + "," + Longitude + ")));";
}
js.Text = @"<script type='text/javascript'>
function initialize() {
if (GBrowserIsCompatible())
{
var map = new GMap2(document.getElementById('map_canvas'));
map.setCenter(new GLatLng(45.05,7.6667), 2);
" + Locations + @"
map.setUIToDefault();
}
}
";
}
protected void btnPlaceName_Click(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
GetLocation(ddlPostalCode.SelectedItem.Text.ToString());
}
}
ASPX Code:
<asp:label id="lblName" runat="server" text="Please Select PostalCode">
| <asp:dropdownlist id="ddlPostalCode" runat="server" width="150px"
="">
| <asp:button id="btnPlaceName" runat="server" text="GetGeoData" onclick="btnPlaceName_Click">
|
<asp:panel id="Panel1" runat="server">
<asp:literal id="js" runat="server">
<asp:literal id="js1" runat="server">
In the dataset i am getting rows like Longitude, Latitude and sales data based on selection of Country.
Can any one suggest me how to do that?
Thanks
Chandra
|
|
|
|
|