I have this array :
Bitmap[] bildeListe = new Bitmap[21];
bildeListe[0] = Properties.Resources.ål;
bildeListe[1] = Properties.Resources.ant;
bildeListe[2] = Properties.Resources.bird;
bildeListe[3] = Properties.Resources.bear;
bildeListe[4] = Properties.Resources.butterfly;
bildeListe[5] = Properties.Resources.cat;
bildeListe[6] = Properties.Resources.chicken;
bildeListe[7] = Properties.Resources.dog;
bildeListe[8] = Properties.Resources.elephant;
bildeListe[9] = Properties.Resources.fish;
bildeListe[10] = Properties.Resources.goat;
bildeListe[11] = Properties.Resources.horse;
bildeListe[12] = Properties.Resources.ladybug;
bildeListe[13] = Properties.Resources.lion;
bildeListe[14] = Properties.Resources.moose;
bildeListe[15] = Properties.Resources.polarbear;
bildeListe[16] = Properties.Resources.reke;
bildeListe[17] = Properties.Resources.sheep;
bildeListe[18] = Properties.Resources.snake;
bildeListe[19] = Properties.Resources.spider;
bildeListe[20] = Properties.Resources.turtle;
I want that array and it´s content in a diffenrent class, and access it from my main form. I don´t know if should use method, function or what to use with arrays. Are there some good way for me to access for instanse bildeListe[0] in my new class?
The simplest way would be to add a property to your class to return that array. That way you always get the correct array if you happen to change it for some reason.
If you want to use a method for returning the image, don't use the other suggested method. It causes many useless objects to be created. One way is to use a static array and method.
class MYBitamp
{
static Bitmap[] bildeListe = new Bitmap[] {
Properties.Resources.ål,
Properties.Resources.ant,
Properties.Resources.bird,
Properties.Resources.bear,
Properties.Resources.butterfly,
Properties.Resources.cat,
Properties.Resources.chicken,
Properties.Resources.dog,
Properties.Resources.elephant,
Properties.Resources.fish,
Properties.Resources.goat,
Properties.Resources.horse,
Properties.Resources.ladybug,
Properties.Resources.lion,
Properties.Resources.moose,
Properties.Resources.polarbear,
Properties.Resources.reke,
Properties.Resources.sheep,
Properties.Resources.snake,
Properties.Resources.spider,
Properties.Resources.turtle
};
public static Bitmap MYarray(int index)
{
return bildeListe[index];
}
}
This way everything is initialized only once and they can be called just my MYBitmap.MYarray(2); without creating an instance of the class. I don't know if you do instantiate the class (maybe it contains something else), but there's still no problem using static here.
Put your array in a method in the class, and then create an object in your main form
class MYBitamp
{
public Bitmap MYarray (int index){
Bitmap[] bildeListe = new Bitmap[21];
bildeListe[0] = Properties.Resources.ål;
bildeListe[1] = Properties.Resources.ant;
bildeListe[2] = Properties.Resources.bird;
bildeListe[3] = Properties.Resources.bear;
bildeListe[4] = Properties.Resources.butterfly;
bildeListe[5] = Properties.Resources.cat;
bildeListe[6] = Properties.Resources.chicken;
bildeListe[7] = Properties.Resources.dog;
bildeListe[8] = Properties.Resources.elephant;
bildeListe[9] = Properties.Resources.fish;
bildeListe[10] = Properties.Resources.goat;
bildeListe[11] = Properties.Resources.horse;
bildeListe[12] = Properties.Resources.ladybug;
bildeListe[13] = Properties.Resources.lion;
bildeListe[14] = Properties.Resources.moose;
bildeListe[15] = Properties.Resources.polarbear;
bildeListe[16] = Properties.Resources.reke;
bildeListe[17] = Properties.Resources.sheep;
bildeListe[18] = Properties.Resources.snake;
bildeListe[19] = Properties.Resources.spider;
bildeListe[20] = Properties.Resources.turtle;
return bildeListe[index];
}
}
and in your main form call it with the index you want
MYBitamp aabc = new MYBitamp();
aabc.MYarray(5);
Related
is it possible to store nested structs or classes in lists using c#?
looking at the following code segments.
Nested Scruct:
struct structBooks
{
public string strBookName;
public string strAuthor;
public structPubished publishedDate;
}
struct structPubished
{
public int intDayOfMonth;
public int intMonthOfYear;
public int intYear;
}
saving as a list:
static void AddBookToList()
{
structBooks testStruct = new structBooks();
testStruct.strBookName = newBookName;
testStruct.strAuthor = newAuther;
testStruct.publishedDate.intYear = intNewYear;
testStruct.publishedDate.intMonthOfYear = intNewMonthOfYear;
testStruct.publishedDate.intDayOfMonth = intNewDayOfMonth;
static List<structBooks> listBooks = new List<structBooks>();
listBooks.Add(new structBooks()
{
strBookName = newBookName,
strAuthor = newAuther,
publishedDate.intYear = intNewYear,
publishedDate.intMonthOfYear = intNewMonthOfYear,
publishedDate.intDayOfMonth = intNewDayOfMonth
});
}
creating all the testStruct's works as expected.
When it comes to storing the struct as a list strBookName and strAuthor both work. However, when it comes to the nested publishedDate Visual Studio tells me "invalid initialiser member declarator".
the list its self is defined in the Main method, I just added it so you can see how it's defined.
what am i missing?
Use new to initialize your publishedDate struct, just as you do with structBooks.
List<structBooks> listBooks = new List<structBooks>();
listBooks.Add(new structBooks()
{
strBookName = "bookName",
strAuthor = "author",
publishedDate = new structPubished
{
intDayOfMonth = 1,
intMonthOfYear = 1,
intYear = 1000
}
});
You need to initialize your struct using the new keyword
List<structBooks> listBooks = new List<structBooks>();
listBooks.Add(new structBooks()
{
strBookName = "bookName",
strAuthor = "author",
publishedDate = new structPubished
{
intDayOfMonth = intNewDayOfMonth,
intMonthOfYear = intNewMonthOfYear,
intYear = intNewYear
}
});
Hope you also realize that you don't actually need to create the structPublished in the first place and could use in-build DateTime.
This would change your structBooks as
struct structBooks
{
public string strBookName;
public string strAuthor;
public DateTime publishedDate;
}
and you can add as
List<structBooks> listBooks = new List<structBooks>();
listBooks.Add(new structBooks()
{
strBookName = "bookName",
strAuthor = "author",
publishedDate = new DateTime(intNewYear,intNewMonthOfYear,intNewDayOfMonth)
});
The inbuild DateTime struct provides a lot of other functionalities which can be useful for your application.
Change this:
testStruct.publishedDate.intYear = intNewYear;
testStruct.publishedDate.intMonthOfYear = intNewMonthOfYear;
testStruct.publishedDate.intDayOfMonth = intNewDayOfMonth;
to this:
testStruct.publishedDate = new structPublished {
intYear = intNewYear,
intMonthOfYear = inNewMonthOfYear,
intDayOfMonth = intNewDayOfMonth
};
You can't initialise something by setting its fields or properties - C# still doesn't know the type of the thing you're trying to initialise. Instead, you need to use the new keyword which is designed for initialising objects.
This is the correct implementation:
static void AddBookToList()
{
structBooks testStruct = new structBooks();
testStruct.strBookName = newBookName;
testStruct.strAuthor = newAuther;
testStruct.publishedDate.intYear = intNewYear;
testStruct.publishedDate.intMonthOfYear = intNewMonthOfYear;
testStruct.publishedDate.intDayOfMonth = intNewDayOfMonth;
List<structBooks> listBooks = new List<structBooks>();
listBooks.Add(new structBooks()
{
strBookName = newBookName,
strAuthor = newAuther,
publishedDate = new structPubished()
{
intYear = intNewYear,
intMonthOfYear = intNewMonthOfYear,
intDayOfMonth = intNewDayOfMonth
}
});
}
The difference between the creation of testStruct and the one inserted to the list, is the way you initialize.
When you do
structBooks testStruct = new structBooks();
it initialize every object inside by using the default constructor, that's why you don't have to type
testStruct.publishedDate = new structPubished();
differently, when you declare the initialization by providing values of the Object, you must specify everything.
You need to use nested object initializer syntax. Notice there is no new keyword required to create the nested struct.
listBooks.Add
(
new structBooks
{
strBookName = newBookName,
strAuthor = newAuther,
publishedDate =
{
intYear = 2018,
intMonthOfYear = 1,
intDayOfMonth = 2
}
}
);
I am trying out the Amazon MWS samples. How do I initialise request.ASINList with a list of ASINs?
My ASINs are in strings.
// Create a request.
GetLowestOfferListingsForASINRequest request = new GetLowestOfferListingsForASINRequest();
string sellerId = "example";
request.SellerId = sellerId;
string mwsAuthToken = "example";
request.MWSAuthToken = mwsAuthToken;
string marketplaceId = "example";
request.MarketplaceId = marketplaceId;
ASINListType asinList = new ASINListType();
request.ASINList = asinList;
string itemCondition = "example";
request.ItemCondition = itemCondition;
bool excludeMe = true;
request.ExcludeMe = excludeMe;
return this.client.GetLowestOfferListingsForASIN(request);
I can't seem to implicitly or explicitly cast a list or array of strings to ASINListType.
Don't know c# but in PHP you have to create an object of class "MarketplaceWebServiceProducts_Model_ASINListType"
e.g.
$asin_list = new MarketplaceWebServiceProducts_Model_ASINListType();
$asin_list->setASIN($asin_array);
$request->setASINList($asin_list);
Your request.ASINList needs to be assigned to an ASINListType. So instantiate that object, and assign your ASINs to it's ASIN property. This is just one way of doing it, but I typically do it very quickly this way:
var asinListType = new ASINListType();
asinListType.ASIN = new List<string> { "B00005TQI7", "B00AVO5XRK", etc, etc };
request.ASINList = asinListType;
I'd like to know how to create the following XML using SharpKml:
<StyleMap id="msn_placemark_circle">
<Pair>
<key>normal</key>
<styleUrl>#sn_placemark_circle</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_placemark_circle_highlight</styleUrl>
</Pair>
</StyleMap>
I've tried several things, but with no success. This is what I have so far:
public static StyleSelector Generate_M_ylw_pushpin3()
{
var stylemap = new StyleMapCollection();
stylemap.Id = "s_ylw-pushpin3";
var normalPair = new Pair();
normalPair.Id = "normal";
normalPair.Selector = StyleGenerator.Generate_s_ylw_pushpin_hl3();
//normalPair.StyleUrl = new Uri(#sh_placemark_circle_highlight); // Exception by .NET
var highlightPair = new Pair();
highlightPair.Id = "highlight";
highlightPair.Selector = StyleGenerator.Generate_s_ylw_pushpin_hl3();
//highlightPair.StyleUrl = new Uri(#sh_placemark_circle_highlight); // Exception by .NET
stylemap.Add(normalPair);
stylemap.Add(highlightPair);
return stylemap;
}
// This code just works fine
public static StyleSelector Generate_s_ylw_pushpin_hl3()
{
var style = new Style();
style.Id = "s_ylw-pushpin_hl3";
var iconStyle = new IconStyle();
iconStyle.Color = Color32.Parse("ff00ff00");
iconStyle.Scale = 1.18182;
iconStyle.Icon = new IconStyle.IconLink(new Uri("http://some/url"));
var labelStyle = new LabelStyle();
labelStyle.Color = Color32.Parse("00ffffff");
style.Icon = iconStyle;
style.Label = labelStyle;
return style;
}
Who knows on how to achieve this?
I've find the answer to my own question:
public static StyleSelector Generate_M_ylw_pushpin3()
{
var stylemap = new StyleMapCollection();
stylemap.Id = "s_ylw-pushpin3";
var normalPair = new Pair();
normalPair.StyleUrl = new Uri("#sh_placemark_circle", UriKind.Relative);
normalPair.State = StyleState.Normal;
var highlightPair = new Pair();
highlightPair.StyleUrl = new Uri("#sh_placemark_circle_highlight", UriKind.Relative);
highlightPair.State = StyleState.Highlight;
stylemap.Add(normalPair);
stylemap.Add(highlightPair);
return stylemap;
}
Martijin added the answer to his own question, which is amazing and helped me get to my solution. Just for an alternative that I believe is slightly more generic I figured I'd drop my solution that I got to thanks to Martijin's answer.
Note: my solution is generating these for a line object to be generated, however this could be used for other styles easily
Creating the style objects for a highlight or normal state. The objects made here are what will be added to the stylemap
Here we just pass in the placemark itself (with some details such as the placemark name, etc., available) and a boolean value as to whether it is a highlight or normal style. My usage is thus:
kmlDom.Style normalStyle = createPlacemarkLineStyle(thisPlacemark, false);
kmlDom.Style highlightStyle = createPlacemarkLineStyle(thisPlacemark, true);
Method:
public kmlDom.Style createPlacemarkLineStyle ( kmlDom.Placemark placemark , bool highlight )
{
kmlDom.Style styleNode = new kmlDom.Style( );
// Add Line Style
kmlDom.LineStyle lineStyle = new kmlDom.LineStyle( );
if( !highlight )
{
styleNode.Id = String.Format( "{0}-normal", placemark.placemarkName );
lineStyle.Color = hexToColor("ff0000ff");
lineStyle.Width = 2;
}
else
{
styleNode.Id = String.Format( "{0}-highlight", placemark.placemarkName );
lineStyle.Color = hexToColor( "ff0000ff" );
lineStyle.Width = 2;
}
styleNode.Line = lineStyle;
return styleNode;
}
Now we create the style selector to be added to the placemark object
Here I pass two style objects and the original placemark object to create the full style map. This is returned to the placemark with a call such as:
thisPlacemark.StyleSelector = createPlacemarkLineStyleMap(placemark, normalStyle, highlightStyle);
Method:
public kmlDom.StyleSelector createPlacemarkLineStyleMap ( kmlDom.Placemark placemark , kmlDom.Style normalStyle , kmlDom.Style highlightStyle )
{
// Set up style map
kmlDom.StyleMapCollection styleMapCollection = new kmlDom.StyleMapCollection( );
styleMapCollection.Id = String.Format( "{0}-stylemap" , placemark.placemarkName );
// Create the normal line pair
kmlDom.Pair normalPair = new kmlDom.Pair();
normalPair.StyleUrl = new Uri(String.Format("#{0}", normalStyle.Id), UriKind.Relative);
normalPair.State = kmlDom.StyleState.Normal;
// Create the highlight line pair
kmlDom.Pair highlightPair = new kmlDom.Pair( );
highlightPair.StyleUrl = new Uri( String.Format( "#{0}" , highlightStyle.Id ) , UriKind.Relative );
highlightPair.State = kmlDom.StyleState.Highlight;
// Attach both pairs to the map
styleMapCollection.Add( normalPair);
styleMapCollection.Add( highlightPair );
return styleMapCollection;
}
Why I have kmlDom, kmlBase, and kmlEngine in front of my object types
** My usings are as follows to separate sharpkml objects from my own internal objects
using kmlBase = SharpKml.Base;
using kmlDom = SharpKml.Dom;
using kmlEngine = SharpKml.Engine;
I have made a ajax post and stringifyed my json and send the data to my controller method:
The data that the controller recieves looks like this:
"[\"0041300201\",1610612764,\"WAS\",\"Washington\",2772,\"Trevor Ariza\",\"F\",\"\",\"37:20\",7,10,0.7,6,6,1,2,4,0.5,1,5,6,2,1,0,0,3,22,18]"
My controller method:
public void addBoxScore(string playerstats)
{
Games gamestats = new Games();
gamestats.GAME_ID = playerstats[0];
gamestats.TEAM_ID = playerstats[1];
gamestats.TEAM_ABBREVIATION = playerstats[2].ToString();
gamestats.TEAM_CITY = playerstats[3].ToString();
gamestats.PLAYER_ID = playerstats[4];
gamestats.PLAYER_NAME = playerstats[5].ToString();
gamestats.START_POSITION = playerstats[6].ToString();
gamestats.COMMENT = playerstats[7].ToString();
gamestats.MIN = playerstats[8];
gamestats.FGM = playerstats[9];
gamestats.FGA = playerstats[10];
gamestats.FG_PCT = playerstats[11];
gamestats.FGTHREEM = playerstats[12];
gamestats.FGTHREEA = playerstats[13];
gamestats.FGTHREE_PCT = playerstats[14];
gamestats.FTM = playerstats[15];
gamestats.FTA = playerstats[16];
gamestats.FT_PCT = playerstats[17];
gamestats.OREB = playerstats[18];
gamestats.DREB = playerstats[19];
gamestats.REB = playerstats[20];
gamestats.AST = playerstats[21];
gamestats.STL = playerstats[22];
gamestats.BLK = playerstats[23];
gamestats.TO = playerstats[24];
gamestats.PF = playerstats[25];
gamestats.PTS = playerstats[26];
gamestats.PLUS_MINUS = playerstats[27];
}
When i do this the gamestats.Game_ID becomes "91" instead of "0041300201" as i wanted it to be.
Please use this array or list of string instead of only strings. like this
public void addBoxScore(string[] playerstats)
I run the code and it will not store the values it stops immediately at the loadingfromscreen. What its supposed to do is a multi-page application form that will reinput the values to the texbox's on the back button from the next form.
The ASP.net code is too long to post, but basically its just texbox's and dropboxes. If needed i can post it, but the main issue im 90% sure is the C# code.
UPDATE: When i say stop it continues the code but will not run the dictionary method...i have put a arrow where the method stops in DICTIONARY ONLY
C#:
public partial class employment_driversapplication_personalinfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<string, string> DriversApplicationData = (Dictionary<string, string>) Session["DriversApp"];
try
{
if (Page.IsPostBack)
{
LoadMemoryFromScreen(DriversApplicationData);
}
else
{
LoadScreenFromMemory(DriversApplicationData);
}
}
catch //(Exception ex)
{
// throw new Exception("Exception occured in employment_driversapplication_personalinfo.aspx - Page_Load" + ex.Message);
}
finally
{
}
}
private void LoadMemoryFromScreen(Dictionary<string, string> DicDriversApp)
{
DicDriversApp["position"] = position.Text; <---Stops here (won't even store this)
DicDriversApp["fname"] = fname.Text;
DicDriversApp["middleinitial"] = middleinitial.Text;
DicDriversApp["lname"] = lname.Text;
DicDriversApp["birthday"] = birthday.Text;
DicDriversApp["proofofage"] = proofofage.SelectedValue;
DicDriversApp["address"] = address.Text;
DicDriversApp["city"] = city.Text;
DicDriversApp["state"] = state.Text;
DicDriversApp["email"] = email.Text;
DicDriversApp["phone"] = phone.Text;
DicDriversApp["altphone"] = altphone.Text;
DicDriversApp["citizen"] = citizen.SelectedValue;
DicDriversApp["whoreferred"] = whoreferred.Text;
DicDriversApp["famfriend"] = famfriend.Text;
DicDriversApp["relationship"] = relationship.Text;
DicDriversApp["rateofpayexpected"] = rateofpayexpected.Text;
DicDriversApp["rateofpaytype"] = RadioButtonList1.SelectedValue;
DicDriversApp["employedNow"] = employednow.SelectedValue;
DicDriversApp["curremployment"] = curremployment.Text;
DicDriversApp["pastAddress"] = pastaddress.SelectedValue;
DicDriversApp["previousAddress"] = previousaddress.Text;
DicDriversApp["previousCity"] = previouscity.Text;
DicDriversApp["previousZip"] = previouszip.Text;
DicDriversApp["previousState"] = previousstate.Text;
DicDriversApp["previousDuration"] = previousduration.Text;
DicDriversApp["previousAddress1"] = previousaddress1.Text;
DicDriversApp["previousCity1"] = previouscity1.Text;
DicDriversApp["previousZip1"] = previouszip1.Text;
DicDriversApp["previousState1"] = previousstate1.Text;
DicDriversApp["previousDuration1"] = previousduration1.Text;
Session["DriversApp"] = DicDriversApp;
}
private void LoadScreenFromMemory(Dictionary<string, string> DicDriversApp)
{
position.Text = DicDriversApp["position"];
fname.Text = DicDriversApp["fname"] ;
middleinitial.Text = DicDriversApp["middleinitial"];
lname.Text = DicDriversApp["lname"];
birthday.Text = DicDriversApp["birthday"];
proofofage.SelectedValue = DicDriversApp["proofofage"];
address.Text = DicDriversApp["address"];
city.Text = DicDriversApp["city"];
state.Text = DicDriversApp["state"];
email.Text = DicDriversApp["email"];
phone.Text = DicDriversApp["phone"];
altphone.Text = DicDriversApp["altphone"];
citizen.SelectedValue = DicDriversApp["citizen"];
whoreferred.Text = DicDriversApp["whoreferred"];
famfriend.Text = DicDriversApp["famfriend"];
relationship.Text = DicDriversApp["relationship"];
rateofpayexpected.Text = DicDriversApp["rateofpayexpected"];
RadioButtonList1.SelectedValue = DicDriversApp["rateofpaytype"];
employednow.SelectedValue = DicDriversApp["employedNow"];
curremployment.Text = DicDriversApp["curremployment"];
pastaddress.SelectedValue = DicDriversApp["pastAddress"];
previousaddress.Text = DicDriversApp["previousAddress"];
previouscity.Text = DicDriversApp["previousCity"];
previouszip.Text = DicDriversApp["previousZip"];
previousstate.Text = DicDriversApp["previousState"];
previousduration.Text = DicDriversApp["previousDuration"];
previousaddress1.Text = DicDriversApp["previousAddress1"];
previouscity1.Text = DicDriversApp["previousCity1"];
previouszip1.Text = DicDriversApp["previousZip1"];
previousstate1.Text = DicDriversApp["previousState1"];
previousduration1.Text = DicDriversApp["previousDuration1"];
}
try something like this:
private void LoadMemoryFromScreen()
{
Dictionary<string, string> driver = new Dictionary<string, string>();
driver.Add("position", position.Text);
driver.Add("othervalue",value.Text); ///etc..
Session["dict"] = driver;
}
then, later on if you want to access the values in your dictionary, use something like this:
var dict = (Dictionary<string, string>)Session["dict"];
problem with this is that you're going to have to use dict.GetElementAt(index) in order to retrieve values, and i don't think that's a very good approach.
This will work, but i kinda dont understand why you are using a dictionary to do this. I assume you are only loading data from 1 person on your page? Then you might as well just make a class with all the properties in it, and then you can just pass that around instead of using this dictionary/session hack.
public class DriversApplicationData
{
public string position {get;set;}
public string firstname {get;set;}
//etc
}
then, you can just do something like
DriversApplicationData data = new DriversApplicationData();
data.position = position.Text;
data.firstname = fname.Text;
this is gonna be alot easier to maintain and to retrieve/insert values.