I have a C# cast problem

0
August 18, 2011

hi

Anybody who can help me with a C# problem in asp.net 2.0

I get this error:

Compiler Error Message: CS0266: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)

Source Error:

Line 28: {

Line 29:    

Line 30:    TabContainer1.TabIndex = whichIndex;

Line 31:

Line 32: }

My code is:

protected void Page_Load(object sender, EventArgs e)

      {

        int whichIndex = -1;

if (Request.QueryString["inde

x"]!= null) // or !=""

{

whichIndex = Convert.ToInt16(Request.Qu

eryString[

"index"]);

if (whichIndex!=-1)

{

   TabContainer1.TabIndex = whichIndex;

}

}

}

Comments See all(0)

Add comment
0
August 18, 2011

Yes, that seems fine.  I was asking about the declaration so we could see what class TabContainer1 is.  Most microsoft classes that have a TabIndex property use an int for that property.  Your TabContainer class uses the int16 which explains the error message, and now makes it clear that error was unrelated to the real issue.

0
August 18, 2011

TabContainer1.TabIndex = whichIndex;

i have to assume that you need to convert or cast whichIndex

Convert.ToInt16(

whichIndex

)  //or

(short) whichIndex

0
August 18, 2011

sorry i am out of it this morning.

try just the cast above instead of the convert.

0
August 18, 2011

TabIndex is generally of type int.  Can you show the declaration of TabContainer1?

0
August 18, 2011

A short is just an Int16, so try this:

TabContainer1.TabIndex = Convert.ToInt16(whichIndex);
0
August 18, 2011

TabContainer1.TabIndex = (short)whichIndex;

0
August 18, 2011

I suspect your problem lies with the first conversion. You declare whichIndex as an int (shorthand for Int32). So, if you change to this your variables and properties should all have matching types.

whichIndex = Convert.ToInt32(Request.Qu

eryString[

"index"]);

Jim

0
August 18, 2011

since everyone already gave a good answer. I'm going to take the time to explain cast and convert

For instance, if variable1 has a value which is a string reference to

"23" then Convert.ToInt32 will parse the int. However, you can't *cast*

from a string to an int.

Another explanation that I use is...

Casting says "This object of type A is really an

object of type B-derived-from-A"

Convert.To*() functions say This object isn't a type B, but there exists

a way to convert to type B"

My phylosophy is...

CAST pretends to change you while CONVERT makes you change if there is a way.

Hope this explains a little bit on that subject..

0
August 18, 2011

the TabContainer1.TabIndex = Convert.ToInt16(whichIndex

);

Make no error more, BUT the tab function did'ent work

0
August 18, 2011

i believe that is because of what matsoo wrote -- tabindex is usually and int(Int32) not a short(Int16)

0
August 18, 2011

Yes, I was feeling invisible.  Can you show the declaration of TabContainer1?

0
August 18, 2011

hI

declaration, can you explain a litle more about this, do not understand your word declaration.

best regard

0
August 18, 2011

my url is

xxxxxxxx/Default.aspx?inde

x=2

0
August 18, 2011

<cc1:TabContainer ID="TabContainer1" ScrollBars="Auto" runat="server" Height="630px" Width="1150px"

ActiveTabIndex="0" Font-Size="X-Small" AutoPostBack="True" style="margin-bottom: 0px"

Font-Bold="True">

<cc1:TabPanel ID="TabPanel2" runat="server" >

0
August 18, 2011

Have you tried changing the way you set whichIndex to:

whichIndex = Convert.ToInt32(Request.Qu

eryString[

"index"]);

Jim

0
August 18, 2011

Here is the solution

It is not  TabContainer1.TabIndex

but TabContainer1.ActiveTabInd

ex

Related Questions

Float Data Type Conversion Problem

I have a data item (AnnualVolume) &#160;defined as a float on the table but I keep getting &quot;Specified cast is not valid&quot; when I try to access the field in my ASP.NET (C#) program. &#160; Ann Read More

Views

39

Votes

0

Answers

1

August 17, 2011

How do I consume a service reference (Soap Data Service)

Greetings Experts, I have what I hope is a simple question. &#160;In the provided code, I have attempted to write a function that, given a product SKU, would return all related products. &#160;The web Read More

Views

52

Votes

0

Answers

2

August 17, 2011

reading xml into a custom built object

hi EE, i have a web service that I call which is fine. &#160;The problem I have is that I would like asp.net to read the xml data and place the content into my custom built object. The web service ret Read More

Views

43

Votes

0

Answers

37

August 17, 2011

Traverse generic List using C# Reflection

Hi, I have three C# classes, A, B, C. B derivates from A and one of it fields is a list of class C: List&lt;C&gt;. C also derives from class A. A has a method called AMethod() with no parameters, so I Read More

Views

47

Votes

0

Answers

7

August 17, 2011

Casting an object back to it's original type at runtime

I am just starting with C++ and managed Extensions and I&#39;m trying to figure out how to convert an object back to it&#39;s original type after a cast. If an object gets implicitly cast to another t Read More

Views

53

Votes

0

Answers

18

August 17, 2011

Asp 3.5 .Net  Bind Checkbox

I have a control inside a user control that looks like this: &lt;EditItemTemplate&gt; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &lt;div style=&quot;float:left&quot;&gt; &#160; &#160; &#1 Read More

Views

44

Votes

0

Answers

10

August 17, 2011

Collection of multiple types

I have a Silverlight 4 stack panel that I have been populating with textblock controls that are stored in a List collection. &#160;List&lt;Textblock&gt; controls = new List&lt;TextBlock&gt;(); I want Read More

Views

58

Votes

0

Answers

4

August 16, 2011

PreviousPage casting problem (Cross page posting)

Okay, so here&#39;s my problem. &#160;I&#39;m very proficient in C#, but I&#39;m learning ASP.NET as we speak, and that&#39;s where my problem is. I am trying to do a Cross-page post, and the actual p Read More

Views

45

Votes

0

Answers

3

August 17, 2011

Uniqueidentifiers when using SQLBulkCopy

I have an xml file that I am copying up to a table within a SQL database. &#160;The program is written in c# for asp.net using Visual Studio 2010. &#160;I am using the sqlbulkcopy command, and have al Read More

Views

62

Votes

0

Answers

2

August 18, 2011

Inserting numeric values into Excel

This is a follow-up question I asked earlier, &quot;cast Datatable string to an integer before insert into Excel&quot;. &#160;The solution answered the question I asked correctly and completely. Howev Read More

Views

53

Votes

0

Answers

9

August 17, 2011

display an object property within arraylist of custom objects

hi EE, I am trying to bind an arraylist to a dropdownlist. my problem is that the arraylist contains my custom object, Website. My class Website, has a public property Name which will return or set th Read More

Views

51

Votes

0

Answers

11

August 17, 2011

c# +returning hashtable.into another function.

i have this class library im building for an application. every thing works good, except that i want to add a new function that would return an hashtable &quot;database value and their field&quot; to Read More

Views

38

Votes

0

Answers

4

August 17, 2011

How can I get integer value,or type  from maskedtextBox.Tex when the textBox is blank by using    serial_no =  Convert.ToInt32(maskedTextBox.Text);

I got a problem when I use textBox.Text or maskTextBox.Text in C# ,visual studio2008 to cast the value to integer value by using the command Convert.toInt32 like int &#160; serial_no = &#160;Convert.T Read More

Views

42

Votes

0

Answers

1

August 16, 2011

Casting List<> of derived class to List<> of base class?

Platform: &#160; &#160; &#160;.NET 2.0 Language: &#160; C# Problem: I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List&lt;DerivedClass&gt;. I need Read More

Views

34

Votes

0

Answers

1

August 17, 2011

Programmatically Select DropDownList Item

Experts, I have the following asp:dropdownlist: (see code snippet). I am holding user addresses in my database. &#160;The &quot;State&quot; column in the database is/will be updated using the same dro Read More

Views

39

Votes

0

Answers

5

August 16, 2011

Passing Data with Server.Transfer - ASP.NET/C#

I&#39;ve seen a few articles on passing data with the Server.Transfer method but they don&#39;t address my problem. I will have many pages that will Server.Transfer to the same page, lets call these P Read More

Views

33

Votes

0

Answers

19

August 17, 2011

getting new textbox value in for loop

I am trying to loop through a comma delimited string. each on i want to get the value of the textbox after i click the update link in a c# datagrid. The code i have will run the first time around but Read More

Views

33

Votes

0

Answers

3

August 17, 2011

explicit conversion between Integer and Short in VB.Net

Hi, I am converting a piece of C# code to vb.net and have run into a problem with casting an Integer (Int32) to a Short (Int16). In the C# Version of the code when I perform the following: int tmpInt Read More

Views

46

Votes

0

Answers

9

August 16, 2011

messagebox

hey does any know how to display a message box on the sever side? i am using update panlel C# coding and AsyncPostBackTrigger, &#160;PostBackTrigger i need a class that i can call a messagebox(STRING) Read More

Views

70

Votes

0

Answers

31

August 16, 2011

This is the very first question

Hello guys! This is the first post in this site Read More

Views

2k

Votes

4

Answers

2

January 08, 2016